@alpha-arcade/sdk 0.4.0 → 0.4.2

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/dist/index.d.cts CHANGED
@@ -276,6 +276,11 @@ type AggregatedOrderbook = {
276
276
  yes: AggregatedOrderbookSide;
277
277
  no: AggregatedOrderbookSide;
278
278
  };
279
+ /**
280
+ * Full processed market orderbook keyed by marketAppId.
281
+ * Matches the REST `/get-full-orderbook` response and `orderbook_changed.orderbook`.
282
+ */
283
+ type FullOrderbookSnapshot = Record<string, WsOrderbookApp>;
279
284
  /** Parameters for split shares */
280
285
  type SplitSharesParams = {
281
286
  /** Market app ID */
@@ -410,7 +415,7 @@ type OrderbookChangedEvent = {
410
415
  type: 'orderbook_changed';
411
416
  ts: number;
412
417
  marketId: string;
413
- orderbook: Record<string, WsOrderbookApp>;
418
+ orderbook: FullOrderbookSnapshot;
414
419
  };
415
420
  /** Payload for markets_changed events (incremental diffs) */
416
421
  type MarketsChangedEvent = {
@@ -439,7 +444,7 @@ type WebSocketStreamEvent = MarketsChangedEvent | MarketChangedEvent | Orderbook
439
444
  * Provides methods for:
440
445
  * - **Trading**: Create limit/market orders, cancel orders, propose matches
441
446
  * - **Positions**: Split/merge shares, claim resolved tokens, view positions
442
- * - **Orderbook**: Read on-chain orderbook for any market
447
+ * - **Orderbook**: Read on-chain orderbooks or fetch full API-backed market snapshots
443
448
  * - **Markets**: Fetch live markets from the Alpha API
444
449
  *
445
450
  * @example
@@ -593,6 +598,17 @@ declare class AlphaClient {
593
598
  * @returns Orderbook with yes and no sides, each having bids and asks
594
599
  */
595
600
  getOrderbook(marketAppId: number): Promise<Orderbook>;
601
+ /**
602
+ * Fetches the full processed orderbook snapshot for a market from the Alpha REST API.
603
+ *
604
+ * Returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by
605
+ * `marketAppId`, where each value includes aggregated bids/asks plus detailed yes/no orders.
606
+ * Requires `apiKey`.
607
+ *
608
+ * @param marketId - The Alpha market UUID
609
+ * @returns Full processed market orderbook keyed by marketAppId
610
+ */
611
+ getFullOrderbookFromApi(marketId: string): Promise<FullOrderbookSnapshot>;
596
612
  /**
597
613
  * Gets open orders for a specific wallet on a market.
598
614
  *
@@ -878,4 +894,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
878
894
  */
879
895
  declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
880
896
 
881
- export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, AlphaWebSocket, type AlphaWebSocketConfig, type AmendOrderParams, type AmendOrderResult, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateOrderResult, DEFAULT_API_BASE_URL, DEFAULT_MARKET_CREATOR_ADDRESS, DEFAULT_WSS_BASE_URL, type EscrowGlobalState, type Market, type MarketChangedEvent, type MarketGlobalState, type MarketOption, type MarketsChangedEvent, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookChangedEvent, type OrderbookEntry, type OrderbookSide, type Position, type ProcessMatchParams, type ProcessMatchResult, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletOrdersChangedEvent, type WalletPosition, type WebSocketStreamEvent, type WsOrderbookAggregatedEntry, type WsOrderbookApp, type WsOrderbookDetailEntry, type WsOrderbookDetailSide, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
897
+ export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, AlphaWebSocket, type AlphaWebSocketConfig, type AmendOrderParams, type AmendOrderResult, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateOrderResult, DEFAULT_API_BASE_URL, DEFAULT_MARKET_CREATOR_ADDRESS, DEFAULT_WSS_BASE_URL, type EscrowGlobalState, type FullOrderbookSnapshot, type Market, type MarketChangedEvent, type MarketGlobalState, type MarketOption, type MarketsChangedEvent, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookChangedEvent, type OrderbookEntry, type OrderbookSide, type Position, type ProcessMatchParams, type ProcessMatchResult, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletOrdersChangedEvent, type WalletPosition, type WebSocketStreamEvent, type WsOrderbookAggregatedEntry, type WsOrderbookApp, type WsOrderbookDetailEntry, type WsOrderbookDetailSide, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
package/dist/index.d.ts CHANGED
@@ -276,6 +276,11 @@ type AggregatedOrderbook = {
276
276
  yes: AggregatedOrderbookSide;
277
277
  no: AggregatedOrderbookSide;
278
278
  };
279
+ /**
280
+ * Full processed market orderbook keyed by marketAppId.
281
+ * Matches the REST `/get-full-orderbook` response and `orderbook_changed.orderbook`.
282
+ */
283
+ type FullOrderbookSnapshot = Record<string, WsOrderbookApp>;
279
284
  /** Parameters for split shares */
280
285
  type SplitSharesParams = {
281
286
  /** Market app ID */
@@ -410,7 +415,7 @@ type OrderbookChangedEvent = {
410
415
  type: 'orderbook_changed';
411
416
  ts: number;
412
417
  marketId: string;
413
- orderbook: Record<string, WsOrderbookApp>;
418
+ orderbook: FullOrderbookSnapshot;
414
419
  };
415
420
  /** Payload for markets_changed events (incremental diffs) */
416
421
  type MarketsChangedEvent = {
@@ -439,7 +444,7 @@ type WebSocketStreamEvent = MarketsChangedEvent | MarketChangedEvent | Orderbook
439
444
  * Provides methods for:
440
445
  * - **Trading**: Create limit/market orders, cancel orders, propose matches
441
446
  * - **Positions**: Split/merge shares, claim resolved tokens, view positions
442
- * - **Orderbook**: Read on-chain orderbook for any market
447
+ * - **Orderbook**: Read on-chain orderbooks or fetch full API-backed market snapshots
443
448
  * - **Markets**: Fetch live markets from the Alpha API
444
449
  *
445
450
  * @example
@@ -593,6 +598,17 @@ declare class AlphaClient {
593
598
  * @returns Orderbook with yes and no sides, each having bids and asks
594
599
  */
595
600
  getOrderbook(marketAppId: number): Promise<Orderbook>;
601
+ /**
602
+ * Fetches the full processed orderbook snapshot for a market from the Alpha REST API.
603
+ *
604
+ * Returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by
605
+ * `marketAppId`, where each value includes aggregated bids/asks plus detailed yes/no orders.
606
+ * Requires `apiKey`.
607
+ *
608
+ * @param marketId - The Alpha market UUID
609
+ * @returns Full processed market orderbook keyed by marketAppId
610
+ */
611
+ getFullOrderbookFromApi(marketId: string): Promise<FullOrderbookSnapshot>;
596
612
  /**
597
613
  * Gets open orders for a specific wallet on a market.
598
614
  *
@@ -878,4 +894,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
878
894
  */
879
895
  declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
880
896
 
881
- export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, AlphaWebSocket, type AlphaWebSocketConfig, type AmendOrderParams, type AmendOrderResult, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateOrderResult, DEFAULT_API_BASE_URL, DEFAULT_MARKET_CREATOR_ADDRESS, DEFAULT_WSS_BASE_URL, type EscrowGlobalState, type Market, type MarketChangedEvent, type MarketGlobalState, type MarketOption, type MarketsChangedEvent, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookChangedEvent, type OrderbookEntry, type OrderbookSide, type Position, type ProcessMatchParams, type ProcessMatchResult, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletOrdersChangedEvent, type WalletPosition, type WebSocketStreamEvent, type WsOrderbookAggregatedEntry, type WsOrderbookApp, type WsOrderbookDetailEntry, type WsOrderbookDetailSide, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
897
+ export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, AlphaWebSocket, type AlphaWebSocketConfig, type AmendOrderParams, type AmendOrderResult, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateOrderResult, DEFAULT_API_BASE_URL, DEFAULT_MARKET_CREATOR_ADDRESS, DEFAULT_WSS_BASE_URL, type EscrowGlobalState, type FullOrderbookSnapshot, type Market, type MarketChangedEvent, type MarketGlobalState, type MarketOption, type MarketsChangedEvent, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookChangedEvent, type OrderbookEntry, type OrderbookSide, type Position, type ProcessMatchParams, type ProcessMatchResult, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletOrdersChangedEvent, type WalletPosition, type WebSocketStreamEvent, type WsOrderbookAggregatedEntry, type WsOrderbookApp, type WsOrderbookDetailEntry, type WsOrderbookDetailSide, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
package/dist/index.js CHANGED
@@ -2244,6 +2244,18 @@ var getWalletOrdersFromApi = async (config, walletAddress) => {
2244
2244
  }
2245
2245
  return allOrders;
2246
2246
  };
2247
+ var getFullOrderbookFromApi = async (config, marketId) => {
2248
+ if (!config.apiKey) {
2249
+ throw new Error("apiKey is required for API-based orderbook fetching. Retrieve an API key from the Alpha Arcade platform via the Account page and pass it to the client.");
2250
+ }
2251
+ const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
2252
+ const url = `${baseUrl}/get-full-orderbook?marketId=${encodeURIComponent(marketId)}`;
2253
+ const response = await fetch(url, { headers: { "x-api-key": config.apiKey } });
2254
+ if (!response.ok) {
2255
+ throw new Error(`Alpha API error: ${response.status} ${response.statusText}`);
2256
+ }
2257
+ return response.json();
2258
+ };
2247
2259
 
2248
2260
  // src/modules/trading.ts
2249
2261
  var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
@@ -3250,6 +3262,19 @@ var AlphaClient = class {
3250
3262
  async getOrderbook(marketAppId) {
3251
3263
  return getOrderbook(this.config, marketAppId);
3252
3264
  }
3265
+ /**
3266
+ * Fetches the full processed orderbook snapshot for a market from the Alpha REST API.
3267
+ *
3268
+ * Returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by
3269
+ * `marketAppId`, where each value includes aggregated bids/asks plus detailed yes/no orders.
3270
+ * Requires `apiKey`.
3271
+ *
3272
+ * @param marketId - The Alpha market UUID
3273
+ * @returns Full processed market orderbook keyed by marketAppId
3274
+ */
3275
+ async getFullOrderbookFromApi(marketId) {
3276
+ return getFullOrderbookFromApi(this.config, marketId);
3277
+ }
3253
3278
  /**
3254
3279
  * Gets open orders for a specific wallet on a market.
3255
3280
  *
@@ -3424,7 +3449,7 @@ var AlphaWebSocket = class {
3424
3449
  }
3425
3450
  /** Query a server property (e.g. "heartbeat", "limits") */
3426
3451
  getProperty(property) {
3427
- return this.sendRequest({ method: "GET_PROPERTY", property });
3452
+ return this.sendRequest({ method: "GET_PROPERTY", params: [property] });
3428
3453
  }
3429
3454
  // ============================================
3430
3455
  // Lifecycle
@@ -3524,9 +3549,10 @@ var AlphaWebSocket = class {
3524
3549
  this.send({ method: "PONG" });
3525
3550
  return;
3526
3551
  }
3527
- if (msg.requestId && this.pendingRequests.has(msg.requestId)) {
3528
- const req = this.pendingRequests.get(msg.requestId);
3529
- this.pendingRequests.delete(msg.requestId);
3552
+ const responseId = typeof msg.id === "string" ? msg.id : typeof msg.requestId === "string" ? msg.requestId : null;
3553
+ if (responseId && this.pendingRequests.has(responseId)) {
3554
+ const req = this.pendingRequests.get(responseId);
3555
+ this.pendingRequests.delete(responseId);
3530
3556
  clearTimeout(req.timer);
3531
3557
  req.resolve(msg);
3532
3558
  return;
@@ -3543,10 +3569,10 @@ var AlphaWebSocket = class {
3543
3569
  }
3544
3570
  }
3545
3571
  sendSubscribe(stream, params) {
3546
- this.send({ method: "SUBSCRIBE", stream, ...params });
3572
+ this.send({ method: "SUBSCRIBE", params: [{ stream, ...params }] });
3547
3573
  }
3548
3574
  sendUnsubscribe(stream, params) {
3549
- this.send({ method: "UNSUBSCRIBE", stream, ...params });
3575
+ this.send({ method: "UNSUBSCRIBE", params: [{ stream, ...params }] });
3550
3576
  }
3551
3577
  sendRequest(payload, timeoutMs = 1e4) {
3552
3578
  const requestId = crypto.randomUUID();
@@ -3558,10 +3584,10 @@ var AlphaWebSocket = class {
3558
3584
  this.pendingRequests.set(requestId, { resolve, reject, timer });
3559
3585
  if (!this.connected) {
3560
3586
  this.connect().then(() => {
3561
- this.send({ ...payload, requestId });
3587
+ this.send({ ...payload, id: requestId });
3562
3588
  }).catch(reject);
3563
3589
  } else {
3564
- this.send({ ...payload, requestId });
3590
+ this.send({ ...payload, id: requestId });
3565
3591
  }
3566
3592
  });
3567
3593
  }
@@ -3576,7 +3602,7 @@ var AlphaWebSocket = class {
3576
3602
  startHeartbeat() {
3577
3603
  this.stopHeartbeat();
3578
3604
  this.heartbeatTimer = setInterval(() => {
3579
- this.send({ type: "ping" });
3605
+ this.send({ method: "PING" });
3580
3606
  }, this.heartbeatIntervalMs);
3581
3607
  }
3582
3608
  stopHeartbeat() {