@liberfi.io/react-predict 0.1.1 → 0.1.3
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.mts +19 -3
- package/dist/index.d.ts +19 -3
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -1
- package/dist/server.d.mts +997 -2
- package/dist/server.d.ts +997 -2
- package/dist/server.js +1 -4
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +1 -4
- package/dist/server.mjs.map +1 -1
- package/package.json +3 -3
- package/dist/server-DlSG7h_v.d.mts +0 -1008
- package/dist/server-DlSG7h_v.d.ts +0 -1008
package/dist/server.d.mts
CHANGED
|
@@ -1,2 +1,997 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Domain types for the prediction-server REST API.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors: github.com/liberfi-io/prediction-server/internal/domain
|
|
5
|
+
*/
|
|
6
|
+
/** Upstream data provider that produced a domain object. */
|
|
7
|
+
type ProviderSource = "dflow" | "polymarket";
|
|
8
|
+
/**
|
|
9
|
+
* Provider-specific metadata attached to every domain aggregate.
|
|
10
|
+
* Keys are namespaced field paths, e.g. "polymarket.conditionId" or "dflow.yesMint".
|
|
11
|
+
* Values are raw JSON-decoded values.
|
|
12
|
+
*/
|
|
13
|
+
type ProviderMeta = Record<string, unknown>;
|
|
14
|
+
/** Provider-sourced label attached to an Event. */
|
|
15
|
+
interface PredictTag {
|
|
16
|
+
slug: string;
|
|
17
|
+
label: string;
|
|
18
|
+
source: ProviderSource;
|
|
19
|
+
provider_meta?: ProviderMeta;
|
|
20
|
+
}
|
|
21
|
+
/** Lifecycle status of a prediction event (normalised across all providers). */
|
|
22
|
+
type EventStatus = "pending" | "open" | "closed" | "voided";
|
|
23
|
+
/** Settlement / resolution data source for an event. */
|
|
24
|
+
interface SettlementSource {
|
|
25
|
+
url: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
}
|
|
28
|
+
/** Root aggregate for a prediction event from the prediction-server API. */
|
|
29
|
+
interface PredictEvent {
|
|
30
|
+
/** Internal database surrogate key (absent when not yet persisted). */
|
|
31
|
+
id?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Canonical business key shared across all providers.
|
|
34
|
+
* - DFlow: ticker (e.g. "KXBTCD-25FEB-T68000")
|
|
35
|
+
* - Polymarket: slug (e.g. "will-trump-win-2024")
|
|
36
|
+
*/
|
|
37
|
+
slug: string;
|
|
38
|
+
title: string;
|
|
39
|
+
subtitle?: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
image_url?: string;
|
|
42
|
+
status: EventStatus;
|
|
43
|
+
/** ISO 8601 timestamp; absent if the provider does not supply it. */
|
|
44
|
+
start_at?: string;
|
|
45
|
+
end_at?: string;
|
|
46
|
+
created_at?: string;
|
|
47
|
+
updated_at?: string;
|
|
48
|
+
/** Provider-sourced labels for display only. */
|
|
49
|
+
tags?: PredictTag[];
|
|
50
|
+
/** All values are USD amounts. */
|
|
51
|
+
volume?: number;
|
|
52
|
+
volume_24h?: number;
|
|
53
|
+
liquidity?: number;
|
|
54
|
+
open_interest?: number;
|
|
55
|
+
settlement_sources?: SettlementSource[];
|
|
56
|
+
/** Nested markets; omitted when the request used `with_markets=false`. */
|
|
57
|
+
markets?: PredictMarket[];
|
|
58
|
+
source: ProviderSource;
|
|
59
|
+
provider_meta?: ProviderMeta;
|
|
60
|
+
}
|
|
61
|
+
/** Lifecycle status of a prediction market (normalised across all providers). */
|
|
62
|
+
type MarketStatus = "pending" | "open" | "closed" | "voided";
|
|
63
|
+
/** Final resolution of a closed market. Empty string means unresolved. */
|
|
64
|
+
type MarketResult = "yes" | "no" | "voided" | "";
|
|
65
|
+
/** One possible outcome in a binary (or multi-outcome) prediction market. */
|
|
66
|
+
interface MarketOutcome {
|
|
67
|
+
/** Display name, e.g. "Yes" or "No". */
|
|
68
|
+
label: string;
|
|
69
|
+
/** Current implied probability [0, 1]. */
|
|
70
|
+
price?: number;
|
|
71
|
+
best_bid?: number;
|
|
72
|
+
best_ask?: number;
|
|
73
|
+
}
|
|
74
|
+
/** Tradeable prediction outcome within an Event. */
|
|
75
|
+
interface PredictMarket {
|
|
76
|
+
id?: number;
|
|
77
|
+
event_id?: number;
|
|
78
|
+
slug: string;
|
|
79
|
+
event_slug: string;
|
|
80
|
+
question: string;
|
|
81
|
+
description?: string;
|
|
82
|
+
image_url?: string;
|
|
83
|
+
/** Resolution/settlement rules in order. */
|
|
84
|
+
rules?: string[];
|
|
85
|
+
status: MarketStatus;
|
|
86
|
+
result?: MarketResult;
|
|
87
|
+
start_at?: string;
|
|
88
|
+
end_at?: string;
|
|
89
|
+
expires_at?: string;
|
|
90
|
+
closed_at?: string;
|
|
91
|
+
created_at?: string;
|
|
92
|
+
updated_at?: string;
|
|
93
|
+
/** Always present; binary markets have exactly 2 outcomes (YES at [0], NO at [1]). */
|
|
94
|
+
outcomes: MarketOutcome[];
|
|
95
|
+
volume?: number;
|
|
96
|
+
volume_24h?: number;
|
|
97
|
+
liquidity?: number;
|
|
98
|
+
open_interest?: number;
|
|
99
|
+
source: ProviderSource;
|
|
100
|
+
provider_meta?: ProviderMeta;
|
|
101
|
+
}
|
|
102
|
+
/** Generic paginated result set returned by the prediction-server list endpoints. */
|
|
103
|
+
interface PredictPage<T> {
|
|
104
|
+
items: T[];
|
|
105
|
+
next_cursor?: string;
|
|
106
|
+
has_more?: boolean;
|
|
107
|
+
limit?: number;
|
|
108
|
+
/** Not all backends support total count. */
|
|
109
|
+
total?: number;
|
|
110
|
+
}
|
|
111
|
+
/** Valid sort fields accepted by `GET /api/v1/events`. */
|
|
112
|
+
type EventSortField = "volume" | "volume_24h" | "liquidity" | "open_interest" | "end_at" | "start_at" | "created_at";
|
|
113
|
+
/** Query parameters for `listEvents`. All fields are optional. */
|
|
114
|
+
interface ListEventsParams {
|
|
115
|
+
/** Page size. Server default: 20. */
|
|
116
|
+
limit?: number;
|
|
117
|
+
/** Opaque pagination cursor returned by the previous page's `next_cursor`. */
|
|
118
|
+
cursor?: string;
|
|
119
|
+
/** Filter by event lifecycle status. */
|
|
120
|
+
status?: EventStatus;
|
|
121
|
+
/** Filter by upstream provider. */
|
|
122
|
+
source?: ProviderSource;
|
|
123
|
+
/** Unified navigation tag slug (e.g. "politics", "sports"). */
|
|
124
|
+
tag_slug?: string;
|
|
125
|
+
/** Full-text search query. */
|
|
126
|
+
search?: string;
|
|
127
|
+
/** Field to sort by. */
|
|
128
|
+
sort_by?: EventSortField;
|
|
129
|
+
/** When `true`, sort ascending. Defaults to descending. */
|
|
130
|
+
sort_asc?: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* When `false`, markets are omitted from each event for lighter payloads.
|
|
133
|
+
* Defaults to `true` on the server.
|
|
134
|
+
*/
|
|
135
|
+
with_markets?: boolean;
|
|
136
|
+
}
|
|
137
|
+
/** Single price level in an order book. */
|
|
138
|
+
interface OrderbookLevel {
|
|
139
|
+
price: number;
|
|
140
|
+
size: number;
|
|
141
|
+
}
|
|
142
|
+
/** Aggregated order book for a market. */
|
|
143
|
+
interface Orderbook {
|
|
144
|
+
market_id: string;
|
|
145
|
+
/** Sorted by price descending. */
|
|
146
|
+
bids: OrderbookLevel[];
|
|
147
|
+
/** Sorted by price ascending. */
|
|
148
|
+
asks: OrderbookLevel[];
|
|
149
|
+
/** best_ask − best_bid; absent when either side is empty. */
|
|
150
|
+
spread?: number;
|
|
151
|
+
}
|
|
152
|
+
/** On-chain trade type. */
|
|
153
|
+
type TradeType = "TRADE" | "SPLIT" | "MERGE" | "REDEEM" | "REWARD" | "CONVERSION" | "MAKER_REBATE" | "REFERRAL_REWARD";
|
|
154
|
+
/** Lightweight event summary embedded in a trade. */
|
|
155
|
+
interface EventSummary {
|
|
156
|
+
slug: string;
|
|
157
|
+
title: string;
|
|
158
|
+
image_url?: string;
|
|
159
|
+
status: EventStatus;
|
|
160
|
+
}
|
|
161
|
+
/** Lightweight market summary embedded in a trade. */
|
|
162
|
+
interface MarketSummary {
|
|
163
|
+
slug: string;
|
|
164
|
+
question: string;
|
|
165
|
+
status: MarketStatus;
|
|
166
|
+
}
|
|
167
|
+
/** A single trade record. */
|
|
168
|
+
interface PredictTrade {
|
|
169
|
+
id: string;
|
|
170
|
+
source: ProviderSource;
|
|
171
|
+
side?: string;
|
|
172
|
+
outcome?: string;
|
|
173
|
+
outcome_index?: number;
|
|
174
|
+
price?: number;
|
|
175
|
+
size: number;
|
|
176
|
+
usd_size?: number;
|
|
177
|
+
/** Unix seconds. */
|
|
178
|
+
timestamp: number;
|
|
179
|
+
tx_hash?: string;
|
|
180
|
+
wallet?: string;
|
|
181
|
+
type: TradeType;
|
|
182
|
+
event?: EventSummary;
|
|
183
|
+
market?: MarketSummary;
|
|
184
|
+
provider_meta?: ProviderMeta;
|
|
185
|
+
}
|
|
186
|
+
/** Query parameters for `GET /api/v1/markets/{slug}/trades`. */
|
|
187
|
+
interface ListMarketTradesParams {
|
|
188
|
+
source: ProviderSource;
|
|
189
|
+
limit?: number;
|
|
190
|
+
cursor?: string;
|
|
191
|
+
/** Filter by side: `"BUY"` or `"SELL"`. */
|
|
192
|
+
side?: string;
|
|
193
|
+
}
|
|
194
|
+
/** Query parameters for `GET /api/v1/trades` (by wallet). */
|
|
195
|
+
interface ListTradesParams {
|
|
196
|
+
/** Provider source. Omit to aggregate all providers. */
|
|
197
|
+
source?: ProviderSource;
|
|
198
|
+
wallet: string;
|
|
199
|
+
limit?: number;
|
|
200
|
+
cursor?: string;
|
|
201
|
+
/** Comma-separated trade types. */
|
|
202
|
+
type?: string;
|
|
203
|
+
side?: string;
|
|
204
|
+
}
|
|
205
|
+
/** A single price-history data point. */
|
|
206
|
+
interface PricePoint {
|
|
207
|
+
/** Unix seconds. */
|
|
208
|
+
t: number;
|
|
209
|
+
/** Price [0, 1]. */
|
|
210
|
+
p: number;
|
|
211
|
+
}
|
|
212
|
+
/** Response from `GET /api/v1/markets/{slug}/price-history`. */
|
|
213
|
+
interface PriceHistoryResponse {
|
|
214
|
+
points: PricePoint[];
|
|
215
|
+
}
|
|
216
|
+
/** Accepted values for the `range` query parameter. */
|
|
217
|
+
type PriceHistoryRange = "1d" | "1w" | "1m" | "all";
|
|
218
|
+
/** OHLCV candlestick data point. */
|
|
219
|
+
interface Candlestick {
|
|
220
|
+
timestamp: string;
|
|
221
|
+
open: number;
|
|
222
|
+
high: number;
|
|
223
|
+
low: number;
|
|
224
|
+
close: number;
|
|
225
|
+
volume: number;
|
|
226
|
+
}
|
|
227
|
+
/** Query parameters for `GET /api/v1/markets/{slug}/candlesticks`. */
|
|
228
|
+
interface ListCandlesticksParams {
|
|
229
|
+
/** Candle interval (e.g. `"1m"`, `"5m"`, `"1h"`, `"1d"`). Default: `"1h"`. */
|
|
230
|
+
interval?: string;
|
|
231
|
+
/** Number of candles. Default: 100. */
|
|
232
|
+
limit?: number;
|
|
233
|
+
}
|
|
234
|
+
/** Query parameters for `GET /api/v1/events/{slug}/similar`. */
|
|
235
|
+
interface SimilarEventsParams {
|
|
236
|
+
/** Number of results (default 5, max 20). */
|
|
237
|
+
limit?: number;
|
|
238
|
+
/** When true, only return events from the same provider. */
|
|
239
|
+
same_source?: boolean;
|
|
240
|
+
}
|
|
241
|
+
/** A single user position in a prediction market. */
|
|
242
|
+
interface PredictPosition {
|
|
243
|
+
source: ProviderSource;
|
|
244
|
+
side: string;
|
|
245
|
+
size: number;
|
|
246
|
+
avg_price?: number;
|
|
247
|
+
current_price?: number;
|
|
248
|
+
current_value?: number;
|
|
249
|
+
initial_value?: number;
|
|
250
|
+
pnl?: number;
|
|
251
|
+
pnl_percent?: number;
|
|
252
|
+
realized_pnl?: number;
|
|
253
|
+
redeemable: boolean;
|
|
254
|
+
event?: PredictEvent;
|
|
255
|
+
market?: PredictMarket;
|
|
256
|
+
provider_meta?: ProviderMeta;
|
|
257
|
+
}
|
|
258
|
+
/** Response from `GET /api/v1/positions`. */
|
|
259
|
+
interface PositionsResponse {
|
|
260
|
+
user: string;
|
|
261
|
+
positions: PredictPosition[];
|
|
262
|
+
}
|
|
263
|
+
/** Response from `GET /api/v1/balance`. */
|
|
264
|
+
interface BalanceResponse {
|
|
265
|
+
user: string;
|
|
266
|
+
source: ProviderSource;
|
|
267
|
+
/** Token symbol (e.g. "USDC"). */
|
|
268
|
+
token: string;
|
|
269
|
+
/** Human-readable balance (e.g. "125.50"). */
|
|
270
|
+
balance: string;
|
|
271
|
+
/** Raw balance in smallest unit (e.g. "125500000"). */
|
|
272
|
+
raw_balance: string;
|
|
273
|
+
/** Token decimals (e.g. 6 for USDC). */
|
|
274
|
+
decimals: number;
|
|
275
|
+
}
|
|
276
|
+
/** Lifecycle status of a user order. */
|
|
277
|
+
type OrderStatus = "live" | "matched" | "cancelled" | "invalid" | "submitted" | "pending" | "open" | "pending_close" | "closed" | "failed" | "expired";
|
|
278
|
+
/** Order direction. */
|
|
279
|
+
type OrderSide = "BUY" | "SELL";
|
|
280
|
+
/** A single user order record. */
|
|
281
|
+
interface PredictOrder {
|
|
282
|
+
id: string;
|
|
283
|
+
source: ProviderSource;
|
|
284
|
+
status: OrderStatus;
|
|
285
|
+
market_id?: string;
|
|
286
|
+
asset_id?: string;
|
|
287
|
+
side?: OrderSide;
|
|
288
|
+
outcome?: string;
|
|
289
|
+
price?: string;
|
|
290
|
+
original_size?: string;
|
|
291
|
+
size_matched?: string;
|
|
292
|
+
in_amount?: string;
|
|
293
|
+
out_amount?: string;
|
|
294
|
+
order_type?: string;
|
|
295
|
+
/** Unix seconds. */
|
|
296
|
+
created_at?: number;
|
|
297
|
+
expires_at?: number;
|
|
298
|
+
provider_meta?: ProviderMeta;
|
|
299
|
+
}
|
|
300
|
+
/** Query parameters for `GET /api/v1/orders`. */
|
|
301
|
+
interface ListOrdersParams {
|
|
302
|
+
/** Provider source. Omit to aggregate all providers. */
|
|
303
|
+
source?: ProviderSource;
|
|
304
|
+
wallet_address?: string;
|
|
305
|
+
market_id?: string;
|
|
306
|
+
asset_id?: string;
|
|
307
|
+
next_cursor?: string;
|
|
308
|
+
}
|
|
309
|
+
/** Result of `DELETE /api/v1/orders/{id}`. */
|
|
310
|
+
interface CancelOrderResult {
|
|
311
|
+
cancelled: string[];
|
|
312
|
+
not_cancelled: Record<string, string>;
|
|
313
|
+
}
|
|
314
|
+
/** Request body for `POST /api/v1/orders/dflow/quote`. */
|
|
315
|
+
interface DFlowQuoteRequest {
|
|
316
|
+
inputMint: string;
|
|
317
|
+
outputMint: string;
|
|
318
|
+
amount: string;
|
|
319
|
+
userPublicKey: string;
|
|
320
|
+
slippageBps?: number;
|
|
321
|
+
}
|
|
322
|
+
/** Raw upstream response from DFlow quote (transparent passthrough). */
|
|
323
|
+
type DFlowQuoteResponse = Record<string, unknown>;
|
|
324
|
+
/** Context stored alongside a DFlow order for later enrichment. */
|
|
325
|
+
interface DFlowOrderContext {
|
|
326
|
+
user_public_key: string;
|
|
327
|
+
input_mint: string;
|
|
328
|
+
output_mint: string;
|
|
329
|
+
amount: string;
|
|
330
|
+
price: string;
|
|
331
|
+
side: string;
|
|
332
|
+
outcome: string;
|
|
333
|
+
market_slug: string;
|
|
334
|
+
slippage_bps: number;
|
|
335
|
+
}
|
|
336
|
+
/** Request body for `POST /api/v1/orders/dflow/submit`. */
|
|
337
|
+
interface DFlowSubmitRequest {
|
|
338
|
+
signedTransaction: string;
|
|
339
|
+
orderContext: DFlowOrderContext;
|
|
340
|
+
}
|
|
341
|
+
/** Response from `POST /api/v1/orders/dflow/submit`. */
|
|
342
|
+
interface DFlowSubmitResponse {
|
|
343
|
+
signature: string;
|
|
344
|
+
status: OrderStatus;
|
|
345
|
+
}
|
|
346
|
+
/** Order type for Polymarket CLOB. */
|
|
347
|
+
type PolymarketOrderType = "GTC" | "FOK" | "GTD" | "FAK";
|
|
348
|
+
/** Input for creating a Polymarket limit order. */
|
|
349
|
+
interface CreateOrderInput {
|
|
350
|
+
/** Token ID (outcome asset ID) from market provider_meta. */
|
|
351
|
+
tokenId: string;
|
|
352
|
+
/** Price in range [0.01, 0.99]. */
|
|
353
|
+
price: number;
|
|
354
|
+
/** Order size in USDC. */
|
|
355
|
+
size: number;
|
|
356
|
+
side: OrderSide;
|
|
357
|
+
orderType?: PolymarketOrderType;
|
|
358
|
+
/** Minimum tick size for this market (from Polymarket gamma API). */
|
|
359
|
+
tickSize: "0.1" | "0.01" | "0.001" | "0.0001";
|
|
360
|
+
/** Whether this is a neg-risk market (uses different exchange contract). */
|
|
361
|
+
negRisk?: boolean;
|
|
362
|
+
/** Expiration timestamp (Unix seconds). Required for GTD orders. */
|
|
363
|
+
expiration?: number;
|
|
364
|
+
/**
|
|
365
|
+
* The funder address that holds the USDC (proxy/Gnosis Safe wallet).
|
|
366
|
+
* Defaults to the signer address when not provided.
|
|
367
|
+
*/
|
|
368
|
+
funderAddress?: string;
|
|
369
|
+
}
|
|
370
|
+
/** WebSocket subscription channel. */
|
|
371
|
+
type WsChannel = "orderbook" | "prices" | "trades";
|
|
372
|
+
/** WebSocket connection lifecycle status. */
|
|
373
|
+
type WsConnectionStatus = "connecting" | "connected" | "disconnected" | "reconnecting";
|
|
374
|
+
/**
|
|
375
|
+
* Full orderbook snapshot pushed via the `orderbook` channel.
|
|
376
|
+
* Always a complete replacement — no delta merging required.
|
|
377
|
+
*/
|
|
378
|
+
interface WsOrderbookEvent {
|
|
379
|
+
market_slug: string;
|
|
380
|
+
bids: OrderbookLevel[];
|
|
381
|
+
asks: OrderbookLevel[];
|
|
382
|
+
spread?: number;
|
|
383
|
+
}
|
|
384
|
+
/** Latest bid/ask prices pushed via the `prices` channel. */
|
|
385
|
+
interface WsPriceEvent {
|
|
386
|
+
market_slug: string;
|
|
387
|
+
yes_bid?: number;
|
|
388
|
+
yes_ask?: number;
|
|
389
|
+
no_bid?: number;
|
|
390
|
+
no_ask?: number;
|
|
391
|
+
spread?: number;
|
|
392
|
+
}
|
|
393
|
+
/** Single trade execution pushed via the `trades` channel. */
|
|
394
|
+
interface WsTradeEvent {
|
|
395
|
+
market_slug: string;
|
|
396
|
+
trade_id?: string;
|
|
397
|
+
side: string;
|
|
398
|
+
price: number;
|
|
399
|
+
size: number;
|
|
400
|
+
outcome?: string;
|
|
401
|
+
}
|
|
402
|
+
/** Union of all channel data payloads. */
|
|
403
|
+
type WsChannelEvent = WsOrderbookEvent | WsPriceEvent | WsTradeEvent;
|
|
404
|
+
/** Subscribe or unsubscribe message sent to the server. */
|
|
405
|
+
interface WsSubscribeMessage {
|
|
406
|
+
type: "subscribe" | "unsubscribe";
|
|
407
|
+
channels: WsChannel[];
|
|
408
|
+
market_slugs: string[];
|
|
409
|
+
}
|
|
410
|
+
/** Client ping (application-level keepalive). */
|
|
411
|
+
interface WsPingMessage {
|
|
412
|
+
type: "ping";
|
|
413
|
+
}
|
|
414
|
+
/** Union of all messages the client can send. */
|
|
415
|
+
type WsClientMessage = WsSubscribeMessage | WsPingMessage;
|
|
416
|
+
/** Pong response to a client ping. */
|
|
417
|
+
interface WsPongMessage {
|
|
418
|
+
type: "pong";
|
|
419
|
+
}
|
|
420
|
+
/** Subscription confirmation. */
|
|
421
|
+
interface WsSubscribedMessage {
|
|
422
|
+
type: "subscribed";
|
|
423
|
+
channels: WsChannel[];
|
|
424
|
+
market_slugs: string[];
|
|
425
|
+
}
|
|
426
|
+
/** Error codes the server may return. */
|
|
427
|
+
type WsErrorCode = "invalid_message" | "market_not_found" | "provider_unavailable" | "provider_reconnecting";
|
|
428
|
+
/** Error message from the server. */
|
|
429
|
+
interface WsErrorMessage {
|
|
430
|
+
type: "error";
|
|
431
|
+
code: WsErrorCode;
|
|
432
|
+
message: string;
|
|
433
|
+
}
|
|
434
|
+
/** Data message carrying a channel event payload. */
|
|
435
|
+
interface WsDataMessage<T extends WsChannelEvent = WsChannelEvent> {
|
|
436
|
+
channel: WsChannel;
|
|
437
|
+
market_slug: string;
|
|
438
|
+
data: T;
|
|
439
|
+
ts: number;
|
|
440
|
+
}
|
|
441
|
+
/** Union of all messages the server can send. */
|
|
442
|
+
type WsServerMessage = WsPongMessage | WsSubscribedMessage | WsErrorMessage | WsDataMessage;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* HTTP client for the prediction-server REST API.
|
|
446
|
+
*
|
|
447
|
+
* Covers all prediction, market, order, position, and trading endpoints.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```ts
|
|
451
|
+
* const client = new PredictClient("https://api.example.com");
|
|
452
|
+
* const page = await client.listEvents({ status: "open", limit: 20 });
|
|
453
|
+
* const event = await client.getEvent("will-trump-win-2024");
|
|
454
|
+
* const market = await client.getMarket("will-trump-win-2024-yes");
|
|
455
|
+
* ```
|
|
456
|
+
*/
|
|
457
|
+
declare class PredictClient {
|
|
458
|
+
private readonly endpoint;
|
|
459
|
+
constructor(endpoint: string);
|
|
460
|
+
/**
|
|
461
|
+
* List prediction events with optional filtering, sorting, and pagination.
|
|
462
|
+
*
|
|
463
|
+
* Maps to `GET /api/v1/events`.
|
|
464
|
+
*
|
|
465
|
+
* @param params - Optional query parameters (filter, sort, pagination).
|
|
466
|
+
* @returns A paginated page of events.
|
|
467
|
+
*/
|
|
468
|
+
listEvents(params?: ListEventsParams): Promise<PredictPage<PredictEvent>>;
|
|
469
|
+
/**
|
|
470
|
+
* Fetch a single prediction event by its slug.
|
|
471
|
+
*
|
|
472
|
+
* Maps to `GET /api/v1/events/:slug?source=...`.
|
|
473
|
+
*
|
|
474
|
+
* @param slug - Canonical event slug (e.g. "will-trump-win-2024" for Polymarket
|
|
475
|
+
* or "KXBTCD-25FEB-T68000" for DFlow).
|
|
476
|
+
* @param source - Upstream provider (`"dflow"` or `"polymarket"`).
|
|
477
|
+
* @returns The matching event.
|
|
478
|
+
* @throws When the server responds with 404 or any other non-2xx status.
|
|
479
|
+
*/
|
|
480
|
+
getEvent(slug: string, source?: ProviderSource): Promise<PredictEvent>;
|
|
481
|
+
/**
|
|
482
|
+
* Fetch events similar to the given slug.
|
|
483
|
+
*
|
|
484
|
+
* Maps to `GET /api/v1/events/:slug/similar?source=...`.
|
|
485
|
+
*/
|
|
486
|
+
getSimilarEvents(slug: string, source: ProviderSource, params?: SimilarEventsParams): Promise<PredictEvent[]>;
|
|
487
|
+
/**
|
|
488
|
+
* Fetch a single prediction market by its slug.
|
|
489
|
+
*
|
|
490
|
+
* Maps to `GET /api/v1/markets/:slug?source=...`.
|
|
491
|
+
*
|
|
492
|
+
* @param slug - Canonical market slug.
|
|
493
|
+
* @param source - Upstream provider (`"dflow"` or `"polymarket"`).
|
|
494
|
+
* @returns The matching market.
|
|
495
|
+
* @throws When the server responds with 404 or any other non-2xx status.
|
|
496
|
+
*/
|
|
497
|
+
getMarket(slug: string, source?: ProviderSource): Promise<PredictMarket>;
|
|
498
|
+
/** Maps to `GET /api/v1/markets/:slug/orderbook?source=...`. */
|
|
499
|
+
getOrderbook(slug: string, source: ProviderSource): Promise<Orderbook>;
|
|
500
|
+
/** Maps to `GET /api/v1/markets/:slug/trades?source=...`. */
|
|
501
|
+
listMarketTrades(slug: string, params: ListMarketTradesParams): Promise<PredictPage<PredictTrade>>;
|
|
502
|
+
/** Maps to `GET /api/v1/markets/:slug/price-history?source=...&range=...`. */
|
|
503
|
+
getPriceHistory(slug: string, source: ProviderSource, range?: PriceHistoryRange): Promise<PriceHistoryResponse>;
|
|
504
|
+
/** Maps to `GET /api/v1/markets/:slug/candlesticks?interval=...&limit=...`. */
|
|
505
|
+
listCandlesticks(slug: string, params?: ListCandlesticksParams): Promise<Candlestick[]>;
|
|
506
|
+
/**
|
|
507
|
+
* Maps to `GET /api/v1/positions?source=...&user=...`.
|
|
508
|
+
*
|
|
509
|
+
* @param user - Wallet address.
|
|
510
|
+
* @param source - Provider source. Omit to aggregate all providers.
|
|
511
|
+
*/
|
|
512
|
+
getPositions(user: string, source?: ProviderSource): Promise<PositionsResponse>;
|
|
513
|
+
/**
|
|
514
|
+
* Get the on-chain USDC balance for a wallet.
|
|
515
|
+
*
|
|
516
|
+
* Maps to `GET /api/v1/balance?source=...&user=...`.
|
|
517
|
+
*
|
|
518
|
+
* @param source - Provider source (`"dflow"` for Solana, `"polymarket"` for Polygon).
|
|
519
|
+
* @param user - Wallet address.
|
|
520
|
+
*/
|
|
521
|
+
getBalance(source: ProviderSource, user: string): Promise<BalanceResponse>;
|
|
522
|
+
/** Maps to `GET /api/v1/orders?source=...&wallet_address=...`. */
|
|
523
|
+
listOrders(params: ListOrdersParams): Promise<PredictPage<PredictOrder>>;
|
|
524
|
+
/** Maps to `GET /api/v1/orders/:id?source=...`. */
|
|
525
|
+
getOrder(id: string, source: ProviderSource): Promise<PredictOrder>;
|
|
526
|
+
/** Maps to `DELETE /api/v1/orders/:id?source=...`. */
|
|
527
|
+
cancelOrder(id: string, source: ProviderSource): Promise<CancelOrderResult>;
|
|
528
|
+
/**
|
|
529
|
+
* Create a Polymarket limit order via the prediction-server proxy.
|
|
530
|
+
*
|
|
531
|
+
* Maps to `POST /api/v1/orders/polymarket`.
|
|
532
|
+
*
|
|
533
|
+
* The caller must attach Polymarket CLOB authentication headers:
|
|
534
|
+
* - `POLY_ADDRESS` — the EVM wallet address.
|
|
535
|
+
* - `POLY_SIGNATURE` — L1 signature or L2 API key.
|
|
536
|
+
* - `POLY_TIMESTAMP` — Unix milliseconds string.
|
|
537
|
+
* - `POLY_NONCE` — credential nonce (use `"0"` for in-session derived creds).
|
|
538
|
+
*
|
|
539
|
+
* @param input - Order parameters.
|
|
540
|
+
* @param headers - Polymarket CLOB auth headers (`POLY_*`).
|
|
541
|
+
*/
|
|
542
|
+
createPolymarketOrder(input: CreateOrderInput, headers: Record<string, string>): Promise<PredictOrder>;
|
|
543
|
+
/** Maps to `POST /api/v1/orders/dflow/quote`. */
|
|
544
|
+
createDFlowQuote(body: DFlowQuoteRequest): Promise<DFlowQuoteResponse>;
|
|
545
|
+
/** Maps to `POST /api/v1/orders/dflow/submit`. */
|
|
546
|
+
submitDFlowTransaction(body: DFlowSubmitRequest): Promise<DFlowSubmitResponse>;
|
|
547
|
+
/** Maps to `GET /api/v1/trades?source=...&wallet=...`. */
|
|
548
|
+
listTrades(params: ListTradesParams): Promise<PredictPage<PredictTrade>>;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Factory function for `PredictClient`.
|
|
552
|
+
*
|
|
553
|
+
* @param endpoint - Base URL of the prediction-server, without a trailing slash.
|
|
554
|
+
*/
|
|
555
|
+
declare function createPredictClient(endpoint: string): PredictClient;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* PredictWsClient — WebSocket client for prediction-server real-time data.
|
|
559
|
+
*
|
|
560
|
+
* Connects to `GET /api/v1/ws` (RFC 6455) and supports three channels:
|
|
561
|
+
* `orderbook`, `prices`, and `trades`.
|
|
562
|
+
*
|
|
563
|
+
* Features:
|
|
564
|
+
* - Auto-reconnect with exponential backoff
|
|
565
|
+
* - Application-level ping/pong keepalive
|
|
566
|
+
* - Subscription state tracking with automatic restore after reconnect
|
|
567
|
+
* - Event-emitter pattern for typed message dispatch
|
|
568
|
+
*/
|
|
569
|
+
|
|
570
|
+
/** Configuration for `PredictWsClient`. */
|
|
571
|
+
interface PredictWsClientConfig {
|
|
572
|
+
/** Full WebSocket URL, e.g. `"wss://api.example.com/api/v1/ws"`. */
|
|
573
|
+
wsUrl: string;
|
|
574
|
+
/** Connect immediately on construction. Default: `true`. */
|
|
575
|
+
autoConnect?: boolean;
|
|
576
|
+
/** Reconnect automatically on unexpected close. Default: `true`. */
|
|
577
|
+
autoReconnect?: boolean;
|
|
578
|
+
/** Base delay (ms) for exponential backoff. Default: `1000`. */
|
|
579
|
+
reconnectIntervalBase?: number;
|
|
580
|
+
/** Maximum reconnect delay (ms). Default: `30000`. */
|
|
581
|
+
reconnectMaxInterval?: number;
|
|
582
|
+
/** Interval (ms) between application-level pings. Default: `30000`. */
|
|
583
|
+
pingInterval?: number;
|
|
584
|
+
/**
|
|
585
|
+
* How long (ms) to wait for a pong after sending a ping before
|
|
586
|
+
* considering the connection dead. Default: `10000`.
|
|
587
|
+
* Must be less than `pingInterval`.
|
|
588
|
+
*/
|
|
589
|
+
pongTimeout?: number;
|
|
590
|
+
}
|
|
591
|
+
type EventCallback<T = unknown> = (data: T) => void;
|
|
592
|
+
interface EventListeners {
|
|
593
|
+
connect: EventCallback<void>[];
|
|
594
|
+
disconnect: EventCallback<{
|
|
595
|
+
code?: number;
|
|
596
|
+
reason?: string;
|
|
597
|
+
}>[];
|
|
598
|
+
reconnecting: EventCallback<{
|
|
599
|
+
attempt: number;
|
|
600
|
+
delay: number;
|
|
601
|
+
}>[];
|
|
602
|
+
error: EventCallback<{
|
|
603
|
+
code: WsErrorCode;
|
|
604
|
+
message: string;
|
|
605
|
+
}>[];
|
|
606
|
+
status: EventCallback<WsConnectionStatus>[];
|
|
607
|
+
subscribed: EventCallback<{
|
|
608
|
+
channels: WsChannel[];
|
|
609
|
+
market_slugs: string[];
|
|
610
|
+
}>[];
|
|
611
|
+
orderbook: EventCallback<WsDataMessage<WsOrderbookEvent>>[];
|
|
612
|
+
prices: EventCallback<WsDataMessage<WsPriceEvent>>[];
|
|
613
|
+
trades: EventCallback<WsDataMessage<WsTradeEvent>>[];
|
|
614
|
+
}
|
|
615
|
+
type WsEventType = keyof EventListeners;
|
|
616
|
+
declare class PredictWsClient {
|
|
617
|
+
private ws;
|
|
618
|
+
private readonly wsUrl;
|
|
619
|
+
private readonly autoReconnect;
|
|
620
|
+
private readonly reconnectBase;
|
|
621
|
+
private readonly reconnectMax;
|
|
622
|
+
private readonly pingMs;
|
|
623
|
+
private readonly pongTimeoutMs;
|
|
624
|
+
private status;
|
|
625
|
+
private reconnectAttempts;
|
|
626
|
+
private reconnectTimer;
|
|
627
|
+
private pingTimer;
|
|
628
|
+
private pongTimer;
|
|
629
|
+
private shouldReconnect;
|
|
630
|
+
private listeners;
|
|
631
|
+
private subs;
|
|
632
|
+
constructor(config: PredictWsClientConfig);
|
|
633
|
+
connect(): void;
|
|
634
|
+
disconnect(): void;
|
|
635
|
+
/** Disconnect and clear all subscription state and listeners. */
|
|
636
|
+
destroy(): void;
|
|
637
|
+
getStatus(): WsConnectionStatus;
|
|
638
|
+
isConnected(): boolean;
|
|
639
|
+
/**
|
|
640
|
+
* Subscribe to one or more channels for the given market slugs.
|
|
641
|
+
* The server acknowledges with a `subscribed` message.
|
|
642
|
+
*/
|
|
643
|
+
subscribe(channels: WsChannel[], marketSlugs: string[]): void;
|
|
644
|
+
/**
|
|
645
|
+
* Unsubscribe from one or more channels for the given market slugs.
|
|
646
|
+
*/
|
|
647
|
+
unsubscribe(channels: WsChannel[], marketSlugs: string[]): void;
|
|
648
|
+
/**
|
|
649
|
+
* Subscribe to price updates for the given slugs.
|
|
650
|
+
* Returns an unsubscribe function.
|
|
651
|
+
*/
|
|
652
|
+
subscribePrices(slugs: string[], onUpdate: (msg: WsDataMessage<WsPriceEvent>) => void): () => void;
|
|
653
|
+
/**
|
|
654
|
+
* Subscribe to orderbook snapshots for the given slugs.
|
|
655
|
+
* Returns an unsubscribe function.
|
|
656
|
+
*/
|
|
657
|
+
subscribeOrderbook(slugs: string[], onUpdate: (msg: WsDataMessage<WsOrderbookEvent>) => void): () => void;
|
|
658
|
+
/**
|
|
659
|
+
* Subscribe to trade events for the given slugs.
|
|
660
|
+
* Returns an unsubscribe function.
|
|
661
|
+
*/
|
|
662
|
+
subscribeTrades(slugs: string[], onUpdate: (msg: WsDataMessage<WsTradeEvent>) => void): () => void;
|
|
663
|
+
/** Register a callback for connection status changes. Returns unsubscribe. */
|
|
664
|
+
onStatusChange(cb: (status: WsConnectionStatus) => void): () => void;
|
|
665
|
+
/** Register a callback for server-sent errors. Returns unsubscribe. */
|
|
666
|
+
onError(cb: (err: {
|
|
667
|
+
code: WsErrorCode;
|
|
668
|
+
message: string;
|
|
669
|
+
}) => void): () => void;
|
|
670
|
+
on<T extends WsEventType>(event: T, callback: EventListeners[T][number]): () => void;
|
|
671
|
+
off<T extends WsEventType>(event: T, callback: EventListeners[T][number]): void;
|
|
672
|
+
private emit;
|
|
673
|
+
private send;
|
|
674
|
+
private setStatus;
|
|
675
|
+
private handleMessage;
|
|
676
|
+
private scheduleReconnect;
|
|
677
|
+
private clearReconnectTimer;
|
|
678
|
+
private restoreSubscriptions;
|
|
679
|
+
private startPing;
|
|
680
|
+
private stopPing;
|
|
681
|
+
/**
|
|
682
|
+
* Start a timer that fires if the server does not reply with pong
|
|
683
|
+
* within `pongTimeoutMs`. On timeout the socket is closed so
|
|
684
|
+
* the normal reconnect flow kicks in.
|
|
685
|
+
*/
|
|
686
|
+
private armPongTimeout;
|
|
687
|
+
private clearPongTimer;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Factory function for `PredictWsClient`.
|
|
691
|
+
*
|
|
692
|
+
* @param config - WebSocket client configuration.
|
|
693
|
+
*/
|
|
694
|
+
declare function createPredictWsClient(config: PredictWsClientConfig): PredictWsClient;
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Server-safe pure functions for events query parameters.
|
|
698
|
+
*
|
|
699
|
+
* This module contains NO React imports and can be used in Server Components,
|
|
700
|
+
* route handlers, or any non-browser context.
|
|
701
|
+
*/
|
|
702
|
+
|
|
703
|
+
/** Default page size for list queries. */
|
|
704
|
+
declare const DEFAULT_PAGE_SIZE = 48;
|
|
705
|
+
/**
|
|
706
|
+
* Selection emitted by a categories/tag selector widget.
|
|
707
|
+
* Duplicated here to avoid pulling in UI component barrel into server-only bundles.
|
|
708
|
+
*/
|
|
709
|
+
interface TagSlugSelection {
|
|
710
|
+
categorySlug: string | null;
|
|
711
|
+
tagSlug: string | null;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Resolve a `TagSlugSelection` to a single `tag_slug` string for the API.
|
|
715
|
+
*/
|
|
716
|
+
declare function resolveTagSlug(selection: TagSlugSelection | null | undefined): string | undefined;
|
|
717
|
+
/**
|
|
718
|
+
* Input accepted by {@link resolveEventsParams}. All fields optional;
|
|
719
|
+
* defaults match the client-side `useInfiniteEvents` hook's initial state.
|
|
720
|
+
*/
|
|
721
|
+
interface ResolveEventsParamsInput {
|
|
722
|
+
tagSlugSelection?: TagSlugSelection | null;
|
|
723
|
+
limit?: number;
|
|
724
|
+
status?: EventStatus;
|
|
725
|
+
sort_by?: EventSortField;
|
|
726
|
+
sort_asc?: boolean;
|
|
727
|
+
source?: ProviderSource;
|
|
728
|
+
with_markets?: boolean;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Build a clean `ListEventsParams` from loose user inputs.
|
|
732
|
+
*
|
|
733
|
+
* Server-side `prefetchInfiniteQuery` and client-side `useInfiniteEvents` must
|
|
734
|
+
* both use this function to guarantee identical query keys.
|
|
735
|
+
*/
|
|
736
|
+
declare function resolveEventsParams(input?: ResolveEventsParamsInput): ListEventsParams;
|
|
737
|
+
/** Stable TanStack Query key for the events infinite query. */
|
|
738
|
+
declare function infiniteEventsQueryKey(params: ListEventsParams): unknown[];
|
|
739
|
+
/**
|
|
740
|
+
* Fetch a single page of events. Can be used outside of React
|
|
741
|
+
* (e.g. in Server Components, loaders, or tests).
|
|
742
|
+
*/
|
|
743
|
+
declare function fetchEventsPage(client: PredictClient, params: ListEventsParams): Promise<PredictPage<PredictEvent>>;
|
|
744
|
+
/** Stable TanStack Query key for a single event. */
|
|
745
|
+
declare function eventQueryKey(slug: string, source?: ProviderSource): unknown[];
|
|
746
|
+
/**
|
|
747
|
+
* Fetch a single event. Can be used outside of React
|
|
748
|
+
* (e.g. in Server Components, loaders, or tests).
|
|
749
|
+
*/
|
|
750
|
+
declare function fetchEvent(client: PredictClient, slug: string, source?: ProviderSource): Promise<PredictEvent>;
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Server-safe pure functions for market query helpers.
|
|
754
|
+
*
|
|
755
|
+
* This module contains NO React imports and can be used in Server Components,
|
|
756
|
+
* route handlers, or any non-browser context.
|
|
757
|
+
*/
|
|
758
|
+
|
|
759
|
+
/** Stable TanStack Query key for a single market. */
|
|
760
|
+
declare function marketQueryKey(slug: string, source?: ProviderSource): unknown[];
|
|
761
|
+
/**
|
|
762
|
+
* Fetch function usable outside React (e.g. in loaders or tests).
|
|
763
|
+
*/
|
|
764
|
+
declare function fetchMarket(client: PredictClient, slug: string, source?: ProviderSource): Promise<PredictMarket>;
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Polymarket L2 HMAC-SHA256 signing utilities.
|
|
768
|
+
*
|
|
769
|
+
* Uses the Web Crypto API (available in all modern browsers and Node ≥18) to
|
|
770
|
+
* avoid taking a dependency on Node.js `crypto` or any third-party library.
|
|
771
|
+
*
|
|
772
|
+
* References:
|
|
773
|
+
* https://docs.polymarket.com/#authentication
|
|
774
|
+
* https://github.com/Polymarket/clob-client/blob/main/src/headers/index.ts
|
|
775
|
+
*/
|
|
776
|
+
/**
|
|
777
|
+
* Compute an HMAC-SHA256 signature using the Web Crypto API.
|
|
778
|
+
*
|
|
779
|
+
* @param secretBase64 - The signing secret encoded as base64.
|
|
780
|
+
* @param message - The plaintext message to sign.
|
|
781
|
+
* @returns The signature as a base64 string.
|
|
782
|
+
*/
|
|
783
|
+
declare function hmacSha256Base64(secretBase64: string, message: string): Promise<string>;
|
|
784
|
+
/** HTTP method values accepted by Polymarket CLOB. */
|
|
785
|
+
type HttpMethod = "GET" | "DELETE" | "POST" | "PUT";
|
|
786
|
+
interface PolymarketL2HeadersInput {
|
|
787
|
+
/** L2 API key (from credential exchange). */
|
|
788
|
+
apiKey: string;
|
|
789
|
+
/** L2 HMAC secret (base64-encoded). */
|
|
790
|
+
secret: string;
|
|
791
|
+
/** L2 passphrase. */
|
|
792
|
+
passphrase: string;
|
|
793
|
+
/** HTTP method of the request. */
|
|
794
|
+
method: HttpMethod;
|
|
795
|
+
/** Request path without host, e.g. "/order". */
|
|
796
|
+
requestPath: string;
|
|
797
|
+
/** Raw JSON request body string; empty string for GET/DELETE. */
|
|
798
|
+
body?: string;
|
|
799
|
+
}
|
|
800
|
+
interface PolymarketL2Headers {
|
|
801
|
+
POLY_ADDRESS: string;
|
|
802
|
+
POLY_SIGNATURE: string;
|
|
803
|
+
POLY_TIMESTAMP: string;
|
|
804
|
+
POLY_API_KEY: string;
|
|
805
|
+
POLY_PASSPHRASE: string;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Build Polymarket L2 HMAC authentication headers for a CLOB API request.
|
|
809
|
+
*
|
|
810
|
+
* The signature is `HMAC-SHA256(secret, timestamp + method + requestPath + body)`.
|
|
811
|
+
*
|
|
812
|
+
* @param address - EVM wallet address (owner of the credentials).
|
|
813
|
+
* @param input - Credential and request details.
|
|
814
|
+
* @returns An object containing all required `POLY_*` headers.
|
|
815
|
+
*/
|
|
816
|
+
declare function buildPolymarketL2Headers(address: string, input: PolymarketL2HeadersInput): Promise<PolymarketL2Headers>;
|
|
817
|
+
/** EIP-712 domain used by Polymarket CLOB for L1 authentication. */
|
|
818
|
+
declare const CLOB_AUTH_DOMAIN: {
|
|
819
|
+
readonly name: "ClobAuthDomain";
|
|
820
|
+
readonly version: "1";
|
|
821
|
+
readonly chainId: 137;
|
|
822
|
+
};
|
|
823
|
+
/** EIP-712 types for the `ClobAuth` struct. */
|
|
824
|
+
declare const CLOB_AUTH_TYPES: {
|
|
825
|
+
readonly ClobAuth: readonly [{
|
|
826
|
+
readonly name: "address";
|
|
827
|
+
readonly type: "address";
|
|
828
|
+
}, {
|
|
829
|
+
readonly name: "timestamp";
|
|
830
|
+
readonly type: "string";
|
|
831
|
+
}, {
|
|
832
|
+
readonly name: "nonce";
|
|
833
|
+
readonly type: "uint256";
|
|
834
|
+
}, {
|
|
835
|
+
readonly name: "message";
|
|
836
|
+
readonly type: "string";
|
|
837
|
+
}];
|
|
838
|
+
};
|
|
839
|
+
interface BuildClobAuthMessageInput {
|
|
840
|
+
/** EVM wallet address. */
|
|
841
|
+
address: string;
|
|
842
|
+
/** Unix seconds string. */
|
|
843
|
+
timestamp: string;
|
|
844
|
+
/** Credential nonce. Use `0` for a permanent / deterministic credential. */
|
|
845
|
+
nonce: number;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Build the EIP-712 typed data value for a `ClobAuth` message.
|
|
849
|
+
* The resulting object is passed to `eth_signTypedData_v4`.
|
|
850
|
+
*/
|
|
851
|
+
declare function buildClobAuthMessage(input: BuildClobAuthMessageInput): Record<string, unknown>;
|
|
852
|
+
/**
|
|
853
|
+
* Exchange an L1 EIP-712 signature for Polymarket L2 API credentials.
|
|
854
|
+
*
|
|
855
|
+
* Calls `GET https://clob.polymarket.com/auth/derive-api-key` directly from
|
|
856
|
+
* the browser (the endpoint has `Access-Control-Allow-Origin: *`).
|
|
857
|
+
*
|
|
858
|
+
* @returns The raw API response `{ apiKey, secret, passphrase }`.
|
|
859
|
+
*/
|
|
860
|
+
declare function derivePolymarketApiKey(address: string, signature: string, timestamp: string, nonce: number): Promise<{
|
|
861
|
+
apiKey: string;
|
|
862
|
+
secret: string;
|
|
863
|
+
passphrase: string;
|
|
864
|
+
}>;
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Polymarket CTF Exchange order construction utilities.
|
|
868
|
+
*
|
|
869
|
+
* Implements EIP-712 signing for `Order` structs on the CTF Exchange and
|
|
870
|
+
* Neg-Risk CTF Exchange contracts (Polygon mainnet).
|
|
871
|
+
*
|
|
872
|
+
* References:
|
|
873
|
+
* https://github.com/Polymarket/clob-client/blob/main/src/order-builder/builder.ts
|
|
874
|
+
* https://github.com/Polymarket/clob-client/blob/main/src/order-builder/helpers.ts
|
|
875
|
+
* https://docs.polymarket.com/#create-order
|
|
876
|
+
*/
|
|
877
|
+
|
|
878
|
+
/** Polymarket CTF Exchange contract (standard markets). */
|
|
879
|
+
declare const CTF_EXCHANGE_ADDRESS: "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E";
|
|
880
|
+
/** Polymarket Neg-Risk CTF Exchange contract (neg-risk markets). */
|
|
881
|
+
declare const NEG_RISK_CTF_EXCHANGE_ADDRESS: "0xC5d563A36AE78145C45a50134d48A1215220f80a";
|
|
882
|
+
/** USDC.e (Bridged USDC) on Polygon — the only collateral token on Polymarket. */
|
|
883
|
+
declare const USDC_ADDRESS: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
|
|
884
|
+
/** Chain ID for Polygon mainnet. */
|
|
885
|
+
declare const POLYGON_CHAIN_ID = 137;
|
|
886
|
+
/**
|
|
887
|
+
* Build the EIP-712 domain for the CTF Exchange.
|
|
888
|
+
*
|
|
889
|
+
* @param negRisk - When `true`, use the Neg-Risk exchange address.
|
|
890
|
+
*/
|
|
891
|
+
declare function buildCtfExchangeDomain(negRisk?: boolean): {
|
|
892
|
+
name: string;
|
|
893
|
+
version: string;
|
|
894
|
+
chainId: number;
|
|
895
|
+
verifyingContract: "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E" | "0xC5d563A36AE78145C45a50134d48A1215220f80a";
|
|
896
|
+
};
|
|
897
|
+
/** EIP-712 type definitions for the Polymarket `Order` struct. */
|
|
898
|
+
declare const CTF_ORDER_TYPES: {
|
|
899
|
+
readonly Order: readonly [{
|
|
900
|
+
readonly name: "salt";
|
|
901
|
+
readonly type: "uint256";
|
|
902
|
+
}, {
|
|
903
|
+
readonly name: "maker";
|
|
904
|
+
readonly type: "address";
|
|
905
|
+
}, {
|
|
906
|
+
readonly name: "signer";
|
|
907
|
+
readonly type: "address";
|
|
908
|
+
}, {
|
|
909
|
+
readonly name: "taker";
|
|
910
|
+
readonly type: "address";
|
|
911
|
+
}, {
|
|
912
|
+
readonly name: "tokenId";
|
|
913
|
+
readonly type: "uint256";
|
|
914
|
+
}, {
|
|
915
|
+
readonly name: "makerAmount";
|
|
916
|
+
readonly type: "uint256";
|
|
917
|
+
}, {
|
|
918
|
+
readonly name: "takerAmount";
|
|
919
|
+
readonly type: "uint256";
|
|
920
|
+
}, {
|
|
921
|
+
readonly name: "expiration";
|
|
922
|
+
readonly type: "uint256";
|
|
923
|
+
}, {
|
|
924
|
+
readonly name: "nonce";
|
|
925
|
+
readonly type: "uint256";
|
|
926
|
+
}, {
|
|
927
|
+
readonly name: "feeRateBps";
|
|
928
|
+
readonly type: "uint256";
|
|
929
|
+
}, {
|
|
930
|
+
readonly name: "side";
|
|
931
|
+
readonly type: "uint8";
|
|
932
|
+
}, {
|
|
933
|
+
readonly name: "signatureType";
|
|
934
|
+
readonly type: "uint8";
|
|
935
|
+
}];
|
|
936
|
+
};
|
|
937
|
+
/** Order type enum matching CTFExchange.sol. */
|
|
938
|
+
declare const ORDER_TYPE: {
|
|
939
|
+
readonly GTC: 0;
|
|
940
|
+
readonly FOK: 1;
|
|
941
|
+
readonly GTD: 2;
|
|
942
|
+
readonly FAK: 3;
|
|
943
|
+
};
|
|
944
|
+
/** Side enum matching CTFExchange.sol (0 = BUY, 1 = SELL). */
|
|
945
|
+
declare const SIDE: {
|
|
946
|
+
readonly BUY: 0;
|
|
947
|
+
readonly SELL: 1;
|
|
948
|
+
};
|
|
949
|
+
interface BuildOrderMessageInput extends CreateOrderInput {
|
|
950
|
+
/** Signer / owner address. */
|
|
951
|
+
signerAddress: string;
|
|
952
|
+
/** Signature type (0 = EOA, 1 = Magic, 2 = Poly Proxy). */
|
|
953
|
+
signatureType: 0 | 1 | 2;
|
|
954
|
+
}
|
|
955
|
+
interface OrderMessage {
|
|
956
|
+
salt: string;
|
|
957
|
+
maker: string;
|
|
958
|
+
signer: string;
|
|
959
|
+
taker: string;
|
|
960
|
+
tokenId: string;
|
|
961
|
+
makerAmount: string;
|
|
962
|
+
takerAmount: string;
|
|
963
|
+
expiration: string;
|
|
964
|
+
nonce: string;
|
|
965
|
+
feeRateBps: string;
|
|
966
|
+
side: number;
|
|
967
|
+
signatureType: number;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Build the EIP-712 typed data value for a Polymarket limit order.
|
|
971
|
+
*
|
|
972
|
+
* All amounts are in micro-USDC (1 USDC = 1 000 000).
|
|
973
|
+
*
|
|
974
|
+
* For a BUY order:
|
|
975
|
+
* - `makerAmount` = USDC spent (cost = size × price)
|
|
976
|
+
* - `takerAmount` = shares received (= size)
|
|
977
|
+
*
|
|
978
|
+
* For a SELL order:
|
|
979
|
+
* - `makerAmount` = shares sold (= size)
|
|
980
|
+
* - `takerAmount` = USDC received (cost = size × price)
|
|
981
|
+
*/
|
|
982
|
+
declare function buildOrderMessage(input: BuildOrderMessageInput): OrderMessage;
|
|
983
|
+
interface SignedOrder extends OrderMessage {
|
|
984
|
+
signature: string;
|
|
985
|
+
orderType: string;
|
|
986
|
+
}
|
|
987
|
+
interface BuildSignedOrderInput extends BuildOrderMessageInput {
|
|
988
|
+
/** Hex signature returned by `eth_signTypedData_v4`. */
|
|
989
|
+
signature: string;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Construct the final signed order payload to submit to the Polymarket CLOB
|
|
993
|
+
* (via prediction-server proxy `POST /api/v1/orders/polymarket`).
|
|
994
|
+
*/
|
|
995
|
+
declare function buildSignedOrder(input: BuildSignedOrderInput): SignedOrder;
|
|
996
|
+
|
|
997
|
+
export { type BalanceResponse, type BuildClobAuthMessageInput, type BuildOrderMessageInput, type BuildSignedOrderInput, CLOB_AUTH_DOMAIN, CLOB_AUTH_TYPES, CTF_EXCHANGE_ADDRESS, CTF_ORDER_TYPES, type CancelOrderResult, type Candlestick, type CreateOrderInput, DEFAULT_PAGE_SIZE, type DFlowOrderContext, type DFlowQuoteRequest, type DFlowQuoteResponse, type DFlowSubmitRequest, type DFlowSubmitResponse, type EventSortField, type EventStatus, type EventSummary, type HttpMethod, type ListCandlesticksParams, type ListEventsParams, type ListMarketTradesParams, type ListOrdersParams, type ListTradesParams, type MarketOutcome, type MarketResult, type MarketStatus, type MarketSummary, NEG_RISK_CTF_EXCHANGE_ADDRESS, ORDER_TYPE, type OrderMessage, type OrderSide, type OrderStatus, type Orderbook, type OrderbookLevel, POLYGON_CHAIN_ID, type PolymarketL2Headers, type PolymarketL2HeadersInput, type PolymarketOrderType, type PositionsResponse, PredictClient, type PredictEvent, type PredictMarket, type PredictOrder, type PredictPage, type PredictPosition, type PredictTag, type PredictTrade, PredictWsClient, type PredictWsClientConfig, type PriceHistoryRange, type PriceHistoryResponse, type PricePoint, type ProviderMeta, type ProviderSource, type ResolveEventsParamsInput, SIDE, type SettlementSource, type SignedOrder, type SimilarEventsParams, type TagSlugSelection, type TradeType, USDC_ADDRESS, type WsChannel, type WsChannelEvent, type WsClientMessage, type WsConnectionStatus, type WsDataMessage, type WsErrorCode, type WsErrorMessage, type WsOrderbookEvent, type WsPingMessage, type WsPongMessage, type WsPriceEvent, type WsServerMessage, type WsSubscribeMessage, type WsSubscribedMessage, type WsTradeEvent, buildClobAuthMessage, buildCtfExchangeDomain, buildOrderMessage, buildPolymarketL2Headers, buildSignedOrder, createPredictClient, createPredictWsClient, derivePolymarketApiKey, eventQueryKey, fetchEvent, fetchEventsPage, fetchMarket, hmacSha256Base64, infiniteEventsQueryKey, marketQueryKey, resolveEventsParams, resolveTagSlug };
|