@alpha-arcade/sdk 0.3.0 → 0.3.5
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 +164 -2
- package/dist/index.cjs +467 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +217 -2
- package/dist/index.d.ts +217 -2
- package/dist/index.js +466 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -147,6 +147,18 @@ type ProposeMatchParams = {
|
|
|
147
147
|
/** Quantity to match in microunits */
|
|
148
148
|
quantityMatched: number;
|
|
149
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* Parameters for matching two existing limit orders (e.g. after an amend).
|
|
152
|
+
* The taker is the order placed last and pays the fee; the maker is the counterparty.
|
|
153
|
+
*/
|
|
154
|
+
type ProcessMatchParams = {
|
|
155
|
+
/** Market app ID */
|
|
156
|
+
marketAppId: number;
|
|
157
|
+
/** Maker escrow app ID (existing order — was on the book first) */
|
|
158
|
+
makerEscrowAppId: number;
|
|
159
|
+
/** Taker escrow app ID (existing order — placed last, pays the fee) */
|
|
160
|
+
takerEscrowAppId: number;
|
|
161
|
+
};
|
|
150
162
|
/** Parameters for amending (editing) an existing unfilled order */
|
|
151
163
|
type AmendOrderParams = {
|
|
152
164
|
/** Market app ID */
|
|
@@ -202,6 +214,15 @@ type ProposeMatchResult = {
|
|
|
202
214
|
/** Confirmed round number */
|
|
203
215
|
confirmedRound: number;
|
|
204
216
|
};
|
|
217
|
+
/** Result of processing a match between two existing orders */
|
|
218
|
+
type ProcessMatchResult = {
|
|
219
|
+
/** Whether the match succeeded */
|
|
220
|
+
success: boolean;
|
|
221
|
+
/** Transaction IDs */
|
|
222
|
+
txIds: string[];
|
|
223
|
+
/** Confirmed round number */
|
|
224
|
+
confirmedRound: number;
|
|
225
|
+
};
|
|
205
226
|
/** Result of amending an order */
|
|
206
227
|
type AmendOrderResult = {
|
|
207
228
|
/** Whether the amendment succeeded */
|
|
@@ -210,6 +231,10 @@ type AmendOrderResult = {
|
|
|
210
231
|
txIds: string[];
|
|
211
232
|
/** Confirmed round number */
|
|
212
233
|
confirmedRound: number;
|
|
234
|
+
/** Total quantity matched against counterparty orders (if matching was performed) */
|
|
235
|
+
matchedQuantity?: number;
|
|
236
|
+
/** Volume-weighted average fill price in microunits (if any matches) */
|
|
237
|
+
matchedPrice?: number;
|
|
213
238
|
};
|
|
214
239
|
/** A single entry in the orderbook (one price level, one order) */
|
|
215
240
|
type OrderbookEntry = {
|
|
@@ -251,6 +276,11 @@ type AggregatedOrderbook = {
|
|
|
251
276
|
yes: AggregatedOrderbookSide;
|
|
252
277
|
no: AggregatedOrderbookSide;
|
|
253
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>;
|
|
254
284
|
/** Parameters for split shares */
|
|
255
285
|
type SplitSharesParams = {
|
|
256
286
|
/** Market app ID */
|
|
@@ -337,6 +367,78 @@ type EscrowGlobalState = {
|
|
|
337
367
|
asset_listed?: number;
|
|
338
368
|
fee_timer_start?: number;
|
|
339
369
|
};
|
|
370
|
+
/** Configuration for AlphaWebSocket */
|
|
371
|
+
type AlphaWebSocketConfig = {
|
|
372
|
+
/** WebSocket URL override (default: wss://wss.platform.alphaarcade.com) */
|
|
373
|
+
url?: string;
|
|
374
|
+
/** Enable auto-reconnect on unexpected disconnect (default: true) */
|
|
375
|
+
reconnect?: boolean;
|
|
376
|
+
/** Maximum reconnect attempts before giving up (default: Infinity) */
|
|
377
|
+
maxReconnectAttempts?: number;
|
|
378
|
+
/** Heartbeat interval in ms (default: 60000) */
|
|
379
|
+
heartbeatIntervalMs?: number;
|
|
380
|
+
/**
|
|
381
|
+
* WebSocket constructor to use. Defaults to the global `WebSocket`.
|
|
382
|
+
* On Node.js < 22, pass the `ws` package: `import WebSocket from 'ws'; new AlphaWebSocket({ WebSocket })`
|
|
383
|
+
*/
|
|
384
|
+
WebSocket?: unknown;
|
|
385
|
+
};
|
|
386
|
+
/** Orderbook bid/ask entry at the top level (decimal cents) */
|
|
387
|
+
type WsOrderbookAggregatedEntry = {
|
|
388
|
+
price: number;
|
|
389
|
+
quantity: number;
|
|
390
|
+
total: number;
|
|
391
|
+
};
|
|
392
|
+
/** Orderbook bid/ask entry with escrow details (raw microunit prices) */
|
|
393
|
+
type WsOrderbookDetailEntry = {
|
|
394
|
+
price: number;
|
|
395
|
+
quantity: number;
|
|
396
|
+
total: number;
|
|
397
|
+
escrowAppId: number;
|
|
398
|
+
owner: string;
|
|
399
|
+
};
|
|
400
|
+
/** Per-side orderbook detail (yes or no) */
|
|
401
|
+
type WsOrderbookDetailSide = {
|
|
402
|
+
bids: WsOrderbookDetailEntry[];
|
|
403
|
+
asks: WsOrderbookDetailEntry[];
|
|
404
|
+
};
|
|
405
|
+
/** Orderbook data for a single app within the orderbook payload */
|
|
406
|
+
type WsOrderbookApp = {
|
|
407
|
+
bids: WsOrderbookAggregatedEntry[];
|
|
408
|
+
asks: WsOrderbookAggregatedEntry[];
|
|
409
|
+
spread: number;
|
|
410
|
+
yes: WsOrderbookDetailSide;
|
|
411
|
+
no: WsOrderbookDetailSide;
|
|
412
|
+
};
|
|
413
|
+
/** Payload for orderbook_changed events */
|
|
414
|
+
type OrderbookChangedEvent = {
|
|
415
|
+
type: 'orderbook_changed';
|
|
416
|
+
ts: number;
|
|
417
|
+
marketId: string;
|
|
418
|
+
slug?: string;
|
|
419
|
+
version: number;
|
|
420
|
+
orderbook: FullOrderbookSnapshot;
|
|
421
|
+
};
|
|
422
|
+
/** Payload for markets_changed events (incremental diffs) */
|
|
423
|
+
type MarketsChangedEvent = {
|
|
424
|
+
type: 'markets_changed';
|
|
425
|
+
ts: number;
|
|
426
|
+
[key: string]: unknown;
|
|
427
|
+
};
|
|
428
|
+
/** Payload for market_changed events (single market) */
|
|
429
|
+
type MarketChangedEvent = {
|
|
430
|
+
type: 'market_changed';
|
|
431
|
+
ts: number;
|
|
432
|
+
[key: string]: unknown;
|
|
433
|
+
};
|
|
434
|
+
/** Payload for wallet_orders_changed events */
|
|
435
|
+
type WalletOrdersChangedEvent = {
|
|
436
|
+
type: 'wallet_orders_changed';
|
|
437
|
+
ts: number;
|
|
438
|
+
[key: string]: unknown;
|
|
439
|
+
};
|
|
440
|
+
/** Discriminated union of all WebSocket stream events */
|
|
441
|
+
type WebSocketStreamEvent = MarketsChangedEvent | MarketChangedEvent | OrderbookChangedEvent | WalletOrdersChangedEvent;
|
|
340
442
|
|
|
341
443
|
/**
|
|
342
444
|
* The main client for interacting with Alpha Market prediction markets on Algorand.
|
|
@@ -344,7 +446,7 @@ type EscrowGlobalState = {
|
|
|
344
446
|
* Provides methods for:
|
|
345
447
|
* - **Trading**: Create limit/market orders, cancel orders, propose matches
|
|
346
448
|
* - **Positions**: Split/merge shares, claim resolved tokens, view positions
|
|
347
|
-
* - **Orderbook**: Read on-chain
|
|
449
|
+
* - **Orderbook**: Read on-chain orderbooks or fetch full API-backed market snapshots
|
|
348
450
|
* - **Markets**: Fetch live markets from the Alpha API
|
|
349
451
|
*
|
|
350
452
|
* @example
|
|
@@ -423,6 +525,17 @@ declare class AlphaClient {
|
|
|
423
525
|
* @returns Whether the match succeeded
|
|
424
526
|
*/
|
|
425
527
|
proposeMatch(params: ProposeMatchParams): Promise<ProposeMatchResult>;
|
|
528
|
+
/**
|
|
529
|
+
* Matches two existing limit orders (no create-escrow in the group).
|
|
530
|
+
*
|
|
531
|
+
* Calls the market app's process_potential_match(maker, taker). Use this
|
|
532
|
+
* after amending an order: the amended order is the taker (pays the fee),
|
|
533
|
+
* the counterparty is the maker.
|
|
534
|
+
*
|
|
535
|
+
* @param params - Process match params (marketAppId, makerEscrowAppId, takerEscrowAppId)
|
|
536
|
+
* @returns Whether the match succeeded
|
|
537
|
+
*/
|
|
538
|
+
processMatch(params: ProcessMatchParams): Promise<ProcessMatchResult>;
|
|
426
539
|
/**
|
|
427
540
|
* Amends (edits) an existing unfilled order in-place.
|
|
428
541
|
*
|
|
@@ -487,6 +600,17 @@ declare class AlphaClient {
|
|
|
487
600
|
* @returns Orderbook with yes and no sides, each having bids and asks
|
|
488
601
|
*/
|
|
489
602
|
getOrderbook(marketAppId: number): Promise<Orderbook>;
|
|
603
|
+
/**
|
|
604
|
+
* Fetches the full processed orderbook snapshot for a market from the Alpha REST API.
|
|
605
|
+
*
|
|
606
|
+
* Returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by
|
|
607
|
+
* `marketAppId`, where each value includes aggregated bids/asks plus detailed yes/no orders.
|
|
608
|
+
* Requires `apiKey`.
|
|
609
|
+
*
|
|
610
|
+
* @param marketId - The Alpha market UUID
|
|
611
|
+
* @returns Full processed market orderbook keyed by marketAppId
|
|
612
|
+
*/
|
|
613
|
+
getFullOrderbookFromApi(marketId: string): Promise<FullOrderbookSnapshot>;
|
|
490
614
|
/**
|
|
491
615
|
* Gets open orders for a specific wallet on a market.
|
|
492
616
|
*
|
|
@@ -561,6 +685,96 @@ declare class AlphaClient {
|
|
|
561
685
|
getMarketFromApi(marketId: string): Promise<Market | null>;
|
|
562
686
|
}
|
|
563
687
|
|
|
688
|
+
/**
|
|
689
|
+
* Real-time WebSocket client for Alpha Market platform streams.
|
|
690
|
+
*
|
|
691
|
+
* Connects to `wss://wss.platform.alphaarcade.com` and provides typed,
|
|
692
|
+
* callback-based subscriptions for live market data. No auth required.
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* ```typescript
|
|
696
|
+
* // Node.js 22+ or browser (native WebSocket)
|
|
697
|
+
* const ws = new AlphaWebSocket();
|
|
698
|
+
*
|
|
699
|
+
* // Node.js < 22 — pass the `ws` package
|
|
700
|
+
* import WebSocket from 'ws';
|
|
701
|
+
* const ws = new AlphaWebSocket({ WebSocket });
|
|
702
|
+
*
|
|
703
|
+
* const unsub = ws.subscribeOrderbook('will-btc-hit-100k', (event) => {
|
|
704
|
+
* console.log('Orderbook:', event.orderbook);
|
|
705
|
+
* });
|
|
706
|
+
*
|
|
707
|
+
* // Later
|
|
708
|
+
* unsub();
|
|
709
|
+
* ws.close();
|
|
710
|
+
* ```
|
|
711
|
+
*/
|
|
712
|
+
declare class AlphaWebSocket {
|
|
713
|
+
private url;
|
|
714
|
+
private reconnectEnabled;
|
|
715
|
+
private maxReconnectAttempts;
|
|
716
|
+
private heartbeatIntervalMs;
|
|
717
|
+
private WebSocketImpl;
|
|
718
|
+
private ws;
|
|
719
|
+
private subscriptions;
|
|
720
|
+
private pendingRequests;
|
|
721
|
+
private lastOrderbookVersionBySubscription;
|
|
722
|
+
private heartbeatTimer;
|
|
723
|
+
private reconnectTimer;
|
|
724
|
+
private reconnectAttempts;
|
|
725
|
+
private intentionallyClosed;
|
|
726
|
+
private connectPromise;
|
|
727
|
+
constructor(config?: AlphaWebSocketConfig);
|
|
728
|
+
/** Whether the WebSocket is currently open and connected */
|
|
729
|
+
get connected(): boolean;
|
|
730
|
+
/**
|
|
731
|
+
* Subscribe to live market probability updates (incremental diffs).
|
|
732
|
+
* @returns An unsubscribe function
|
|
733
|
+
*/
|
|
734
|
+
subscribeLiveMarkets(callback: (event: MarketsChangedEvent) => void): () => void;
|
|
735
|
+
/**
|
|
736
|
+
* Subscribe to change events for a single market.
|
|
737
|
+
* @param slug - The market slug
|
|
738
|
+
* @returns An unsubscribe function
|
|
739
|
+
*/
|
|
740
|
+
subscribeMarket(slug: string, callback: (event: MarketChangedEvent) => void): () => void;
|
|
741
|
+
/**
|
|
742
|
+
* Subscribe to full orderbook snapshots (~5s interval on changes).
|
|
743
|
+
* @param slug - The market slug
|
|
744
|
+
* @returns An unsubscribe function
|
|
745
|
+
*/
|
|
746
|
+
subscribeOrderbook(slug: string, callback: (event: OrderbookChangedEvent) => void): () => void;
|
|
747
|
+
/**
|
|
748
|
+
* Subscribe to wallet order updates.
|
|
749
|
+
* @param wallet - The wallet address
|
|
750
|
+
* @returns An unsubscribe function
|
|
751
|
+
*/
|
|
752
|
+
subscribeWalletOrders(wallet: string, callback: (event: WalletOrdersChangedEvent) => void): () => void;
|
|
753
|
+
/** Query the server for the list of active subscriptions on this connection */
|
|
754
|
+
listSubscriptions(): Promise<unknown>;
|
|
755
|
+
/** Query a server property (e.g. "heartbeat", "limits") */
|
|
756
|
+
getProperty(property: string): Promise<unknown>;
|
|
757
|
+
/** Open the WebSocket connection. Called automatically on first subscribe. */
|
|
758
|
+
connect(): Promise<void>;
|
|
759
|
+
/** Close the connection and clean up all resources */
|
|
760
|
+
close(): void;
|
|
761
|
+
private buildStreamKey;
|
|
762
|
+
private buildQueryString;
|
|
763
|
+
private subscribe;
|
|
764
|
+
private doConnect;
|
|
765
|
+
private handleMessage;
|
|
766
|
+
private matchesSubscriptionMessage;
|
|
767
|
+
private shouldDispatchSubscriptionMessage;
|
|
768
|
+
private sendSubscribe;
|
|
769
|
+
private sendUnsubscribe;
|
|
770
|
+
private sendRequest;
|
|
771
|
+
private send;
|
|
772
|
+
private startHeartbeat;
|
|
773
|
+
private stopHeartbeat;
|
|
774
|
+
private scheduleReconnect;
|
|
775
|
+
private clearTimers;
|
|
776
|
+
}
|
|
777
|
+
|
|
564
778
|
/**
|
|
565
779
|
* Fetches all live, tradeable markets directly from the Algorand blockchain.
|
|
566
780
|
*
|
|
@@ -603,6 +817,7 @@ declare const getLiveMarketsFromApi: (config: AlphaClientConfig) => Promise<Mark
|
|
|
603
817
|
declare const getMarketFromApi: (config: AlphaClientConfig, marketId: string) => Promise<Market | null>;
|
|
604
818
|
|
|
605
819
|
declare const DEFAULT_API_BASE_URL = "https://platform.alphaarcade.com/api";
|
|
820
|
+
declare const DEFAULT_WSS_BASE_URL = "wss://wss.platform.alphaarcade.com";
|
|
606
821
|
declare const DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
|
|
607
822
|
|
|
608
823
|
/**
|
|
@@ -684,4 +899,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
|
|
|
684
899
|
*/
|
|
685
900
|
declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
|
|
686
901
|
|
|
687
|
-
export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, 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, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
|
|
902
|
+
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
|
@@ -147,6 +147,18 @@ type ProposeMatchParams = {
|
|
|
147
147
|
/** Quantity to match in microunits */
|
|
148
148
|
quantityMatched: number;
|
|
149
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* Parameters for matching two existing limit orders (e.g. after an amend).
|
|
152
|
+
* The taker is the order placed last and pays the fee; the maker is the counterparty.
|
|
153
|
+
*/
|
|
154
|
+
type ProcessMatchParams = {
|
|
155
|
+
/** Market app ID */
|
|
156
|
+
marketAppId: number;
|
|
157
|
+
/** Maker escrow app ID (existing order — was on the book first) */
|
|
158
|
+
makerEscrowAppId: number;
|
|
159
|
+
/** Taker escrow app ID (existing order — placed last, pays the fee) */
|
|
160
|
+
takerEscrowAppId: number;
|
|
161
|
+
};
|
|
150
162
|
/** Parameters for amending (editing) an existing unfilled order */
|
|
151
163
|
type AmendOrderParams = {
|
|
152
164
|
/** Market app ID */
|
|
@@ -202,6 +214,15 @@ type ProposeMatchResult = {
|
|
|
202
214
|
/** Confirmed round number */
|
|
203
215
|
confirmedRound: number;
|
|
204
216
|
};
|
|
217
|
+
/** Result of processing a match between two existing orders */
|
|
218
|
+
type ProcessMatchResult = {
|
|
219
|
+
/** Whether the match succeeded */
|
|
220
|
+
success: boolean;
|
|
221
|
+
/** Transaction IDs */
|
|
222
|
+
txIds: string[];
|
|
223
|
+
/** Confirmed round number */
|
|
224
|
+
confirmedRound: number;
|
|
225
|
+
};
|
|
205
226
|
/** Result of amending an order */
|
|
206
227
|
type AmendOrderResult = {
|
|
207
228
|
/** Whether the amendment succeeded */
|
|
@@ -210,6 +231,10 @@ type AmendOrderResult = {
|
|
|
210
231
|
txIds: string[];
|
|
211
232
|
/** Confirmed round number */
|
|
212
233
|
confirmedRound: number;
|
|
234
|
+
/** Total quantity matched against counterparty orders (if matching was performed) */
|
|
235
|
+
matchedQuantity?: number;
|
|
236
|
+
/** Volume-weighted average fill price in microunits (if any matches) */
|
|
237
|
+
matchedPrice?: number;
|
|
213
238
|
};
|
|
214
239
|
/** A single entry in the orderbook (one price level, one order) */
|
|
215
240
|
type OrderbookEntry = {
|
|
@@ -251,6 +276,11 @@ type AggregatedOrderbook = {
|
|
|
251
276
|
yes: AggregatedOrderbookSide;
|
|
252
277
|
no: AggregatedOrderbookSide;
|
|
253
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>;
|
|
254
284
|
/** Parameters for split shares */
|
|
255
285
|
type SplitSharesParams = {
|
|
256
286
|
/** Market app ID */
|
|
@@ -337,6 +367,78 @@ type EscrowGlobalState = {
|
|
|
337
367
|
asset_listed?: number;
|
|
338
368
|
fee_timer_start?: number;
|
|
339
369
|
};
|
|
370
|
+
/** Configuration for AlphaWebSocket */
|
|
371
|
+
type AlphaWebSocketConfig = {
|
|
372
|
+
/** WebSocket URL override (default: wss://wss.platform.alphaarcade.com) */
|
|
373
|
+
url?: string;
|
|
374
|
+
/** Enable auto-reconnect on unexpected disconnect (default: true) */
|
|
375
|
+
reconnect?: boolean;
|
|
376
|
+
/** Maximum reconnect attempts before giving up (default: Infinity) */
|
|
377
|
+
maxReconnectAttempts?: number;
|
|
378
|
+
/** Heartbeat interval in ms (default: 60000) */
|
|
379
|
+
heartbeatIntervalMs?: number;
|
|
380
|
+
/**
|
|
381
|
+
* WebSocket constructor to use. Defaults to the global `WebSocket`.
|
|
382
|
+
* On Node.js < 22, pass the `ws` package: `import WebSocket from 'ws'; new AlphaWebSocket({ WebSocket })`
|
|
383
|
+
*/
|
|
384
|
+
WebSocket?: unknown;
|
|
385
|
+
};
|
|
386
|
+
/** Orderbook bid/ask entry at the top level (decimal cents) */
|
|
387
|
+
type WsOrderbookAggregatedEntry = {
|
|
388
|
+
price: number;
|
|
389
|
+
quantity: number;
|
|
390
|
+
total: number;
|
|
391
|
+
};
|
|
392
|
+
/** Orderbook bid/ask entry with escrow details (raw microunit prices) */
|
|
393
|
+
type WsOrderbookDetailEntry = {
|
|
394
|
+
price: number;
|
|
395
|
+
quantity: number;
|
|
396
|
+
total: number;
|
|
397
|
+
escrowAppId: number;
|
|
398
|
+
owner: string;
|
|
399
|
+
};
|
|
400
|
+
/** Per-side orderbook detail (yes or no) */
|
|
401
|
+
type WsOrderbookDetailSide = {
|
|
402
|
+
bids: WsOrderbookDetailEntry[];
|
|
403
|
+
asks: WsOrderbookDetailEntry[];
|
|
404
|
+
};
|
|
405
|
+
/** Orderbook data for a single app within the orderbook payload */
|
|
406
|
+
type WsOrderbookApp = {
|
|
407
|
+
bids: WsOrderbookAggregatedEntry[];
|
|
408
|
+
asks: WsOrderbookAggregatedEntry[];
|
|
409
|
+
spread: number;
|
|
410
|
+
yes: WsOrderbookDetailSide;
|
|
411
|
+
no: WsOrderbookDetailSide;
|
|
412
|
+
};
|
|
413
|
+
/** Payload for orderbook_changed events */
|
|
414
|
+
type OrderbookChangedEvent = {
|
|
415
|
+
type: 'orderbook_changed';
|
|
416
|
+
ts: number;
|
|
417
|
+
marketId: string;
|
|
418
|
+
slug?: string;
|
|
419
|
+
version: number;
|
|
420
|
+
orderbook: FullOrderbookSnapshot;
|
|
421
|
+
};
|
|
422
|
+
/** Payload for markets_changed events (incremental diffs) */
|
|
423
|
+
type MarketsChangedEvent = {
|
|
424
|
+
type: 'markets_changed';
|
|
425
|
+
ts: number;
|
|
426
|
+
[key: string]: unknown;
|
|
427
|
+
};
|
|
428
|
+
/** Payload for market_changed events (single market) */
|
|
429
|
+
type MarketChangedEvent = {
|
|
430
|
+
type: 'market_changed';
|
|
431
|
+
ts: number;
|
|
432
|
+
[key: string]: unknown;
|
|
433
|
+
};
|
|
434
|
+
/** Payload for wallet_orders_changed events */
|
|
435
|
+
type WalletOrdersChangedEvent = {
|
|
436
|
+
type: 'wallet_orders_changed';
|
|
437
|
+
ts: number;
|
|
438
|
+
[key: string]: unknown;
|
|
439
|
+
};
|
|
440
|
+
/** Discriminated union of all WebSocket stream events */
|
|
441
|
+
type WebSocketStreamEvent = MarketsChangedEvent | MarketChangedEvent | OrderbookChangedEvent | WalletOrdersChangedEvent;
|
|
340
442
|
|
|
341
443
|
/**
|
|
342
444
|
* The main client for interacting with Alpha Market prediction markets on Algorand.
|
|
@@ -344,7 +446,7 @@ type EscrowGlobalState = {
|
|
|
344
446
|
* Provides methods for:
|
|
345
447
|
* - **Trading**: Create limit/market orders, cancel orders, propose matches
|
|
346
448
|
* - **Positions**: Split/merge shares, claim resolved tokens, view positions
|
|
347
|
-
* - **Orderbook**: Read on-chain
|
|
449
|
+
* - **Orderbook**: Read on-chain orderbooks or fetch full API-backed market snapshots
|
|
348
450
|
* - **Markets**: Fetch live markets from the Alpha API
|
|
349
451
|
*
|
|
350
452
|
* @example
|
|
@@ -423,6 +525,17 @@ declare class AlphaClient {
|
|
|
423
525
|
* @returns Whether the match succeeded
|
|
424
526
|
*/
|
|
425
527
|
proposeMatch(params: ProposeMatchParams): Promise<ProposeMatchResult>;
|
|
528
|
+
/**
|
|
529
|
+
* Matches two existing limit orders (no create-escrow in the group).
|
|
530
|
+
*
|
|
531
|
+
* Calls the market app's process_potential_match(maker, taker). Use this
|
|
532
|
+
* after amending an order: the amended order is the taker (pays the fee),
|
|
533
|
+
* the counterparty is the maker.
|
|
534
|
+
*
|
|
535
|
+
* @param params - Process match params (marketAppId, makerEscrowAppId, takerEscrowAppId)
|
|
536
|
+
* @returns Whether the match succeeded
|
|
537
|
+
*/
|
|
538
|
+
processMatch(params: ProcessMatchParams): Promise<ProcessMatchResult>;
|
|
426
539
|
/**
|
|
427
540
|
* Amends (edits) an existing unfilled order in-place.
|
|
428
541
|
*
|
|
@@ -487,6 +600,17 @@ declare class AlphaClient {
|
|
|
487
600
|
* @returns Orderbook with yes and no sides, each having bids and asks
|
|
488
601
|
*/
|
|
489
602
|
getOrderbook(marketAppId: number): Promise<Orderbook>;
|
|
603
|
+
/**
|
|
604
|
+
* Fetches the full processed orderbook snapshot for a market from the Alpha REST API.
|
|
605
|
+
*
|
|
606
|
+
* Returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by
|
|
607
|
+
* `marketAppId`, where each value includes aggregated bids/asks plus detailed yes/no orders.
|
|
608
|
+
* Requires `apiKey`.
|
|
609
|
+
*
|
|
610
|
+
* @param marketId - The Alpha market UUID
|
|
611
|
+
* @returns Full processed market orderbook keyed by marketAppId
|
|
612
|
+
*/
|
|
613
|
+
getFullOrderbookFromApi(marketId: string): Promise<FullOrderbookSnapshot>;
|
|
490
614
|
/**
|
|
491
615
|
* Gets open orders for a specific wallet on a market.
|
|
492
616
|
*
|
|
@@ -561,6 +685,96 @@ declare class AlphaClient {
|
|
|
561
685
|
getMarketFromApi(marketId: string): Promise<Market | null>;
|
|
562
686
|
}
|
|
563
687
|
|
|
688
|
+
/**
|
|
689
|
+
* Real-time WebSocket client for Alpha Market platform streams.
|
|
690
|
+
*
|
|
691
|
+
* Connects to `wss://wss.platform.alphaarcade.com` and provides typed,
|
|
692
|
+
* callback-based subscriptions for live market data. No auth required.
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* ```typescript
|
|
696
|
+
* // Node.js 22+ or browser (native WebSocket)
|
|
697
|
+
* const ws = new AlphaWebSocket();
|
|
698
|
+
*
|
|
699
|
+
* // Node.js < 22 — pass the `ws` package
|
|
700
|
+
* import WebSocket from 'ws';
|
|
701
|
+
* const ws = new AlphaWebSocket({ WebSocket });
|
|
702
|
+
*
|
|
703
|
+
* const unsub = ws.subscribeOrderbook('will-btc-hit-100k', (event) => {
|
|
704
|
+
* console.log('Orderbook:', event.orderbook);
|
|
705
|
+
* });
|
|
706
|
+
*
|
|
707
|
+
* // Later
|
|
708
|
+
* unsub();
|
|
709
|
+
* ws.close();
|
|
710
|
+
* ```
|
|
711
|
+
*/
|
|
712
|
+
declare class AlphaWebSocket {
|
|
713
|
+
private url;
|
|
714
|
+
private reconnectEnabled;
|
|
715
|
+
private maxReconnectAttempts;
|
|
716
|
+
private heartbeatIntervalMs;
|
|
717
|
+
private WebSocketImpl;
|
|
718
|
+
private ws;
|
|
719
|
+
private subscriptions;
|
|
720
|
+
private pendingRequests;
|
|
721
|
+
private lastOrderbookVersionBySubscription;
|
|
722
|
+
private heartbeatTimer;
|
|
723
|
+
private reconnectTimer;
|
|
724
|
+
private reconnectAttempts;
|
|
725
|
+
private intentionallyClosed;
|
|
726
|
+
private connectPromise;
|
|
727
|
+
constructor(config?: AlphaWebSocketConfig);
|
|
728
|
+
/** Whether the WebSocket is currently open and connected */
|
|
729
|
+
get connected(): boolean;
|
|
730
|
+
/**
|
|
731
|
+
* Subscribe to live market probability updates (incremental diffs).
|
|
732
|
+
* @returns An unsubscribe function
|
|
733
|
+
*/
|
|
734
|
+
subscribeLiveMarkets(callback: (event: MarketsChangedEvent) => void): () => void;
|
|
735
|
+
/**
|
|
736
|
+
* Subscribe to change events for a single market.
|
|
737
|
+
* @param slug - The market slug
|
|
738
|
+
* @returns An unsubscribe function
|
|
739
|
+
*/
|
|
740
|
+
subscribeMarket(slug: string, callback: (event: MarketChangedEvent) => void): () => void;
|
|
741
|
+
/**
|
|
742
|
+
* Subscribe to full orderbook snapshots (~5s interval on changes).
|
|
743
|
+
* @param slug - The market slug
|
|
744
|
+
* @returns An unsubscribe function
|
|
745
|
+
*/
|
|
746
|
+
subscribeOrderbook(slug: string, callback: (event: OrderbookChangedEvent) => void): () => void;
|
|
747
|
+
/**
|
|
748
|
+
* Subscribe to wallet order updates.
|
|
749
|
+
* @param wallet - The wallet address
|
|
750
|
+
* @returns An unsubscribe function
|
|
751
|
+
*/
|
|
752
|
+
subscribeWalletOrders(wallet: string, callback: (event: WalletOrdersChangedEvent) => void): () => void;
|
|
753
|
+
/** Query the server for the list of active subscriptions on this connection */
|
|
754
|
+
listSubscriptions(): Promise<unknown>;
|
|
755
|
+
/** Query a server property (e.g. "heartbeat", "limits") */
|
|
756
|
+
getProperty(property: string): Promise<unknown>;
|
|
757
|
+
/** Open the WebSocket connection. Called automatically on first subscribe. */
|
|
758
|
+
connect(): Promise<void>;
|
|
759
|
+
/** Close the connection and clean up all resources */
|
|
760
|
+
close(): void;
|
|
761
|
+
private buildStreamKey;
|
|
762
|
+
private buildQueryString;
|
|
763
|
+
private subscribe;
|
|
764
|
+
private doConnect;
|
|
765
|
+
private handleMessage;
|
|
766
|
+
private matchesSubscriptionMessage;
|
|
767
|
+
private shouldDispatchSubscriptionMessage;
|
|
768
|
+
private sendSubscribe;
|
|
769
|
+
private sendUnsubscribe;
|
|
770
|
+
private sendRequest;
|
|
771
|
+
private send;
|
|
772
|
+
private startHeartbeat;
|
|
773
|
+
private stopHeartbeat;
|
|
774
|
+
private scheduleReconnect;
|
|
775
|
+
private clearTimers;
|
|
776
|
+
}
|
|
777
|
+
|
|
564
778
|
/**
|
|
565
779
|
* Fetches all live, tradeable markets directly from the Algorand blockchain.
|
|
566
780
|
*
|
|
@@ -603,6 +817,7 @@ declare const getLiveMarketsFromApi: (config: AlphaClientConfig) => Promise<Mark
|
|
|
603
817
|
declare const getMarketFromApi: (config: AlphaClientConfig, marketId: string) => Promise<Market | null>;
|
|
604
818
|
|
|
605
819
|
declare const DEFAULT_API_BASE_URL = "https://platform.alphaarcade.com/api";
|
|
820
|
+
declare const DEFAULT_WSS_BASE_URL = "wss://wss.platform.alphaarcade.com";
|
|
606
821
|
declare const DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
|
|
607
822
|
|
|
608
823
|
/**
|
|
@@ -684,4 +899,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
|
|
|
684
899
|
*/
|
|
685
900
|
declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
|
|
686
901
|
|
|
687
|
-
export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, 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, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getLiveMarketsFromApi, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsOnChain };
|
|
902
|
+
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 };
|