polynode 0.12.1

Rust SDK for the PolyNode API — real-time Polymarket data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! REST API response types.

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatusResponse {
    pub node_connected: bool,
    pub uptime_seconds: u64,
    pub stream_length: u64,
    pub events_stream_length: u64,
    pub pending_txs: u64,
    pub ws_subscribers: u64,
    pub firehose_connections: u32,
    pub redis_memory: String,
    pub state: StateSummary,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateSummary {
    pub market_count: u64,
    pub wallet_count: u64,
    pub metadata_count: u64,
    pub events_buffered: u64,
    pub events_processed: u64,
    pub latest_block: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyResponse {
    pub api_key: String,
    pub name: String,
    pub rate_limit_per_minute: u32,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketsResponse {
    pub count: u64,
    pub total: u64,
    pub markets: Vec<MarketSummary>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketSummary {
    /// Token ID (present in markets endpoint, absent in search results).
    #[serde(default)]
    pub token_id: Option<String>,
    /// Token IDs array (present in search results).
    #[serde(default)]
    pub token_ids: Option<Vec<String>>,
    pub last_price: Option<f64>,
    #[serde(default)]
    pub volume_24h: f64,
    #[serde(default)]
    pub trade_count_24h: u64,
    #[serde(default)]
    pub last_trade_at: Option<i64>,
    pub question: Option<String>,
    pub slug: Option<String>,
    pub outcomes: Option<Vec<String>>,
    pub condition_id: Option<String>,
    #[serde(default)]
    pub neg_risk: Option<bool>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MarketsListResponse {
    pub count: u64,
    pub total: u64,
    pub cursor: Option<u64>,
    pub markets: Vec<MarketSummary>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListMarketsParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_volume: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResponse {
    pub query: String,
    pub count: u64,
    pub results: Vec<MarketSummary>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct CandlesResponse {
    pub candles: Vec<Candle>,
    pub count: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Candle {
    pub timestamp: i64,
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub volume: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settlement {
    #[serde(default)]
    pub id: Option<String>,
    #[serde(default)]
    pub status: String,
    #[serde(default)]
    pub taker_side: String,
    #[serde(default)]
    pub taker_size: String,
    #[serde(default)]
    pub taker_price: String,
    #[serde(default)]
    pub taker_wallet: Option<String>,
    #[serde(default)]
    pub taker_token: Option<String>,
    #[serde(default)]
    pub market_title: String,
    #[serde(default)]
    pub market_slug: Option<String>,
    #[serde(default)]
    pub market_image: Option<String>,
    #[serde(default)]
    pub event_title: Option<String>,
    #[serde(default)]
    pub outcome: Option<String>,
    #[serde(default)]
    pub timestamp: Option<String>,
    #[serde(default)]
    pub detected_at: Option<String>,
    #[serde(default)]
    pub block_number: Option<String>,
    #[serde(default)]
    pub tx_hash: Option<String>,
    /// Raw JSON for any additional fields not covered above.
    #[serde(flatten)]
    pub extra: serde_json::Value,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SettlementsResponse {
    pub count: u64,
    pub settlements: Vec<Settlement>,
    #[serde(default)]
    pub wallet: Option<String>,
    #[serde(default)]
    pub token_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct WalletResponse {
    pub wallet: String,
    #[serde(flatten)]
    pub extra: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    pub jsonrpc: String,
    pub method: String,
    pub params: serde_json::Value,
    pub id: serde_json::Value,
}

#[derive(Debug, Clone, Deserialize)]
pub struct JsonRpcResponse {
    pub jsonrpc: String,
    pub id: Option<serde_json::Value>,
    pub result: Option<serde_json::Value>,
    pub error: Option<JsonRpcError>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct JsonRpcError {
    pub code: i32,
    pub message: String,
}

// ── Wallet Positions (used by cache backfill) ──

#[derive(Debug, Clone, Deserialize)]
pub struct WalletPositionsResponse {
    pub wallet: Option<String>,
    pub count: u64,
    pub positions: Vec<serde_json::Value>,
}

// ── Onchain Positions (from PNL subgraph via v2-proxy) ──

#[derive(Debug, Clone, Deserialize)]
pub struct OnchainPosition {
    pub token_id: String,
    pub size: f64,
    pub avg_price: f64,
    pub realized_pnl: f64,
    pub total_bought: f64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct WalletOnchainPositionsResponse {
    pub wallet: String,
    pub source: String,
    pub count: u64,
    pub open_count: u64,
    pub closed_count: u64,
    pub total_realized_pnl: f64,
    pub positions_with_pnl: u64,
    pub positions: Vec<OnchainPosition>,
}

// ── Wallet/Market Trades (used by cache backfill) ──

#[derive(Debug, Clone, Deserialize)]
pub struct WalletTradesResponse {
    pub wallet: Option<String>,
    pub count: u64,
    pub trades: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MarketTradesResponse {
    pub condition_id: Option<String>,
    pub count: u64,
    pub trades: Vec<serde_json::Value>,
}

// ── Orderbook (REST) ──

#[derive(Debug, Clone, Deserialize)]
pub struct OrderbookRestResponse {
    pub bids: Vec<OrderbookRestLevel>,
    pub asks: Vec<OrderbookRestLevel>,
    pub asset_id: String,
    pub market: String,
    pub hash: String,
    pub last_trade_price: String,
    pub min_order_size: String,
    pub tick_size: String,
    pub neg_risk: bool,
    pub timestamp: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct OrderbookRestLevel {
    pub price: String,
    pub size: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MidpointResponse {
    pub mid: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SpreadResponse {
    pub spread: String,
}

// ── Enriched Data ──

#[derive(Debug, Clone, Deserialize)]
pub struct LeaderboardTrader {
    pub rank: u32,
    pub wallet: String,
    pub name: String,
    pub pnl: f64,
    pub volume: f64,
    #[serde(rename = "profileImage")]
    pub profile_image: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct LeaderboardResponse {
    pub traders: Vec<LeaderboardTrader>,
    pub period: String,
    pub sort: String,
    #[serde(default)]
    pub count: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct TrendingResponse {
    pub carousel: Vec<serde_json::Value>,
    pub breaking: Vec<serde_json::Value>,
    #[serde(rename = "hotTopics")]
    pub hot_topics: Vec<serde_json::Value>,
    pub featured: Vec<serde_json::Value>,
    pub movers: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ActivityTrade {
    pub wallet: String,
    pub side: String,
    #[serde(rename = "tokenId")]
    pub token_id: String,
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    pub size: f64,
    pub price: f64,
    pub timestamp: i64,
    pub title: String,
    pub slug: String,
    #[serde(rename = "eventSlug")]
    pub event_slug: String,
    pub outcome: String,
    pub name: String,
    #[serde(rename = "txHash")]
    pub tx_hash: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ActivityResponse {
    pub trades: Vec<ActivityTrade>,
    pub count: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MoverMarket {
    pub id: String,
    pub slug: String,
    pub question: String,
    pub image: Option<String>,
    #[serde(rename = "outcomePrices")]
    pub outcome_prices: Vec<String>,
    #[serde(rename = "oneDayPriceChange")]
    pub one_day_price_change: f64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MoversResponse {
    pub markets: Vec<MoverMarket>,
    pub count: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct TraderProfile {
    pub wallet: String,
    pub name: String,
    pub pseudonym: Option<String>,
    #[serde(rename = "profileSlug")]
    pub profile_slug: String,
    #[serde(rename = "joinDate")]
    pub join_date: String,
    pub trades: u64,
    #[serde(rename = "marketsTraded")]
    pub markets_traded: u64,
    #[serde(rename = "largestWin")]
    pub largest_win: f64,
    pub views: u64,
    #[serde(rename = "totalVolume")]
    pub total_volume: f64,
    #[serde(rename = "totalPnl")]
    pub total_pnl: f64,
    #[serde(rename = "realizedPnl")]
    pub realized_pnl: f64,
    #[serde(rename = "unrealizedPnl")]
    pub unrealized_pnl: f64,
    #[serde(rename = "positionValue")]
    pub position_value: f64,
    #[serde(rename = "profileImage")]
    pub profile_image: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct PnlPoint {
    pub timestamp: i64,
    pub pnl: f64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct TraderPnlResponse {
    pub wallet: String,
    pub period: String,
    pub series: Vec<PnlPoint>,
    pub count: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EventMarket {
    pub question: String,
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    pub outcomes: Vec<String>,
    #[serde(rename = "outcomePrices")]
    pub outcome_prices: Vec<String>,
    pub volume: f64,
    pub liquidity: f64,
    pub active: bool,
    pub closed: bool,
    #[serde(rename = "groupItemTitle")]
    pub group_item_title: Option<String>,
    #[serde(rename = "tokenId")]
    pub token_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EventDetailResponse {
    pub id: String,
    pub slug: String,
    pub title: String,
    pub description: String,
    pub image: Option<String>,
    #[serde(default)]
    pub icon: Option<String>,
    pub active: bool,
    pub closed: bool,
    pub volume: f64,
    #[serde(rename = "volume24hr", default)]
    pub volume_24hr: f64,
    pub liquidity: f64,
    #[serde(rename = "openInterest", default)]
    pub open_interest: Option<f64>,
    #[serde(rename = "startDate")]
    pub start_date: String,
    #[serde(rename = "endDate", default)]
    pub end_date: Option<String>,
    pub markets: Vec<EventMarket>,
    #[serde(rename = "similarMarkets", default)]
    pub similar_markets: u64,
    #[serde(default)]
    pub annotations: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EventSearchResult {
    pub id: String,
    pub slug: String,
    pub title: String,
    pub image: Option<String>,
    pub active: bool,
    pub markets: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EventSearchResponse {
    pub query: String,
    pub events: Vec<EventSearchResult>,
    pub count: u64,
}