Skip to main content

polymarket_bindings/gamma/
event.rs

1use polymarket_types::EventId;
2use serde::{Deserialize, Serialize};
3
4use super::market::{try_normalize_market, GammaMarket, Market, TagReference};
5
6/// Normalized Polymarket event.
7#[derive(Clone, Debug, PartialEq, Serialize)]
8pub struct Event {
9    pub id: EventId,
10    pub slug: Option<String>,
11    pub title: Option<String>,
12    pub subtitle: Option<String>,
13    pub description: Option<String>,
14    pub category: Option<String>,
15    pub subcategory: Option<String>,
16    pub image: Option<String>,
17    pub icon: Option<String>,
18    pub state: EventState,
19    pub markets: Vec<Market>,
20    pub tags: Vec<TagReference>,
21}
22
23#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
24pub struct EventState {
25    pub active: Option<bool>,
26    pub closed: Option<bool>,
27    pub archived: Option<bool>,
28    pub featured: Option<bool>,
29    pub restricted: Option<bool>,
30}
31
32#[derive(Debug, Deserialize)]
33pub struct GammaEvent {
34    pub id: String,
35    pub slug: Option<String>,
36    pub title: Option<String>,
37    pub subtitle: Option<String>,
38    pub description: Option<String>,
39    pub category: Option<String>,
40    pub subcategory: Option<String>,
41    pub image: Option<String>,
42    pub icon: Option<String>,
43    pub active: Option<bool>,
44    pub closed: Option<bool>,
45    pub archived: Option<bool>,
46    pub featured: Option<bool>,
47    pub restricted: Option<bool>,
48    #[serde(default)]
49    pub markets: Vec<GammaMarket>,
50    #[serde(default)]
51    pub tags: Vec<TagReference>,
52}
53
54pub fn try_normalize_event(raw: GammaEvent) -> Result<Event, String> {
55    let id = EventId::parse(raw.id).map_err(|e| e.message)?;
56    let markets = raw
57        .markets
58        .into_iter()
59        .filter_map(|m| try_normalize_market(m).ok())
60        .collect();
61
62    Ok(Event {
63        id,
64        slug: raw.slug,
65        title: raw.title,
66        subtitle: raw.subtitle,
67        description: raw.description,
68        category: raw.category,
69        subcategory: raw.subcategory,
70        image: raw.image,
71        icon: raw.icon,
72        state: EventState {
73            active: raw.active,
74            closed: raw.closed,
75            archived: raw.archived,
76            featured: raw.featured,
77            restricted: raw.restricted,
78        },
79        markets,
80        tags: raw.tags,
81    })
82}
83
84pub fn normalize_event(raw: GammaEvent) -> Event {
85    try_normalize_event(raw).expect("event normalization should succeed for valid gamma events")
86}
87
88#[derive(Debug, Deserialize)]
89pub struct ListEventsKeysetRaw {
90    pub events: Vec<GammaEvent>,
91    pub next_cursor: Option<String>,
92}
93
94#[derive(Debug)]
95pub struct ListEventsKeysetResponse {
96    pub items: Vec<Event>,
97    pub next_cursor: Option<polymarket_types::PaginationCursor>,
98}
99
100impl ListEventsKeysetResponse {
101    pub fn from_raw(raw: ListEventsKeysetRaw) -> Self {
102        let items = raw.events.into_iter().map(normalize_event).collect();
103        let next_cursor = raw
104            .next_cursor
105            .and_then(|c| polymarket_types::PaginationCursor::parse(c).ok());
106        Self { items, next_cursor }
107    }
108}