@catalyst-team/poly-sdk 0.4.6 → 0.4.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 +18 -9
- package/README.zh-CN.md +18 -9
- package/dist/src/clients/data-api.d.ts +25 -0
- package/dist/src/clients/data-api.d.ts.map +1 -1
- package/dist/src/clients/data-api.js +57 -0
- package/dist/src/clients/data-api.js.map +1 -1
- package/dist/src/core/types.d.ts +55 -0
- package/dist/src/core/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +6 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +4 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/realtime/index.d.ts +18 -0
- package/dist/src/realtime/index.d.ts.map +1 -0
- package/dist/src/realtime/index.js +14 -0
- package/dist/src/realtime/index.js.map +1 -0
- package/dist/src/realtime/realtime-data-client.d.ts +274 -0
- package/dist/src/realtime/realtime-data-client.d.ts.map +1 -0
- package/dist/src/realtime/realtime-data-client.js +771 -0
- package/dist/src/realtime/realtime-data-client.js.map +1 -0
- package/dist/src/realtime/types.d.ts +485 -0
- package/dist/src/realtime/types.d.ts.map +1 -0
- package/dist/src/realtime/types.js +36 -0
- package/dist/src/realtime/types.js.map +1 -0
- package/dist/src/services/arbitrage-service.d.ts.map +1 -1
- package/dist/src/services/arbitrage-service.js +2 -1
- package/dist/src/services/arbitrage-service.js.map +1 -1
- package/dist/src/services/dip-arb-service.d.ts.map +1 -1
- package/dist/src/services/dip-arb-service.js +3 -19
- package/dist/src/services/dip-arb-service.js.map +1 -1
- package/dist/src/services/market-service.d.ts +93 -11
- package/dist/src/services/market-service.d.ts.map +1 -1
- package/dist/src/services/market-service.js +189 -22
- package/dist/src/services/market-service.js.map +1 -1
- package/dist/src/services/order-handle.test.d.ts +15 -0
- package/dist/src/services/order-handle.test.d.ts.map +1 -0
- package/dist/src/services/order-handle.test.js +333 -0
- package/dist/src/services/order-handle.test.js.map +1 -0
- package/dist/src/services/order-manager.d.ts +162 -6
- package/dist/src/services/order-manager.d.ts.map +1 -1
- package/dist/src/services/order-manager.js +419 -30
- package/dist/src/services/order-manager.js.map +1 -1
- package/dist/src/services/realtime-service-v2.d.ts +122 -6
- package/dist/src/services/realtime-service-v2.d.ts.map +1 -1
- package/dist/src/services/realtime-service-v2.js +475 -70
- package/dist/src/services/realtime-service-v2.js.map +1 -1
- package/dist/src/services/trading-service.d.ts +129 -1
- package/dist/src/services/trading-service.d.ts.map +1 -1
- package/dist/src/services/trading-service.js +198 -5
- package/dist/src/services/trading-service.js.map +1 -1
- package/package.json +1 -2
- package/dist/src/services/ctf-detector.d.ts +0 -215
- package/dist/src/services/ctf-detector.d.ts.map +0 -1
- package/dist/src/services/ctf-detector.js +0 -420
- package/dist/src/services/ctf-detector.js.map +0 -1
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RealTimeDataClient
|
|
3
|
+
*
|
|
4
|
+
* Custom WebSocket client for Polymarket real-time data.
|
|
5
|
+
* Replaces @polymarket/real-time-data-client with proper:
|
|
6
|
+
* - RFC 6455 ping/pong mechanism
|
|
7
|
+
* - Exponential backoff reconnection
|
|
8
|
+
* - Full subscription management
|
|
9
|
+
*
|
|
10
|
+
* WebSocket Protocol (from official docs):
|
|
11
|
+
* - URL: wss://ws-subscriptions-clob.polymarket.com/ws/market
|
|
12
|
+
* - Initial subscription: { type: "MARKET", assets_ids: ["token_id_1", ...] }
|
|
13
|
+
* - Dynamic subscribe: { operation: "subscribe", assets_ids: ["token_id_1"] }
|
|
14
|
+
* - Dynamic unsubscribe: { operation: "unsubscribe", assets_ids: ["token_id_1"] }
|
|
15
|
+
*
|
|
16
|
+
* Event types: book, price_change, last_trade_price, tick_size_change, best_bid_ask
|
|
17
|
+
*
|
|
18
|
+
* @see https://docs.polymarket.com/developers/CLOB/websocket/wss-overview
|
|
19
|
+
* @see https://docs.polymarket.com/developers/CLOB/websocket/market-channel
|
|
20
|
+
*/
|
|
21
|
+
import { type RealTimeDataClientConfig, type RealTimeDataClientInterface, type SubscriptionMessage, type ClobApiKeyCreds, ConnectionStatus } from './types.js';
|
|
22
|
+
export declare class RealTimeDataClient implements RealTimeDataClientInterface {
|
|
23
|
+
private ws;
|
|
24
|
+
private config;
|
|
25
|
+
private status;
|
|
26
|
+
private reconnectAttempts;
|
|
27
|
+
private reconnectTimer;
|
|
28
|
+
private pingTimer;
|
|
29
|
+
private pongTimer;
|
|
30
|
+
private pongReceived;
|
|
31
|
+
private intentionalDisconnect;
|
|
32
|
+
constructor(config?: RealTimeDataClientConfig);
|
|
33
|
+
/**
|
|
34
|
+
* Connect to the WebSocket server
|
|
35
|
+
*/
|
|
36
|
+
connect(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Disconnect from the WebSocket server
|
|
39
|
+
*/
|
|
40
|
+
disconnect(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Subscribe to market data
|
|
43
|
+
*
|
|
44
|
+
* For initial subscription (when first connecting or adding new tokens):
|
|
45
|
+
* { type: "MARKET", assets_ids: ["token_id_1", "token_id_2"] }
|
|
46
|
+
*
|
|
47
|
+
* For adding tokens to existing subscription:
|
|
48
|
+
* { operation: "subscribe", assets_ids: ["token_id_3"] }
|
|
49
|
+
*
|
|
50
|
+
* @deprecated Use subscribeMarket() for the new API format
|
|
51
|
+
*/
|
|
52
|
+
subscribe(msg: {
|
|
53
|
+
subscriptions: SubscriptionMessage[];
|
|
54
|
+
}): void;
|
|
55
|
+
/**
|
|
56
|
+
* Unsubscribe from market data
|
|
57
|
+
*
|
|
58
|
+
* Format: { operation: "unsubscribe", assets_ids: ["token_id_1"] }
|
|
59
|
+
*
|
|
60
|
+
* @deprecated Use unsubscribeMarket() for the new API format
|
|
61
|
+
*/
|
|
62
|
+
unsubscribe(msg: {
|
|
63
|
+
subscriptions: SubscriptionMessage[];
|
|
64
|
+
}): void;
|
|
65
|
+
/**
|
|
66
|
+
* Subscribe to market data (new API)
|
|
67
|
+
*
|
|
68
|
+
* @param assetsIds - Array of token IDs to subscribe to
|
|
69
|
+
* @param isInitial - If true, sends initial subscription format { type: "MARKET", ... }
|
|
70
|
+
* If false, sends dynamic format { operation: "subscribe", ... }
|
|
71
|
+
*/
|
|
72
|
+
subscribeMarket(assetsIds: string[], isInitial?: boolean): void;
|
|
73
|
+
/**
|
|
74
|
+
* Unsubscribe from market data (new API)
|
|
75
|
+
*
|
|
76
|
+
* @param assetsIds - Array of token IDs to unsubscribe from
|
|
77
|
+
*/
|
|
78
|
+
unsubscribeMarket(assetsIds: string[]): void;
|
|
79
|
+
/**
|
|
80
|
+
* Subscribe to user channel (requires authentication)
|
|
81
|
+
*
|
|
82
|
+
* User channel provides personal order and trade events.
|
|
83
|
+
* Note: This requires connecting to the USER WebSocket endpoint.
|
|
84
|
+
*
|
|
85
|
+
* Format: { type: "USER", auth: { apiKey, secret, passphrase }, markets?: [...] }
|
|
86
|
+
*
|
|
87
|
+
* @param auth - CLOB API credentials
|
|
88
|
+
* @param markets - Optional array of condition IDs to filter events
|
|
89
|
+
*/
|
|
90
|
+
subscribeUser(auth: ClobApiKeyCreds, markets?: string[]): void;
|
|
91
|
+
/**
|
|
92
|
+
* Subscribe to Binance crypto prices
|
|
93
|
+
*
|
|
94
|
+
* Provides real-time price updates from Binance exchange.
|
|
95
|
+
*
|
|
96
|
+
* **Usage:**
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const client = new RealTimeDataClient({ url: WS_ENDPOINTS.LIVE_DATA });
|
|
99
|
+
* client.connect();
|
|
100
|
+
* client.subscribeCryptoPrices(['btcusdt', 'ethusdt', 'solusdt', 'xrpusdt']);
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* **Symbol Format:** Lowercase concatenated pairs (e.g., 'btcusdt', 'ethusdt', 'solusdt', 'xrpusdt')
|
|
104
|
+
*
|
|
105
|
+
* **Subscription Message Format:**
|
|
106
|
+
* ```json
|
|
107
|
+
* {
|
|
108
|
+
* "action": "subscribe",
|
|
109
|
+
* "subscriptions": [{
|
|
110
|
+
* "topic": "crypto_prices",
|
|
111
|
+
* "type": "*",
|
|
112
|
+
* "filters": "{\"symbol\":\"btcusdt\"}"
|
|
113
|
+
* }]
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* **Response Message Format:**
|
|
118
|
+
* ```json
|
|
119
|
+
* {
|
|
120
|
+
* "topic": "crypto_prices",
|
|
121
|
+
* "type": "update",
|
|
122
|
+
* "timestamp": 1769846473135,
|
|
123
|
+
* "payload": {
|
|
124
|
+
* "symbol": "ethusdt",
|
|
125
|
+
* "timestamp": 1769846473000,
|
|
126
|
+
* "value": 2681.42,
|
|
127
|
+
* "full_accuracy_value": "2681.42000000"
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* **Important Notes:**
|
|
133
|
+
* - Each symbol requires a separate subscription message
|
|
134
|
+
* - Use `type: "*"` (not `"update"`)
|
|
135
|
+
* - Filters must be JSON-stringified: `JSON.stringify({ symbol: "btcusdt" })`
|
|
136
|
+
* - This implementation matches the official @polymarket/real-time-data-client
|
|
137
|
+
*
|
|
138
|
+
* @param symbols - Array of Binance symbols in lowercase (e.g., ['btcusdt', 'ethusdt'])
|
|
139
|
+
* @see https://docs.polymarket.com/developers/RTDS/RTDS-crypto-prices
|
|
140
|
+
* @see https://github.com/Polymarket/real-time-data-client
|
|
141
|
+
*/
|
|
142
|
+
subscribeCryptoPrices(symbols: string[]): void;
|
|
143
|
+
/**
|
|
144
|
+
* Unsubscribe from Binance crypto prices
|
|
145
|
+
*
|
|
146
|
+
* @param symbols - Array of Binance symbols to unsubscribe from
|
|
147
|
+
*/
|
|
148
|
+
unsubscribeCryptoPrices(symbols: string[]): void;
|
|
149
|
+
/**
|
|
150
|
+
* Subscribe to Chainlink crypto prices
|
|
151
|
+
*
|
|
152
|
+
* Provides real-time price updates from Chainlink oracles (official settlement price source).
|
|
153
|
+
*
|
|
154
|
+
* **Usage:**
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const client = new RealTimeDataClient({ url: WS_ENDPOINTS.LIVE_DATA });
|
|
157
|
+
* client.connect();
|
|
158
|
+
* client.subscribeCryptoChainlinkPrices(['btc/usd', 'eth/usd', 'sol/usd', 'xrp/usd']);
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* **Symbol Format:** Lowercase slash-separated pairs (e.g., 'btc/usd', 'eth/usd', 'sol/usd', 'xrp/usd')
|
|
162
|
+
*
|
|
163
|
+
* **Subscription Message Format:**
|
|
164
|
+
* ```json
|
|
165
|
+
* {
|
|
166
|
+
* "action": "subscribe",
|
|
167
|
+
* "subscriptions": [{
|
|
168
|
+
* "topic": "crypto_prices_chainlink",
|
|
169
|
+
* "type": "*",
|
|
170
|
+
* "filters": "{\"symbol\":\"btc/usd\"}"
|
|
171
|
+
* }]
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* **Response Message Format:**
|
|
176
|
+
* ```json
|
|
177
|
+
* {
|
|
178
|
+
* "topic": "crypto_prices_chainlink",
|
|
179
|
+
* "type": "update",
|
|
180
|
+
* "timestamp": 1769833333076,
|
|
181
|
+
* "payload": {
|
|
182
|
+
* "symbol": "btc/usd",
|
|
183
|
+
* "timestamp": 1769833332000,
|
|
184
|
+
* "value": 83915.04025109926,
|
|
185
|
+
* "full_accuracy_value": "83915040251099250000000"
|
|
186
|
+
* }
|
|
187
|
+
* }
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* **Important Notes:**
|
|
191
|
+
* - Each symbol requires a separate subscription message
|
|
192
|
+
* - Use `type: "*"` for all message types
|
|
193
|
+
* - Filters must be JSON-stringified: `JSON.stringify({ symbol: "btc/usd" })`
|
|
194
|
+
* - Chainlink prices are used as the official settlement source for 15m crypto markets
|
|
195
|
+
* - Higher precision available via `full_accuracy_value` field
|
|
196
|
+
*
|
|
197
|
+
* @param symbols - Array of Chainlink symbols in lowercase (e.g., ['btc/usd', 'eth/usd'])
|
|
198
|
+
* @see https://docs.polymarket.com/developers/RTDS/RTDS-crypto-prices
|
|
199
|
+
* @see https://data.chain.link/streams - Official Chainlink data feeds
|
|
200
|
+
*/
|
|
201
|
+
subscribeCryptoChainlinkPrices(symbols: string[]): void;
|
|
202
|
+
/**
|
|
203
|
+
* Unsubscribe from Chainlink crypto prices
|
|
204
|
+
*
|
|
205
|
+
* @param symbols - Array of Chainlink symbols to unsubscribe from
|
|
206
|
+
*/
|
|
207
|
+
unsubscribeCryptoChainlinkPrices(symbols: string[]): void;
|
|
208
|
+
/**
|
|
209
|
+
* Check if connected
|
|
210
|
+
*/
|
|
211
|
+
isConnected(): boolean;
|
|
212
|
+
/**
|
|
213
|
+
* Get current connection status
|
|
214
|
+
*/
|
|
215
|
+
getStatus(): ConnectionStatus;
|
|
216
|
+
private send;
|
|
217
|
+
private handleOpen;
|
|
218
|
+
private handleMessage;
|
|
219
|
+
/**
|
|
220
|
+
* Parse incoming WebSocket messages into our Message format
|
|
221
|
+
*
|
|
222
|
+
* ## Market Channel Events (topic: 'clob_market')
|
|
223
|
+
* @see https://docs.polymarket.com/developers/CLOB/websocket/market-channel
|
|
224
|
+
*
|
|
225
|
+
* | Event Type | Format | Trigger |
|
|
226
|
+
* |-------------------|-----------------------------------------------------------|----------------------------------|
|
|
227
|
+
* | book | Array: [{ market, asset_id, bids, asks, timestamp, hash }]| Initial subscribe or trade |
|
|
228
|
+
* | price_change | { market, price_changes: [{ asset_id, price, size, ... }]}| Order placed or cancelled |
|
|
229
|
+
* | last_trade_price | { market, asset_id, price, side, size, fee_rate_bps, ... }| Trade execution |
|
|
230
|
+
* | tick_size_change | { market, asset_id, old_tick_size, new_tick_size, ... } | Price > 0.96 or < 0.04 |
|
|
231
|
+
* | best_bid_ask | { market, asset_id, best_bid, best_ask, spread, ... } | Best price change (feature-flag) |
|
|
232
|
+
* | new_market | { id, question, market, slug, assets_ids, outcomes, ... } | Market creation (feature-flag) |
|
|
233
|
+
* | market_resolved | { ..., winning_asset_id, winning_outcome } | Market resolved (feature-flag) |
|
|
234
|
+
*
|
|
235
|
+
* ## User Channel Events (topic: 'clob_user')
|
|
236
|
+
* @see https://docs.polymarket.com/developers/CLOB/websocket/user-channel
|
|
237
|
+
*
|
|
238
|
+
* | Event Type | Format | Trigger |
|
|
239
|
+
* |------------|-----------------------------------------------------------------|--------------------------------|
|
|
240
|
+
* | trade | { event_type: 'trade', status, side, price, size, maker_orders }| Order matched/mined/confirmed |
|
|
241
|
+
* | order | { event_type: 'order', type, side, price, original_size, ... } | Order placed/updated/cancelled |
|
|
242
|
+
*/
|
|
243
|
+
private parseMessages;
|
|
244
|
+
/**
|
|
245
|
+
* Normalize timestamp to milliseconds
|
|
246
|
+
* Polymarket sends timestamps in seconds, need to convert to milliseconds
|
|
247
|
+
*/
|
|
248
|
+
private normalizeTimestamp;
|
|
249
|
+
private handleClose;
|
|
250
|
+
private handleError;
|
|
251
|
+
private handlePong;
|
|
252
|
+
/**
|
|
253
|
+
* Start periodic ping to keep connection alive
|
|
254
|
+
*
|
|
255
|
+
* Uses RFC 6455 WebSocket ping frames, which the server MUST respond to
|
|
256
|
+
* with pong frames. If no pong is received within pongTimeout, we
|
|
257
|
+
* consider the connection dead and reconnect.
|
|
258
|
+
*/
|
|
259
|
+
private startPing;
|
|
260
|
+
private stopPing;
|
|
261
|
+
private setPongTimeout;
|
|
262
|
+
private clearPongTimeout;
|
|
263
|
+
private handleDeadConnection;
|
|
264
|
+
/**
|
|
265
|
+
* Handle reconnection with exponential backoff
|
|
266
|
+
* Delays: 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s, 512s (capped at maxReconnectAttempts)
|
|
267
|
+
*/
|
|
268
|
+
private handleReconnect;
|
|
269
|
+
private cancelReconnect;
|
|
270
|
+
private cleanup;
|
|
271
|
+
private setStatus;
|
|
272
|
+
private log;
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=realtime-data-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime-data-client.d.ts","sourceRoot":"","sources":["../../../src/realtime/realtime-data-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,2BAA2B,EAChC,KAAK,mBAAmB,EACxB,KAAK,eAAe,EAEpB,gBAAgB,EAEjB,MAAM,YAAY,CAAC;AASpB,qBAAa,kBAAmB,YAAW,2BAA2B;IACpE,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,MAAM,CAIZ;IAEF,OAAO,CAAC,MAAM,CAAmD;IACjE,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,cAAc,CAA8C;IACpE,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,SAAS,CAA8C;IAC/D,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,qBAAqB,CAAS;gBAE1B,MAAM,GAAE,wBAA6B;IAyBjD;;OAEG;IACH,OAAO,IAAI,IAAI;IAmBf;;OAEG;IACH,UAAU,IAAI,IAAI;IAalB;;;;;;;;;;OAUG;IACH,SAAS,CAAC,GAAG,EAAE;QAAE,aAAa,EAAE,mBAAmB,EAAE,CAAA;KAAE,GAAG,IAAI;IAqB9D;;;;;;OAMG;IACH,WAAW,CAAC,GAAG,EAAE;QAAE,aAAa,EAAE,mBAAmB,EAAE,CAAA;KAAE,GAAG,IAAI;IAqBhE;;;;;;OAMG;IACH,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,SAAS,UAAO,GAAG,IAAI;IAkB5D;;;;OAIG;IACH,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI;IAQ5C;;;;;;;;;;OAUG;IACH,aAAa,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI;IAa9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkDG;IACH,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAmB9C;;;;OAIG;IACH,uBAAuB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAmBhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmDG;IACH,8BAA8B,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAmBvD;;;;OAIG;IACH,gCAAgC,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAmBzD;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,SAAS,IAAI,gBAAgB;IAQ7B,OAAO,CAAC,IAAI;IASZ,OAAO,CAAC,UAAU;IASlB,OAAO,CAAC,aAAa;IAiBrB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,OAAO,CAAC,aAAa;IA2KrB;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,WAAW;IAgBnB,OAAO,CAAC,WAAW;IAKnB,OAAO,CAAC,UAAU;IAUlB;;;;;;OAMG;IACH,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,QAAQ;IAQhB,OAAO,CAAC,cAAc;IAUtB,OAAO,CAAC,gBAAgB;IAOxB,OAAO,CAAC,oBAAoB;IAoB5B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAuBvB,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,SAAS;IAQjB,OAAO,CAAC,GAAG;CAKZ"}
|