@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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  TypeScript SDK for trading on **Alpha Market** — Algorand prediction markets.
4
4
 
5
- Place orders, manage positions, read orderbooks, and build automated trading bots — all directly on-chain.
5
+ Place orders, manage positions, read orderbooks from the API or chain, and build automated trading bots.
6
6
 
7
7
  ## Installation
8
8
 
@@ -246,7 +246,7 @@ for (const pos of positions) {
246
246
 
247
247
  #### `getOrderbook(marketAppId)`
248
248
 
249
- Fetches the full on-chain orderbook.
249
+ Fetches the full on-chain orderbook for a single market app.
250
250
 
251
251
  ```typescript
252
252
  const book = await client.getOrderbook(123456789);
@@ -263,6 +263,24 @@ if (book.yes.bids.length > 0) {
263
263
  }
264
264
  ```
265
265
 
266
+ #### `getFullOrderbookFromApi(marketId)`
267
+
268
+ Fetches the full processed orderbook snapshot for a market from the Alpha REST API. Requires `apiKey`.
269
+
270
+ This returns the same shape as websocket `orderbook_changed.orderbook`: a record keyed by `marketAppId`, where each value includes:
271
+ - top-level aggregated `bids`, `asks`, and `spread`
272
+ - detailed `yes` and `no` bid/ask orders with `escrowAppId` and `owner`
273
+
274
+ ```typescript
275
+ const snapshot = await client.getFullOrderbookFromApi('market-uuid-here');
276
+
277
+ for (const [appId, book] of Object.entries(snapshot)) {
278
+ console.log(`App ${appId}: spread=${book.spread}`);
279
+ console.log('Top-level bids:', book.bids);
280
+ console.log('Detailed YES bids:', book.yes.bids);
281
+ }
282
+ ```
283
+
266
284
  #### `getOpenOrders(marketAppId, walletAddress?)`
267
285
 
268
286
  Gets open orders for a wallet on a specific market (from on-chain data).
@@ -337,6 +355,150 @@ for (const m of rewardMarkets) {
337
355
 
338
356
  ---
339
357
 
358
+ ### WebSocket Streams
359
+
360
+ Real-time data streams via WebSocket. No API key or auth required. Replaces polling with push-based updates.
361
+
362
+ The SDK connects to the public platform websocket at `wss://wss.platform.alphaarcade.com`. The first
363
+ subscription is sent in the connection query string, and any later subscribe or unsubscribe calls use the
364
+ server's control-message envelope:
365
+
366
+ ```json
367
+ {
368
+ "id": "request-id",
369
+ "method": "SUBSCRIBE",
370
+ "params": [
371
+ { "stream": "get-orderbook", "slug": "will-btc-hit-100k" }
372
+ ]
373
+ }
374
+ ```
375
+
376
+ Supported public streams:
377
+
378
+ - `get-live-markets`
379
+ - `get-market` with `slug`
380
+ - `get-orderbook` with `slug`
381
+ - `get-wallet-orders` with `wallet`
382
+
383
+ ```typescript
384
+ import { AlphaWebSocket } from '@alpha-arcade/sdk';
385
+
386
+ // Node.js 22+ and browsers — native WebSocket, nothing extra needed
387
+ const ws = new AlphaWebSocket();
388
+
389
+ // Node.js < 22 — install `ws` and pass it in:
390
+ // npm install ws
391
+ import WebSocket from 'ws';
392
+ const ws = new AlphaWebSocket({ WebSocket });
393
+
394
+ // Subscribe to orderbook updates (~5s snapshots)
395
+ const unsub = ws.subscribeOrderbook('will-btc-hit-100k', (event) => {
396
+ console.log('Orderbook:', event.orderbook);
397
+ });
398
+
399
+ // Unsubscribe when done
400
+ unsub();
401
+
402
+ // Close the connection
403
+ ws.close();
404
+ ```
405
+
406
+ #### `subscribeLiveMarkets(callback)`
407
+
408
+ Receive incremental diffs whenever market probabilities change.
409
+
410
+ ```typescript
411
+ ws.subscribeLiveMarkets((event) => {
412
+ console.log('Markets changed at', event.ts, event);
413
+ });
414
+ ```
415
+
416
+ #### `subscribeMarket(slug, callback)`
417
+
418
+ Receive change events for a single market. Uses the market **slug** (not `marketAppId`) — see note on `subscribeOrderbook` below.
419
+
420
+ ```typescript
421
+ ws.subscribeMarket('will-btc-hit-100k', (event) => {
422
+ console.log('Market update:', event);
423
+ });
424
+ ```
425
+
426
+ #### `subscribeOrderbook(slug, callback)`
427
+
428
+ Receive full orderbook snapshots on every change (~5s interval). The payload matches `getFullOrderbookFromApi(marketId)`.
429
+
430
+ **Note:** The WebSocket API uses market **slugs** (URL-friendly names like `"will-btc-hit-100k"`), not `marketAppId` numbers. You can get a market's slug from the `slug` field on `Market` objects returned by `getLiveMarkets()` or `getMarket()`.
431
+
432
+ ```typescript
433
+ ws.subscribeOrderbook('will-btc-hit-100k', (event) => {
434
+ // Top-level bids/asks use decimal prices (cents)
435
+ // Nested yes/no use raw microunit prices with escrowAppId and owner
436
+ for (const [appId, book] of Object.entries(event.orderbook)) {
437
+ console.log(`App ${appId}: spread=${book.spread}`);
438
+ console.log(' Bids:', book.bids);
439
+ console.log(' Yes bids:', book.yes.bids);
440
+ }
441
+ });
442
+ ```
443
+
444
+ #### `subscribeWalletOrders(wallet, callback)`
445
+
446
+ Receive updates when orders for a wallet are created or modified.
447
+
448
+ ```typescript
449
+ ws.subscribeWalletOrders('MMU6X...', (event) => {
450
+ console.log('Wallet orders changed:', event);
451
+ });
452
+ ```
453
+
454
+ #### Unsubscribing
455
+
456
+ Each `subscribe*` method returns an unsubscribe function. Call it to stop receiving events for that stream:
457
+
458
+ ```typescript
459
+ const unsub = ws.subscribeOrderbook('my-market', (event) => { /* ... */ });
460
+
461
+ // Later, stop listening
462
+ unsub();
463
+ ```
464
+
465
+ #### Control Methods
466
+
467
+ ```typescript
468
+ // List active subscriptions on this connection
469
+ const subs = await ws.listSubscriptions();
470
+
471
+ // Query server properties (`heartbeat` or `limits`)
472
+ const props = await ws.getProperty('heartbeat');
473
+ ```
474
+
475
+ #### Configuration
476
+
477
+ ```typescript
478
+ import WebSocket from 'ws'; // Only needed on Node.js < 22
479
+
480
+ const ws = new AlphaWebSocket({
481
+ WebSocket, // Pass `ws` on Node.js < 22 (not needed in browsers or Node 22+)
482
+ url: 'wss://custom-endpoint.example.com', // Override default URL
483
+ reconnect: true, // Auto-reconnect (default: true)
484
+ maxReconnectAttempts: 10, // Give up after 10 retries (default: Infinity)
485
+ heartbeatIntervalMs: 60_000, // Ping interval in ms (default: 60000)
486
+ });
487
+ ```
488
+
489
+ #### Connection Details
490
+
491
+ | Setting | Value |
492
+ |---------|-------|
493
+ | Heartbeat | 60s (auto-handled) |
494
+ | Idle timeout | 180s |
495
+ | Rate limit | 5 messages/sec/connection |
496
+ | Reconnect | Exponential backoff (1s → 30s max) |
497
+
498
+ The client automatically responds to server pings, sends keepalive pings, and reconnects with exponential backoff on unexpected disconnects. All active subscriptions are restored after reconnect.
499
+
500
+ ---
501
+
340
502
  ### Utility Functions
341
503
 
342
504
  These are exported for advanced users: