polyoxide-gamma 0.14.0

Rust client library for Polymarket Gamma (market data) 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
use polyoxide_core::{HttpClient, QueryBuilder, Request};

use crate::{
    error::GammaError,
    types::{CountResponse, Event, EventCreator, EventsPagination, KeysetEventsResponse, Tag},
};

/// Events namespace for event-related operations
#[derive(Clone)]
pub struct Events {
    pub(crate) http_client: HttpClient,
}

impl Events {
    /// List events with optional filtering
    pub fn list(&self) -> ListEvents {
        ListEvents {
            request: Request::new(self.http_client.clone(), "/events"),
        }
    }

    /// Get an event by ID
    pub fn get(&self, id: impl Into<String>) -> GetEvent {
        GetEvent {
            request: Request::new(
                self.http_client.clone(),
                format!("/events/{}", urlencoding::encode(&id.into())),
            ),
        }
    }

    /// Get an event by slug
    pub fn get_by_slug(&self, slug: impl Into<String>) -> GetEvent {
        GetEvent {
            request: Request::new(
                self.http_client.clone(),
                format!("/events/slug/{}", urlencoding::encode(&slug.into())),
            ),
        }
    }

    /// Get tags for an event
    pub fn tags(&self, id: impl Into<String>) -> Request<Vec<Tag>, GammaError> {
        Request::new(
            self.http_client.clone(),
            format!("/events/{}/tags", urlencoding::encode(&id.into())),
        )
    }

    /// Get tweet count for an event
    pub fn tweet_count(&self, id: impl Into<String>) -> Request<CountResponse, GammaError> {
        Request::new(
            self.http_client.clone(),
            format!("/events/{}/tweet-count", urlencoding::encode(&id.into())),
        )
    }

    /// Get comment count for an event
    pub fn comment_count(&self, id: impl Into<String>) -> Request<CountResponse, GammaError> {
        Request::new(
            self.http_client.clone(),
            format!("/events/{}/comments/count", urlencoding::encode(&id.into())),
        )
    }

    /// List event creators with optional filtering
    /// (`GET /events/creators`).
    pub fn list_creators(&self) -> ListEventCreators {
        ListEventCreators {
            request: Request::new(self.http_client.clone(), "/events/creators"),
        }
    }

    /// Get an event creator by ID (`GET /events/creators/{id}`).
    pub fn get_creator(&self, id: impl Into<String>) -> Request<EventCreator, GammaError> {
        Request::new(
            self.http_client.clone(),
            format!("/events/creators/{}", urlencoding::encode(&id.into())),
        )
    }

    /// List events with offset-style pagination metadata
    /// (`GET /events/pagination`).
    pub fn list_paginated(&self) -> ListPaginatedEvents {
        ListPaginatedEvents {
            request: Request::new(self.http_client.clone(), "/events/pagination"),
        }
    }

    /// List sport event results (`GET /events/results`).
    pub fn list_results(&self) -> ListEventResults {
        ListEventResults {
            request: Request::new(self.http_client.clone(), "/events/results"),
        }
    }

    /// List events using cursor-based (keyset) pagination
    /// (`GET /events/keyset`).
    ///
    /// Prefer this over [`Self::list`] for stable paging through large result
    /// sets. Use `next_cursor` from each response as `after_cursor` in the
    /// next request; pagination is complete when `next_cursor` is `None`.
    ///
    /// Note: a handful of obscure upstream query parameters
    /// (`start_time_min/max`, `event_date`, `event_week`, `recurrence`,
    /// `created_by`, `parent_event_id`, `include_children`, `partner_slug`,
    /// `include_best_lines`, `locale`, `decimalized`, `tag_match`) are not
    /// yet exposed. The majority of filters are available; callers needing
    /// the omitted params can reach the endpoint directly via
    /// [`Request::query`].
    pub fn list_keyset(&self) -> ListKeysetEvents {
        ListKeysetEvents {
            request: Request::new(self.http_client.clone(), "/events/keyset"),
        }
    }
}

/// Request builder for [`Events::list_creators`].
pub struct ListEventCreators {
    request: Request<Vec<EventCreator>, GammaError>,
}

impl ListEventCreators {
    /// Limit the number of results (minimum: 0).
    pub fn limit(mut self, limit: u32) -> Self {
        self.request = self.request.query("limit", limit);
        self
    }

    /// Pagination offset (minimum: 0).
    pub fn offset(mut self, offset: u32) -> Self {
        self.request = self.request.query("offset", offset);
        self
    }

    /// Comma-separated list of fields to order by.
    pub fn order(mut self, order: impl Into<String>) -> Self {
        self.request = self.request.query("order", order.into());
        self
    }

    /// Sort direction.
    pub fn ascending(mut self, ascending: bool) -> Self {
        self.request = self.request.query("ascending", ascending);
        self
    }

    /// Filter by creator name.
    pub fn creator_name(mut self, name: impl Into<String>) -> Self {
        self.request = self.request.query("creator_name", name.into());
        self
    }

    /// Filter by creator handle.
    pub fn creator_handle(mut self, handle: impl Into<String>) -> Self {
        self.request = self.request.query("creator_handle", handle.into());
        self
    }

    /// Execute the request.
    pub async fn send(self) -> Result<Vec<EventCreator>, GammaError> {
        self.request.send().await
    }
}

/// Request builder for [`Events::list_paginated`].
pub struct ListPaginatedEvents {
    request: Request<EventsPagination, GammaError>,
}

impl ListPaginatedEvents {
    /// Limit the number of results.
    pub fn limit(mut self, limit: u32) -> Self {
        self.request = self.request.query("limit", limit);
        self
    }

    /// Pagination offset.
    pub fn offset(mut self, offset: u32) -> Self {
        self.request = self.request.query("offset", offset);
        self
    }

    /// Comma-separated list of fields to order by.
    pub fn order(mut self, order: impl Into<String>) -> Self {
        self.request = self.request.query("order", order.into());
        self
    }

    /// Sort direction.
    pub fn ascending(mut self, ascending: bool) -> Self {
        self.request = self.request.query("ascending", ascending);
        self
    }

    /// Include chat data in response.
    pub fn include_chat(mut self, include: bool) -> Self {
        self.request = self.request.query("include_chat", include);
        self
    }

    /// Include template data in response.
    pub fn include_template(mut self, include: bool) -> Self {
        self.request = self.request.query("include_template", include);
        self
    }

    /// Filter by recurrence pattern.
    pub fn recurrence(mut self, recurrence: impl Into<String>) -> Self {
        self.request = self.request.query("recurrence", recurrence.into());
        self
    }

    /// Execute the request.
    pub async fn send(self) -> Result<EventsPagination, GammaError> {
        self.request.send().await
    }
}

/// Request builder for [`Events::list_results`].
pub struct ListEventResults {
    request: Request<Vec<Event>, GammaError>,
}

impl ListEventResults {
    /// Limit the number of results.
    pub fn limit(mut self, limit: u32) -> Self {
        self.request = self.request.query("limit", limit);
        self
    }

    /// Pagination offset.
    pub fn offset(mut self, offset: u32) -> Self {
        self.request = self.request.query("offset", offset);
        self
    }

    /// Comma-separated list of fields to order by.
    pub fn order(mut self, order: impl Into<String>) -> Self {
        self.request = self.request.query("order", order.into());
        self
    }

    /// Sort direction.
    pub fn ascending(mut self, ascending: bool) -> Self {
        self.request = self.request.query("ascending", ascending);
        self
    }

    /// Execute the request.
    pub async fn send(self) -> Result<Vec<Event>, GammaError> {
        self.request.send().await
    }
}

/// Request builder for [`Events::list_keyset`].
pub struct ListKeysetEvents {
    request: Request<KeysetEventsResponse, GammaError>,
}

impl ListKeysetEvents {
    /// Maximum number of results to return (upstream max 500).
    pub fn limit(mut self, limit: u32) -> Self {
        self.request = self.request.query("limit", limit);
        self
    }

    /// Comma-separated list of JSON field names to order by.
    pub fn order(mut self, order: impl Into<String>) -> Self {
        self.request = self.request.query("order", order.into());
        self
    }

    /// Sort direction (used only when `order` is set).
    pub fn ascending(mut self, ascending: bool) -> Self {
        self.request = self.request.query("ascending", ascending);
        self
    }

    /// Opaque cursor token returned as `next_cursor` from a previous response.
    pub fn after_cursor(mut self, cursor: impl Into<String>) -> Self {
        self.request = self.request.query("after_cursor", cursor.into());
        self
    }

    /// Filter by specific event IDs.
    pub fn id(mut self, ids: impl IntoIterator<Item = i64>) -> Self {
        self.request = self.request.query_many("id", ids);
        self
    }

    /// Filter by event slugs.
    pub fn slug(mut self, slugs: impl IntoIterator<Item = impl ToString>) -> Self {
        self.request = self.request.query_many("slug", slugs);
        self
    }

    /// Filter by closed status.
    pub fn closed(mut self, closed: bool) -> Self {
        self.request = self.request.query("closed", closed);
        self
    }

    /// Filter live events only.
    pub fn live(mut self, live: bool) -> Self {
        self.request = self.request.query("live", live);
        self
    }

    /// Filter featured events only.
    pub fn featured(mut self, featured: bool) -> Self {
        self.request = self.request.query("featured", featured);
        self
    }

    /// Search by event title substring.
    pub fn title_search(mut self, query: impl Into<String>) -> Self {
        self.request = self.request.query("title_search", query.into());
        self
    }

    /// Filter by tag IDs.
    pub fn tag_id(mut self, tag_ids: impl IntoIterator<Item = i64>) -> Self {
        self.request = self.request.query_many("tag_id", tag_ids);
        self
    }

    /// Filter by tag slug.
    pub fn tag_slug(mut self, slug: impl Into<String>) -> Self {
        self.request = self.request.query("tag_slug", slug.into());
        self
    }

    /// Set minimum liquidity threshold.
    pub fn liquidity_min(mut self, min: f64) -> Self {
        self.request = self.request.query("liquidity_min", min);
        self
    }

    /// Set maximum liquidity threshold.
    pub fn liquidity_max(mut self, max: f64) -> Self {
        self.request = self.request.query("liquidity_max", max);
        self
    }

    /// Set minimum trading volume.
    pub fn volume_min(mut self, min: f64) -> Self {
        self.request = self.request.query("volume_min", min);
        self
    }

    /// Set maximum trading volume.
    pub fn volume_max(mut self, max: f64) -> Self {
        self.request = self.request.query("volume_max", max);
        self
    }

    /// Execute the request.
    pub async fn send(self) -> Result<KeysetEventsResponse, GammaError> {
        self.request.send().await
    }
}

/// Request builder for getting a single event
pub struct GetEvent {
    request: Request<Event, GammaError>,
}

impl GetEvent {
    /// Include chat data in response
    pub fn include_chat(mut self, include: bool) -> Self {
        self.request = self.request.query("include_chat", include);
        self
    }

    /// Include template data in response
    pub fn include_template(mut self, include: bool) -> Self {
        self.request = self.request.query("include_template", include);
        self
    }

    /// Execute the request
    pub async fn send(self) -> Result<Event, GammaError> {
        self.request.send().await
    }
}

/// Request builder for listing events
pub struct ListEvents {
    request: Request<Vec<Event>, GammaError>,
}

impl ListEvents {
    /// Set maximum number of results (minimum: 0)
    pub fn limit(mut self, limit: u32) -> Self {
        self.request = self.request.query("limit", limit);
        self
    }

    /// Set pagination offset (minimum: 0)
    pub fn offset(mut self, offset: u32) -> Self {
        self.request = self.request.query("offset", offset);
        self
    }

    /// Set order fields (comma-separated list)
    pub fn order(mut self, order: impl Into<String>) -> Self {
        self.request = self.request.query("order", order.into());
        self
    }

    /// Set sort direction
    pub fn ascending(mut self, ascending: bool) -> Self {
        self.request = self.request.query("ascending", ascending);
        self
    }

    /// Filter by specific event IDs
    ///
    /// Safe batch size: ≤ 400 per request. URLs over ~8 KB are rejected
    /// upstream with `414 URI Too Long`.
    pub fn id(mut self, ids: impl IntoIterator<Item = i64>) -> Self {
        self.request = self.request.query_many("id", ids);
        self
    }

    /// Filter by tag identifier
    pub fn tag_id(mut self, tag_id: i64) -> Self {
        self.request = self.request.query("tag_id", tag_id);
        self
    }

    /// Exclude events with specified tag IDs
    ///
    /// Safe batch size: ≤ 500 per request. Tag IDs are short integers
    /// (~5 B/entry); URLs over ~8 KB are rejected upstream with `414`.
    pub fn exclude_tag_id(mut self, tag_ids: impl IntoIterator<Item = i64>) -> Self {
        self.request = self.request.query_many("exclude_tag_id", tag_ids);
        self
    }

    /// Filter by event slugs
    ///
    /// Safe batch size: ≤ 100 per request. URL length is capped at ~8 KB
    /// upstream; slug entries vary so pick a cap based on your longest slug.
    pub fn slug(mut self, slugs: impl IntoIterator<Item = impl ToString>) -> Self {
        self.request = self.request.query_many("slug", slugs);
        self
    }

    /// Filter by tag slug
    pub fn tag_slug(mut self, slug: impl Into<String>) -> Self {
        self.request = self.request.query("tag_slug", slug.into());
        self
    }

    /// Include related tags in response
    pub fn related_tags(mut self, include: bool) -> Self {
        self.request = self.request.query("related_tags", include);
        self
    }

    /// Filter active events only
    pub fn active(mut self, active: bool) -> Self {
        self.request = self.request.query("active", active);
        self
    }

    /// Filter archived events
    pub fn archived(mut self, archived: bool) -> Self {
        self.request = self.request.query("archived", archived);
        self
    }

    /// Filter featured events
    pub fn featured(mut self, featured: bool) -> Self {
        self.request = self.request.query("featured", featured);
        self
    }

    /// Filter create-your-own-market events
    pub fn cyom(mut self, cyom: bool) -> Self {
        self.request = self.request.query("cyom", cyom);
        self
    }

    /// Include chat data in response
    pub fn include_chat(mut self, include: bool) -> Self {
        self.request = self.request.query("include_chat", include);
        self
    }

    /// Include template data
    pub fn include_template(mut self, include: bool) -> Self {
        self.request = self.request.query("include_template", include);
        self
    }

    /// Filter by recurrence pattern
    pub fn recurrence(mut self, recurrence: impl Into<String>) -> Self {
        self.request = self.request.query("recurrence", recurrence.into());
        self
    }

    /// Filter closed events
    pub fn closed(mut self, closed: bool) -> Self {
        self.request = self.request.query("closed", closed);
        self
    }

    /// Set minimum liquidity threshold
    pub fn liquidity_min(mut self, min: f64) -> Self {
        self.request = self.request.query("liquidity_min", min);
        self
    }

    /// Set maximum liquidity threshold
    pub fn liquidity_max(mut self, max: f64) -> Self {
        self.request = self.request.query("liquidity_max", max);
        self
    }

    /// Set minimum trading volume
    pub fn volume_min(mut self, min: f64) -> Self {
        self.request = self.request.query("volume_min", min);
        self
    }

    /// Set maximum trading volume
    pub fn volume_max(mut self, max: f64) -> Self {
        self.request = self.request.query("volume_max", max);
        self
    }

    /// Set earliest start date (ISO 8601 format)
    pub fn start_date_min(mut self, date: impl Into<String>) -> Self {
        self.request = self.request.query("start_date_min", date.into());
        self
    }

    /// Set latest start date (ISO 8601 format)
    pub fn start_date_max(mut self, date: impl Into<String>) -> Self {
        self.request = self.request.query("start_date_max", date.into());
        self
    }

    /// Set earliest end date (ISO 8601 format)
    pub fn end_date_min(mut self, date: impl Into<String>) -> Self {
        self.request = self.request.query("end_date_min", date.into());
        self
    }

    /// Set latest end date (ISO 8601 format)
    pub fn end_date_max(mut self, date: impl Into<String>) -> Self {
        self.request = self.request.query("end_date_max", date.into());
        self
    }

    /// Execute the request
    pub async fn send(self) -> Result<Vec<Event>, GammaError> {
        self.request.send().await
    }
}

#[cfg(test)]
mod tests {
    use crate::Gamma;

    fn gamma() -> Gamma {
        Gamma::new().unwrap()
    }

    /// Verify that all event builder methods chain correctly
    #[test]
    fn test_list_events_full_chain() {
        let _list = gamma()
            .events()
            .list()
            .limit(10)
            .offset(20)
            .order("volume")
            .ascending(true)
            .id(vec![1i64, 2])
            .tag_id(42)
            .exclude_tag_id(vec![99i64])
            .slug(vec!["slug-a"])
            .tag_slug("politics")
            .related_tags(true)
            .active(true)
            .archived(false)
            .featured(true)
            .cyom(false)
            .include_chat(true)
            .include_template(false)
            .recurrence("daily")
            .closed(false)
            .liquidity_min(1000.0)
            .liquidity_max(50000.0)
            .volume_min(100.0)
            .volume_max(10000.0)
            .start_date_min("2024-01-01")
            .start_date_max("2025-01-01")
            .end_date_min("2024-06-01")
            .end_date_max("2025-12-31");
    }

    #[test]
    fn test_get_event_accepts_str_and_string() {
        let _req1 = gamma().events().get("evt-123");
        let _req2 = gamma().events().get(String::from("evt-123"));
    }

    #[test]
    fn test_get_by_slug_accepts_str_and_string() {
        let _req1 = gamma().events().get_by_slug("slug");
        let _req2 = gamma().events().get_by_slug(String::from("slug"));
    }

    #[test]
    fn test_get_event_with_query_params() {
        let _req = gamma()
            .events()
            .get("evt-123")
            .include_chat(true)
            .include_template(false);
    }

    #[test]
    fn test_event_tags_accepts_str_and_string() {
        let _req1 = gamma().events().tags("evt-123");
        let _req2 = gamma().events().tags(String::from("evt-123"));
    }

    #[test]
    fn test_event_tweet_count() {
        let _req = gamma().events().tweet_count("evt-123");
    }

    #[test]
    fn test_event_comment_count() {
        let _req = gamma().events().comment_count("evt-123");
    }

    #[test]
    fn test_list_creators_full_chain() {
        let _req = gamma()
            .events()
            .list_creators()
            .limit(10)
            .offset(0)
            .order("createdAt")
            .ascending(true)
            .creator_name("poly")
            .creator_handle("polymarket");
    }

    #[test]
    fn test_get_creator_accepts_str_and_string() {
        let _req1 = gamma().events().get_creator("c-1");
        let _req2 = gamma().events().get_creator(String::from("c-1"));
    }

    #[test]
    fn test_list_paginated_full_chain() {
        let _req = gamma()
            .events()
            .list_paginated()
            .limit(25)
            .offset(50)
            .order("startDate")
            .ascending(false)
            .include_chat(false)
            .include_template(true)
            .recurrence("daily");
    }

    #[test]
    fn test_list_results_full_chain() {
        let _req = gamma()
            .events()
            .list_results()
            .limit(5)
            .offset(0)
            .order("endDate")
            .ascending(true);
    }

    #[test]
    fn test_list_keyset_full_chain() {
        let _req = gamma()
            .events()
            .list_keyset()
            .limit(50)
            .order("volume_num")
            .ascending(true)
            .after_cursor("abc")
            .id(vec![1i64, 2])
            .slug(vec!["slug-a"])
            .closed(false)
            .live(true)
            .featured(true)
            .title_search("bitcoin")
            .tag_id(vec![42i64])
            .tag_slug("politics")
            .liquidity_min(0.0)
            .liquidity_max(1e6)
            .volume_min(0.0)
            .volume_max(1e6);
    }
}