@alpha-arcade/sdk 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -91,7 +91,7 @@ const result = await client.createLimitOrder({
91
91
  quantity: 2_000_000, // 2 shares in microunits
92
92
  isBuying: true,
93
93
  });
94
- // result: { escrowAppId, txIds, confirmedRound }
94
+ // result: { escrowAppId, txIds, confirmedRound, matchedQuantity?, matchedPrice? }
95
95
  ```
96
96
 
97
97
  #### `createMarketOrder(params)`
@@ -107,7 +107,7 @@ const result = await client.createMarketOrder({
107
107
  isBuying: true,
108
108
  slippage: 50_000, // $0.05 slippage tolerance
109
109
  });
110
- // result: { escrowAppId, matchedQuantity, txIds, confirmedRound }
110
+ // result: { escrowAppId, txIds, confirmedRound, matchedQuantity, matchedPrice }
111
111
  ```
112
112
 
113
113
  #### `cancelOrder(params)`
package/dist/index.cjs CHANGED
@@ -1427,6 +1427,12 @@ var calculateMatchingOrders = (orderbook, isBuying, isYes, quantity, price, slip
1427
1427
  }
1428
1428
  return matches;
1429
1429
  };
1430
+
1431
+ // src/constants.ts
1432
+ var DEFAULT_API_BASE_URL = "https://partners.alphaarcade.com/api";
1433
+ var DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
1434
+
1435
+ // src/modules/orderbook.ts
1430
1436
  var getAllCreatedApplications = async (indexerClient, address, limit = 100) => {
1431
1437
  let applications = [];
1432
1438
  let nextToken;
@@ -1520,6 +1526,49 @@ var getOpenOrders = async (config, marketAppId, walletAddress) => {
1520
1526
  owner: o.globalState.owner ?? ""
1521
1527
  }));
1522
1528
  };
1529
+ var normalizeApiOrder = (raw) => ({
1530
+ escrowAppId: Number(raw.escrowAppId ?? raw.orderId),
1531
+ marketAppId: Number(raw.marketAppId),
1532
+ position: raw.orderPosition ?? 0,
1533
+ side: raw.orderSide === "BUY" ? 1 : 0,
1534
+ price: raw.orderPrice ?? 0,
1535
+ quantity: raw.orderQuantity ?? 0,
1536
+ quantityFilled: raw.orderQuantityFilled ?? 0,
1537
+ slippage: raw.slippage ?? 0,
1538
+ owner: raw.senderWallet ?? ""
1539
+ });
1540
+ var getWalletOrdersFromApi = async (config, walletAddress) => {
1541
+ if (!config.apiKey) {
1542
+ throw new Error("apiKey is required for API-based market fetching.");
1543
+ }
1544
+ const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
1545
+ const allOrders = [];
1546
+ let cursor;
1547
+ let hasMore = true;
1548
+ while (hasMore) {
1549
+ const params = new URLSearchParams({ wallet: walletAddress, limit: "300" });
1550
+ if (cursor) {
1551
+ params.set("cursor", cursor);
1552
+ }
1553
+ const url = `${baseUrl}/get-wallet-orders?${params.toString()}`;
1554
+ const response = await fetch(url, { headers: { "x-api-key": config.apiKey } });
1555
+ if (!response.ok) {
1556
+ throw new Error(`Alpha API error: ${response.status} ${response.statusText}`);
1557
+ }
1558
+ const data = await response.json();
1559
+ if (Array.isArray(data)) {
1560
+ allOrders.push(...data.map(normalizeApiOrder));
1561
+ hasMore = false;
1562
+ } else if (data.orders) {
1563
+ allOrders.push(...data.orders.map(normalizeApiOrder));
1564
+ cursor = data.nextCursor ?? void 0;
1565
+ hasMore = data.hasMore === true && !!cursor;
1566
+ } else {
1567
+ hasMore = false;
1568
+ }
1569
+ }
1570
+ return allOrders;
1571
+ };
1523
1572
 
1524
1573
  // src/modules/trading.ts
1525
1574
  var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
@@ -1544,7 +1593,21 @@ var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
1544
1593
  return 0;
1545
1594
  };
1546
1595
  var createLimitOrder = async (config, params) => {
1547
- return createOrder(config, { ...params, slippage: 0, matchingOrders: [] });
1596
+ const orderbook = await getOrderbook(config, params.marketAppId);
1597
+ const matchingOrders = calculateMatchingOrders(
1598
+ orderbook,
1599
+ params.isBuying,
1600
+ params.position === 1,
1601
+ params.quantity,
1602
+ params.price,
1603
+ 0
1604
+ );
1605
+ const totalMatchedQuantity = matchingOrders.reduce((sum, o) => sum + o.quantity, 0);
1606
+ const matchedPrice = totalMatchedQuantity > 0 ? Math.round(
1607
+ matchingOrders.reduce((sum, o) => sum + (o.price ?? params.price) * o.quantity, 0) / totalMatchedQuantity
1608
+ ) : params.price;
1609
+ const result = await createOrder(config, { ...params, slippage: 0, matchingOrders });
1610
+ return { ...result, matchedQuantity: totalMatchedQuantity, matchedPrice };
1548
1611
  };
1549
1612
  var createMarketOrder = async (config, params) => {
1550
1613
  let matchingOrders = params.matchingOrders;
@@ -1967,8 +2030,6 @@ var getPositions = async (config, walletAddress) => {
1967
2030
  };
1968
2031
 
1969
2032
  // src/modules/markets.ts
1970
- var DEFAULT_API_BASE_URL = "https://partners.alphaarcade.com/api";
1971
- var DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
1972
2033
  var toSeconds = (ts) => ts > 1e10 ? Math.floor(ts / 1e3) : ts;
1973
2034
  var groupMultiChoiceMarkets = (flatMarkets) => {
1974
2035
  const parentMap = /* @__PURE__ */ new Map();
@@ -2161,7 +2222,7 @@ var AlphaClient = class {
2161
2222
  if (!config.usdcAssetId) throw new Error("usdcAssetId is required");
2162
2223
  this.config = {
2163
2224
  ...config,
2164
- apiBaseUrl: config.apiBaseUrl ?? "https://partners.alphaarcade.com/api"
2225
+ apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL
2165
2226
  };
2166
2227
  }
2167
2228
  // ============================================
@@ -2293,6 +2354,15 @@ var AlphaClient = class {
2293
2354
  async getOpenOrders(marketAppId, walletAddress) {
2294
2355
  return getOpenOrders(this.config, marketAppId, walletAddress);
2295
2356
  }
2357
+ /**
2358
+ * Gets all open orders for a wallet across every live market using the Alpha REST API.
2359
+ *
2360
+ * @param walletAddress - The wallet address
2361
+ * @returns Array of open orders for the wallet
2362
+ */
2363
+ async getWalletOrdersFromApi(walletAddress) {
2364
+ return getWalletOrdersFromApi(this.config, walletAddress);
2365
+ }
2296
2366
  // ============================================
2297
2367
  // Markets
2298
2368
  // ============================================
@@ -2362,6 +2432,7 @@ var AlphaClient = class {
2362
2432
  };
2363
2433
 
2364
2434
  exports.AlphaClient = AlphaClient;
2435
+ exports.DEFAULT_API_BASE_URL = DEFAULT_API_BASE_URL;
2365
2436
  exports.DEFAULT_MARKET_CREATOR_ADDRESS = DEFAULT_MARKET_CREATOR_ADDRESS;
2366
2437
  exports.calculateFee = calculateFee;
2367
2438
  exports.calculateFeeFromTotal = calculateFeeFromTotal;