polyoxide-clob 0.12.4

Rust client library for Polymarket CLOB (order book) API
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Market channel message types.
//!
//! The market channel provides real-time order book and price updates.

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Order summary in the order book
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderSummary {
    /// Price level
    #[serde(with = "rust_decimal::serde::str")]
    pub price: Decimal,
    /// Size at this price level
    #[serde(with = "rust_decimal::serde::str")]
    pub size: Decimal,
}

/// Book message - full order book snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookMessage {
    /// Event type (always "book")
    pub event_type: String,
    /// Asset ID (token ID)
    pub asset_id: String,
    /// Market condition ID
    pub market: String,
    /// Timestamp in milliseconds (as string)
    pub timestamp: String,
    /// Order book hash
    pub hash: String,
    /// Buy orders (bids)
    pub bids: Vec<OrderSummary>,
    /// Sell orders (asks)
    pub asks: Vec<OrderSummary>,
    /// Last trade price
    pub last_trade_price: Option<String>,
}

/// Price change entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceChange {
    /// Asset ID (token ID)
    pub asset_id: String,
    /// Price level
    #[serde(with = "rust_decimal::serde::str")]
    pub price: Decimal,
    /// Size at this price level
    #[serde(with = "rust_decimal::serde::str")]
    pub size: Decimal,
    /// Order side (BUY or SELL)
    pub side: String,
    /// Order book hash
    pub hash: String,
    /// Best bid price
    #[serde(default, with = "rust_decimal::serde::str_option")]
    pub best_bid: Option<Decimal>,
    /// Best ask price
    #[serde(default, with = "rust_decimal::serde::str_option")]
    pub best_ask: Option<Decimal>,
}

/// Price change message - incremental order book update
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceChangeMessage {
    /// Event type (always "price_change")
    pub event_type: String,
    /// Market condition ID
    pub market: String,
    /// List of price changes
    pub price_changes: Vec<PriceChange>,
    /// Timestamp in milliseconds (as string)
    pub timestamp: String,
}

/// Tick size change message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TickSizeChangeMessage {
    /// Event type (always "tick_size_change")
    pub event_type: String,
    /// Asset ID (token ID)
    pub asset_id: String,
    /// Market condition ID
    pub market: String,
    /// Old tick size
    pub old_tick_size: String,
    /// New tick size
    pub new_tick_size: String,
    /// Side (BUY or SELL)
    pub side: String,
    /// Timestamp in milliseconds (as string)
    pub timestamp: String,
}

/// Last trade price message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LastTradePriceMessage {
    /// Event type (always "last_trade_price")
    pub event_type: String,
    /// Asset ID (token ID)
    pub asset_id: String,
    /// Market condition ID
    pub market: String,
    /// Trade price
    #[serde(with = "rust_decimal::serde::str")]
    pub price: Decimal,
    /// Trade side (BUY or SELL)
    pub side: String,
    /// Trade size
    #[serde(with = "rust_decimal::serde::str")]
    pub size: Decimal,
    /// Fee rate
    pub fee_rate_bps: Option<String>,
    /// Timestamp in milliseconds (as string)
    pub timestamp: String,
}

/// Market channel message types
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum MarketMessage {
    /// Full order book snapshot
    Book(BookMessage),
    /// Incremental price change
    PriceChange(PriceChangeMessage),
    /// Tick size change
    TickSizeChange(TickSizeChangeMessage),
    /// Last trade price
    LastTradePrice(LastTradePriceMessage),
}

impl MarketMessage {
    /// Parse a market channel message from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        // Book messages come as an array with a single element
        if json.starts_with('[') {
            let books: Vec<BookMessage> = serde_json::from_str(json)?;
            if let Some(book) = books.into_iter().next() {
                return Ok(MarketMessage::Book(book));
            }
            return Err(serde::de::Error::custom("Empty book array"));
        }

        #[derive(Deserialize)]
        struct RawMessage {
            event_type: String,
        }

        let raw: RawMessage = serde_json::from_str(json)?;
        match raw.event_type.as_str() {
            "book" => Ok(MarketMessage::Book(serde_json::from_str(json)?)),
            "price_change" => Ok(MarketMessage::PriceChange(serde_json::from_str(json)?)),
            "tick_size_change" => Ok(MarketMessage::TickSizeChange(serde_json::from_str(json)?)),
            "last_trade_price" => Ok(MarketMessage::LastTradePrice(serde_json::from_str(json)?)),
            _ => Err(serde::de::Error::custom(format!(
                "Unknown market event type: {}",
                raw.event_type
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    #[test]
    fn deserialize_book_message_array_format() {
        let json = r#"[{
            "event_type": "book",
            "asset_id": "abc123",
            "market": "0xcond",
            "timestamp": "1700000000000",
            "hash": "0xhash",
            "bids": [{"price": "0.55", "size": "100"}],
            "asks": [{"price": "0.60", "size": "200"}],
            "last_trade_price": "0.57"
        }]"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::Book(book) => {
                assert_eq!(book.event_type, "book");
                assert_eq!(book.asset_id, "abc123");
                assert_eq!(book.market, "0xcond");
                assert_eq!(book.hash, "0xhash");
                assert_eq!(book.bids.len(), 1);
                assert_eq!(book.bids[0].price, dec!(0.55));
                assert_eq!(book.bids[0].size, dec!(100));
                assert_eq!(book.asks.len(), 1);
                assert_eq!(book.asks[0].price, dec!(0.60));
                assert_eq!(book.asks[0].size, dec!(200));
                assert_eq!(book.last_trade_price.as_deref(), Some("0.57"));
            }
            _ => panic!("Expected Book variant"),
        }
    }

    #[test]
    fn deserialize_book_message_object_format() {
        let json = r#"{
            "event_type": "book",
            "asset_id": "abc123",
            "market": "0xcond",
            "timestamp": "1700000000000",
            "hash": "0xhash",
            "bids": [],
            "asks": [],
            "last_trade_price": null
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::Book(book) => {
                assert_eq!(book.event_type, "book");
                assert!(book.bids.is_empty());
                assert!(book.asks.is_empty());
                assert!(book.last_trade_price.is_none());
            }
            _ => panic!("Expected Book variant"),
        }
    }

    #[test]
    fn deserialize_book_empty_array_errors() {
        let json = "[]";
        let err = MarketMessage::from_json(json).unwrap_err();
        assert!(err.to_string().contains("Empty book array"));
    }

    #[test]
    fn deserialize_price_change_message() {
        let json = r#"{
            "event_type": "price_change",
            "market": "0xcond",
            "price_changes": [{
                "asset_id": "abc123",
                "price": "0.55",
                "size": "100",
                "side": "BUY",
                "hash": "0xhash",
                "best_bid": "0.54",
                "best_ask": "0.56"
            }],
            "timestamp": "1700000000000"
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::PriceChange(pc) => {
                assert_eq!(pc.market, "0xcond");
                assert_eq!(pc.price_changes.len(), 1);
                let change = &pc.price_changes[0];
                assert_eq!(change.price, dec!(0.55));
                assert_eq!(change.size, dec!(100));
                assert_eq!(change.side, "BUY");
                assert_eq!(change.best_bid, Some(dec!(0.54)));
                assert_eq!(change.best_ask, Some(dec!(0.56)));
            }
            _ => panic!("Expected PriceChange variant"),
        }
    }

    #[test]
    fn deserialize_price_change_optional_fields() {
        let json = r#"{
            "event_type": "price_change",
            "market": "0xcond",
            "price_changes": [{
                "asset_id": "abc123",
                "price": "0.55",
                "size": "100",
                "side": "SELL",
                "hash": "0xhash"
            }],
            "timestamp": "1700000000000"
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::PriceChange(pc) => {
                let change = &pc.price_changes[0];
                assert!(change.best_bid.is_none());
                assert!(change.best_ask.is_none());
            }
            _ => panic!("Expected PriceChange variant"),
        }
    }

    #[test]
    fn deserialize_tick_size_change_message() {
        let json = r#"{
            "event_type": "tick_size_change",
            "asset_id": "abc123",
            "market": "0xcond",
            "old_tick_size": "0.01",
            "new_tick_size": "0.001",
            "side": "BUY",
            "timestamp": "1700000000000"
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::TickSizeChange(tsc) => {
                assert_eq!(tsc.asset_id, "abc123");
                assert_eq!(tsc.old_tick_size, "0.01");
                assert_eq!(tsc.new_tick_size, "0.001");
                assert_eq!(tsc.side, "BUY");
            }
            _ => panic!("Expected TickSizeChange variant"),
        }
    }

    #[test]
    fn deserialize_last_trade_price_message() {
        let json = r#"{
            "event_type": "last_trade_price",
            "asset_id": "abc123",
            "market": "0xcond",
            "price": "0.72",
            "side": "SELL",
            "size": "50",
            "fee_rate_bps": "200",
            "timestamp": "1700000000000"
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::LastTradePrice(ltp) => {
                assert_eq!(ltp.price, dec!(0.72));
                assert_eq!(ltp.side, "SELL");
                assert_eq!(ltp.size, dec!(50));
                assert_eq!(ltp.fee_rate_bps.as_deref(), Some("200"));
            }
            _ => panic!("Expected LastTradePrice variant"),
        }
    }

    #[test]
    fn deserialize_last_trade_price_optional_fee() {
        let json = r#"{
            "event_type": "last_trade_price",
            "asset_id": "abc123",
            "market": "0xcond",
            "price": "0.72",
            "side": "BUY",
            "size": "50",
            "timestamp": "1700000000000"
        }"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::LastTradePrice(ltp) => {
                assert!(ltp.fee_rate_bps.is_none());
            }
            _ => panic!("Expected LastTradePrice variant"),
        }
    }

    #[test]
    fn from_json_unknown_event_type_errors() {
        let json = r#"{"event_type": "unknown_event", "market": "0x1"}"#;
        let err = MarketMessage::from_json(json).unwrap_err();
        assert!(err.to_string().contains("Unknown market event type"));
        assert!(err.to_string().contains("unknown_event"));
    }

    #[test]
    fn from_json_invalid_json_errors() {
        let err = MarketMessage::from_json("not json at all").unwrap_err();
        assert!(err.is_data() || err.is_syntax());
    }

    #[test]
    fn from_json_missing_event_type_errors() {
        let json = r#"{"market": "0xcond"}"#;
        let err = MarketMessage::from_json(json).unwrap_err();
        assert!(err.is_data());
    }

    #[test]
    fn order_summary_decimal_precision() {
        let json = r#"{"price": "0.123456789", "size": "999999.999"}"#;
        let summary: OrderSummary = serde_json::from_str(json).unwrap();
        assert_eq!(summary.price, dec!(0.123456789));
        assert_eq!(summary.size, dec!(999999.999));
    }

    #[test]
    fn book_array_with_multiple_entries_takes_first() {
        // from_json takes only the first element from a multi-entry array
        let json = r#"[
            {
                "event_type": "book",
                "asset_id": "first",
                "market": "0xcond",
                "timestamp": "1",
                "hash": "0xh1",
                "bids": [],
                "asks": []
            },
            {
                "event_type": "book",
                "asset_id": "second",
                "market": "0xcond",
                "timestamp": "2",
                "hash": "0xh2",
                "bids": [],
                "asks": []
            }
        ]"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::Book(book) => {
                assert_eq!(book.asset_id, "first", "Should take first entry only");
            }
            _ => panic!("Expected Book variant"),
        }
    }

    #[test]
    fn order_summary_rejects_non_numeric_price() {
        let json = r#"{"price": "not_a_number", "size": "100"}"#;
        let err = serde_json::from_str::<OrderSummary>(json).unwrap_err();
        assert!(err.is_data());
    }

    #[test]
    fn price_change_rejects_non_numeric_decimal_fields() {
        let json = r#"{
            "asset_id": "abc",
            "price": "invalid",
            "size": "100",
            "side": "BUY",
            "hash": "0xh"
        }"#;
        let err = serde_json::from_str::<PriceChange>(json).unwrap_err();
        assert!(err.is_data());
    }

    #[test]
    fn book_message_multiple_bids_asks() {
        let json = r#"[{
            "event_type": "book",
            "asset_id": "abc123",
            "market": "0xcond",
            "timestamp": "1700000000000",
            "hash": "0xhash",
            "bids": [
                {"price": "0.55", "size": "100"},
                {"price": "0.54", "size": "200"},
                {"price": "0.53", "size": "300"}
            ],
            "asks": [
                {"price": "0.60", "size": "50"},
                {"price": "0.61", "size": "75"}
            ]
        }]"#;

        let msg = MarketMessage::from_json(json).unwrap();
        match msg {
            MarketMessage::Book(book) => {
                assert_eq!(book.bids.len(), 3);
                assert_eq!(book.asks.len(), 2);
                assert_eq!(book.bids[2].price, dec!(0.53));
                assert_eq!(book.bids[2].size, dec!(300));
            }
            _ => panic!("Expected Book variant"),
        }
    }
}