polymarket-bindings 0.1.0

API bindings and normalized models for the Polymarket Rust SDK
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
474
475
476
477
478
479
480
481
482
use polymarket_types::{CtfConditionId, DecimalString, EventId, EvmAddress, MarketId, TokenId};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use crate::de::{
    deserialize_decimalish, deserialize_empty_string_as_none, deserialize_string_array,
};

/// Reference to an event embedded in a market.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketEventRef {
    pub id: polymarket_types::EventId,
    pub slug: Option<String>,
    pub title: Option<String>,
}

/// Tag reference attached to a market or event.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TagReference {
    pub id: String,
    pub slug: Option<String>,
    pub label: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketState {
    pub active: Option<bool>,
    pub closed: Option<bool>,
    pub archived: Option<bool>,
    pub accepting_orders: Option<bool>,
    pub enable_order_book: Option<bool>,
    pub neg_risk: Option<bool>,
    pub start_date: Option<String>,
    pub end_date: Option<String>,
    pub closed_time: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketOutcome {
    pub label: String,
    pub token_id: Option<TokenId>,
    pub price: Option<DecimalString>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketOutcomes {
    pub yes: MarketOutcome,
    pub no: MarketOutcome,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketMetrics {
    pub volume: Option<DecimalString>,
    pub volume_num: Option<DecimalString>,
    pub volume24hr: Option<DecimalString>,
    pub liquidity: Option<DecimalString>,
    pub liquidity_num: Option<DecimalString>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketPrices {
    pub best_bid: Option<DecimalString>,
    pub best_ask: Option<DecimalString>,
    pub last_trade_price: Option<DecimalString>,
    pub spread: Option<DecimalString>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketTrading {
    pub minimum_order_size: Option<DecimalString>,
    pub minimum_tick_size: Option<DecimalString>,
    pub seconds_delay: Option<i64>,
    pub fees_enabled: Option<bool>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MarketResolution {
    pub question_id: Option<String>,
    pub uma_resolution_status: Option<String>,
    pub source: Option<String>,
    pub resolved_by: Option<EvmAddress>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Market {
    pub id: MarketId,
    pub slug: Option<String>,
    pub condition_id: Option<CtfConditionId>,
    pub question: Option<String>,
    pub description: Option<String>,
    pub category: Option<String>,
    pub image: Option<String>,
    pub icon: Option<String>,
    pub state: MarketState,
    pub outcomes: MarketOutcomes,
    pub metrics: MarketMetrics,
    pub prices: MarketPrices,
    pub trading: MarketTrading,
    pub resolution: MarketResolution,
    pub events: Vec<MarketEventRef>,
    pub tags: Vec<TagReference>,
}

/// Raw Gamma API market payload (snake_case fields).
#[derive(Debug, Deserialize)]
pub struct GammaMarket {
    pub id: String,
    pub slug: Option<String>,
    #[serde(rename = "conditionId")]
    pub condition_id: Option<String>,
    pub question: Option<String>,
    pub description: Option<String>,
    pub category: Option<String>,
    pub image: Option<String>,
    pub icon: Option<String>,
    pub active: Option<bool>,
    pub closed: Option<bool>,
    pub archived: Option<bool>,
    #[serde(rename = "acceptingOrders")]
    pub accepting_orders: Option<bool>,
    #[serde(rename = "enableOrderBook")]
    pub enable_order_book: Option<bool>,
    #[serde(rename = "negRisk")]
    pub neg_risk: Option<bool>,
    #[serde(rename = "startDate")]
    pub start_date: Option<String>,
    #[serde(rename = "endDate")]
    pub end_date: Option<String>,
    #[serde(rename = "closedTime")]
    pub closed_time: Option<String>,
    #[serde(default, deserialize_with = "deserialize_string_array")]
    pub outcomes: Vec<String>,
    #[serde(
        default,
        rename = "outcomePrices",
        deserialize_with = "deserialize_string_array"
    )]
    pub outcome_prices: Vec<String>,
    #[serde(default, deserialize_with = "deserialize_decimalish")]
    pub volume: Option<String>,
    #[serde(
        default,
        rename = "volumeNum",
        deserialize_with = "deserialize_decimalish"
    )]
    pub volume_num: Option<String>,
    #[serde(
        default,
        rename = "volume24hr",
        deserialize_with = "deserialize_decimalish"
    )]
    pub volume24hr: Option<String>,
    #[serde(default, deserialize_with = "deserialize_decimalish")]
    pub liquidity: Option<String>,
    #[serde(
        default,
        rename = "liquidityNum",
        deserialize_with = "deserialize_decimalish"
    )]
    pub liquidity_num: Option<String>,
    #[serde(
        default,
        rename = "bestBid",
        deserialize_with = "deserialize_decimalish"
    )]
    pub best_bid: Option<String>,
    #[serde(
        default,
        rename = "bestAsk",
        deserialize_with = "deserialize_decimalish"
    )]
    pub best_ask: Option<String>,
    #[serde(
        default,
        rename = "lastTradePrice",
        deserialize_with = "deserialize_decimalish"
    )]
    pub last_trade_price: Option<String>,
    #[serde(default, deserialize_with = "deserialize_decimalish")]
    pub spread: Option<String>,
    #[serde(
        default,
        rename = "orderMinSize",
        deserialize_with = "deserialize_decimalish"
    )]
    pub order_min_size: Option<String>,
    #[serde(
        default,
        rename = "orderPriceMinTickSize",
        deserialize_with = "deserialize_decimalish"
    )]
    pub order_price_min_tick_size: Option<String>,
    #[serde(rename = "secondsDelay")]
    pub seconds_delay: Option<i64>,
    #[serde(rename = "feesEnabled")]
    pub fees_enabled: Option<bool>,
    #[serde(rename = "questionID")]
    pub question_id: Option<String>,
    #[serde(rename = "umaResolutionStatus")]
    pub uma_resolution_status: Option<String>,
    #[serde(rename = "resolutionSource")]
    pub resolution_source: Option<String>,
    #[serde(default, deserialize_with = "deserialize_empty_string_as_none")]
    pub resolved_by: Option<String>,
    #[serde(
        default,
        rename = "clobTokenIds",
        deserialize_with = "deserialize_string_array"
    )]
    pub clob_token_ids: Vec<String>,
    #[serde(default)]
    pub(crate) events: Vec<GammaMarketEvent>,
    #[serde(default)]
    pub tags: Vec<TagReference>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct GammaMarketEvent {
    id: String,
    slug: Option<String>,
    title: Option<String>,
}

pub const fn has_binary_outcomes(market: &GammaMarket) -> bool {
    market.outcomes.len() == 2
}

pub fn try_normalize_market(raw: GammaMarket) -> Result<Market, String> {
    if !has_binary_outcomes(&raw) {
        return Err(format!(
            "expected binary market outcomes, received {}",
            raw.outcomes.len()
        ));
    }

    let id = MarketId::parse(raw.id).map_err(|e| e.message)?;
    let condition_id = raw
        .condition_id
        .as_deref()
        .map(CtfConditionId::parse)
        .transpose()
        .map_err(|e| e.message)?;

    let resolved_by = raw
        .resolved_by
        .as_deref()
        .map(EvmAddress::from_str)
        .transpose()
        .map_err(|e| e.message)?;

    let parse_decimal = |s: Option<String>| {
        s.filter(|v| !v.is_empty())
            .and_then(|v| DecimalString::parse(v).ok())
    };

    let yes_label = raw.outcomes[0].clone();
    let no_label = raw.outcomes[1].clone();

    Ok(Market {
        id,
        slug: raw.slug,
        condition_id,
        question: raw.question,
        description: raw.description,
        category: raw.category,
        image: raw.image,
        icon: raw.icon,
        state: MarketState {
            active: raw.active,
            closed: raw.closed,
            archived: raw.archived,
            accepting_orders: raw.accepting_orders,
            enable_order_book: raw.enable_order_book,
            neg_risk: raw.neg_risk,
            start_date: raw.start_date,
            end_date: raw.end_date,
            closed_time: raw.closed_time,
        },
        outcomes: MarketOutcomes {
            yes: MarketOutcome {
                label: yes_label,
                token_id: raw
                    .clob_token_ids
                    .first()
                    .and_then(|t| TokenId::parse(t.clone()).ok()),
                price: raw
                    .outcome_prices
                    .first()
                    .and_then(|p| DecimalString::parse(p.clone()).ok()),
            },
            no: MarketOutcome {
                label: no_label,
                token_id: raw
                    .clob_token_ids
                    .get(1)
                    .and_then(|t| TokenId::parse(t.clone()).ok()),
                price: raw
                    .outcome_prices
                    .get(1)
                    .and_then(|p| DecimalString::parse(p.clone()).ok()),
            },
        },
        metrics: MarketMetrics {
            volume: parse_decimal(raw.volume),
            volume_num: parse_decimal(raw.volume_num),
            volume24hr: parse_decimal(raw.volume24hr),
            liquidity: parse_decimal(raw.liquidity),
            liquidity_num: parse_decimal(raw.liquidity_num),
        },
        prices: MarketPrices {
            best_bid: parse_decimal(raw.best_bid),
            best_ask: parse_decimal(raw.best_ask),
            last_trade_price: parse_decimal(raw.last_trade_price),
            spread: parse_decimal(raw.spread),
        },
        trading: MarketTrading {
            minimum_order_size: parse_decimal(raw.order_min_size),
            minimum_tick_size: parse_decimal(raw.order_price_min_tick_size),
            seconds_delay: raw.seconds_delay,
            fees_enabled: raw.fees_enabled,
        },
        resolution: MarketResolution {
            question_id: raw.question_id,
            uma_resolution_status: raw.uma_resolution_status,
            source: raw.resolution_source,
            resolved_by,
        },
        events: raw
            .events
            .into_iter()
            .filter_map(|e| {
                Some(MarketEventRef {
                    id: EventId::parse(e.id).ok()?,
                    slug: e.slug,
                    title: e.title,
                })
            })
            .collect(),
        tags: raw.tags,
    })
}

pub fn normalize_market(raw: GammaMarket) -> Market {
    try_normalize_market(raw).expect("binary market normalization should succeed")
}

#[derive(Debug, Deserialize)]
pub struct ListMarketsKeysetRaw {
    pub markets: Vec<GammaMarket>,
    pub next_cursor: Option<String>,
}

#[derive(Debug)]
pub struct ListMarketsKeysetResponse {
    pub items: Vec<Market>,
    pub next_cursor: Option<polymarket_types::PaginationCursor>,
}

impl ListMarketsKeysetResponse {
    pub fn from_raw(raw: ListMarketsKeysetRaw) -> Self {
        let items = raw
            .markets
            .into_iter()
            .filter_map(|m| try_normalize_market(m).ok())
            .collect();
        let next_cursor = raw
            .next_cursor
            .and_then(|c| polymarket_types::PaginationCursor::parse(c).ok());
        Self { items, next_cursor }
    }
}

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

    #[test]
    fn normalizes_binary_market() {
        let raw = GammaMarket {
            id: "123".into(),
            slug: Some("test-market".into()),
            condition_id: Some(
                "0x4cd77d456c83e7d8c569a8fb8f6396c3f40154f657e6d970733e2b1b6a7110ff".into(),
            ),
            question: Some("Will it happen?".into()),
            description: None,
            category: Some("politics".into()),
            image: None,
            icon: None,
            active: Some(true),
            closed: Some(false),
            archived: Some(false),
            accepting_orders: Some(true),
            enable_order_book: Some(true),
            neg_risk: Some(false),
            start_date: None,
            end_date: None,
            closed_time: None,
            outcomes: vec!["Yes".into(), "No".into()],
            outcome_prices: vec!["0.52".into(), "0.48".into()],
            volume: Some("1000".into()),
            volume_num: None,
            volume24hr: None,
            liquidity: None,
            liquidity_num: None,
            best_bid: Some("0.51".into()),
            best_ask: Some("0.53".into()),
            last_trade_price: Some("0.52".into()),
            spread: Some("0.02".into()),
            order_min_size: Some("5".into()),
            order_price_min_tick_size: Some("0.01".into()),
            seconds_delay: None,
            fees_enabled: Some(false),
            question_id: None,
            uma_resolution_status: None,
            resolution_source: None,
            resolved_by: None,
            clob_token_ids: vec!["token-yes".into(), "token-no".into()],
            events: vec![],
            tags: vec![],
        };

        let market = normalize_market(raw);
        assert_eq!(market.id.as_str(), "123");
        assert_eq!(market.outcomes.yes.label, "Yes");
        assert_eq!(
            market.outcomes.yes.token_id.as_ref().map(|t| t.as_str()),
            Some("token-yes")
        );
    }

    #[test]
    fn skips_non_binary_in_list_response() {
        let raw = ListMarketsKeysetRaw {
            markets: vec![GammaMarket {
                id: "1".into(),
                slug: None,
                condition_id: None,
                question: None,
                description: None,
                category: None,
                image: None,
                icon: None,
                active: None,
                closed: None,
                archived: None,
                accepting_orders: None,
                enable_order_book: None,
                neg_risk: None,
                start_date: None,
                end_date: None,
                closed_time: None,
                outcomes: vec!["A".into(), "B".into(), "C".into()],
                outcome_prices: vec![],
                volume: None,
                volume_num: None,
                volume24hr: None,
                liquidity: None,
                liquidity_num: None,
                best_bid: None,
                best_ask: None,
                last_trade_price: None,
                spread: None,
                order_min_size: None,
                order_price_min_tick_size: None,
                seconds_delay: None,
                fees_enabled: None,
                question_id: None,
                uma_resolution_status: None,
                resolution_source: None,
                resolved_by: None,
                clob_token_ids: vec![],
                events: vec![],
                tags: vec![],
            }],
            next_cursor: None,
        };

        let response = ListMarketsKeysetResponse::from_raw(raw);
        assert!(response.items.is_empty());
    }
}