polymarket-sdk 0.0.5

Rust SDK for Polymarket prediction markets - REST APIs, WebSocket streams, order signing, and Safe wallet integration
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
//! Gamma API Client
//!
//! Provides access to Polymarket's Gamma API for market discovery and metadata.
//!
//! ## Example
//!
//! ```rust,ignore
//! use polymarket_sdk::gamma::{GammaClient, GammaConfig};
//!
//! let client = GammaClient::new(GammaConfig::default())?;
//!
//! // Get all active markets
//! let markets = client.get_markets(None).await?;
//!
//! // Get a specific market by condition ID
//! let market = client.get_market("0x...").await?;
//!
//! // Get events
//! let events = client.get_events(None).await?;
//! ```

use std::time::Duration;

use reqwest::Client;
use serde::Deserialize;
use tracing::{debug, info, instrument};
use url::Url;

use crate::core::gamma_api_url;
use crate::core::{PolymarketError, Result};
use crate::types::{Event, ListParams, Market, Tag};

/// Gamma API configuration
#[derive(Debug, Clone)]
pub struct GammaConfig {
    /// Base URL for the Gamma API
    pub base_url: String,
    /// Request timeout
    pub timeout: Duration,
    /// User agent string
    pub user_agent: String,
}

impl Default for GammaConfig {
    fn default() -> Self {
        Self {
            // Use helper function to support env var override (POLYMARKET_GAMMA_URL)
            base_url: gamma_api_url(),
            timeout: Duration::from_secs(30),
            user_agent: "polymarket-sdk/0.1.0".to_string(),
        }
    }
}

impl GammaConfig {
    /// Create a new configuration builder
    #[must_use]
    pub fn builder() -> Self {
        Self::default()
    }

    /// Set base URL
    #[must_use]
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Set request timeout
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set user agent string
    #[must_use]
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// Create config from environment variables.
    ///
    /// **Deprecated**: Use `GammaConfig::default()` instead.
    /// The default implementation already supports `POLYMARKET_GAMMA_URL` env var override.
    #[must_use]
    #[deprecated(
        since = "0.1.0",
        note = "Use GammaConfig::default() instead. URL override via POLYMARKET_GAMMA_URL env var is already supported."
    )]
    pub fn from_env() -> Self {
        Self::default()
    }
}

/// Gamma API client
#[derive(Debug, Clone)]
pub struct GammaClient {
    config: GammaConfig,
    client: Client,
}

impl GammaClient {
    /// Create a new Gamma client
    pub fn new(config: GammaConfig) -> Result<Self> {
        let client = Client::builder()
            .timeout(config.timeout)
            .user_agent(&config.user_agent)
            .gzip(true)
            .build()
            .map_err(|e| PolymarketError::config(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self { config, client })
    }

    /// Create client with default configuration
    pub fn with_defaults() -> Result<Self> {
        Self::new(GammaConfig::default())
    }

    /// Create client from environment variables.
    ///
    /// **Deprecated**: Use `GammaClient::with_defaults()` instead.
    #[deprecated(since = "0.1.0", note = "Use GammaClient::with_defaults() instead")]
    #[allow(deprecated)]
    pub fn from_env() -> Result<Self> {
        Self::new(GammaConfig::from_env())
    }

    /// Get all markets with optional filtering
    #[instrument(skip(self), level = "debug")]
    pub async fn get_markets(&self, params: Option<ListParams>) -> Result<Vec<Market>> {
        let mut url = format!("{}/markets", self.config.base_url);
        let query_params = self.build_list_query_params(params);

        if !query_params.is_empty() {
            url.push('?');
            url.push_str(&query_params);
        }

        debug!(%url, "Fetching markets");

        let response = self.client.get(&url).send().await?;
        self.handle_response::<Vec<Market>>(response).await
    }

    /// Get a specific market by condition ID
    #[instrument(skip(self), level = "debug")]
    pub async fn get_market(&self, condition_id: &str) -> Result<Option<Market>> {
        let url = format!("{}/markets/{}", self.config.base_url, condition_id);
        debug!(%url, "Fetching market");

        let response = self.client.get(&url).send().await?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        }

        self.handle_response::<Market>(response).await.map(Some)
    }

    /// Search markets by query
    #[instrument(skip(self), level = "debug")]
    pub async fn search_markets(&self, query: &str, limit: Option<u32>) -> Result<Vec<Market>> {
        let mut url = format!(
            "{}/markets?search={}",
            self.config.base_url,
            urlencoding::encode(query)
        );

        if let Some(limit) = limit {
            url.push_str(&format!("&limit={limit}"));
        }

        debug!(%url, "Searching markets");

        let response = self.client.get(&url).send().await?;
        self.handle_response::<Vec<Market>>(response).await
    }

    /// Get all events with optional filtering
    #[instrument(skip(self), level = "debug")]
    pub async fn get_events(&self, params: Option<ListParams>) -> Result<Vec<Event>> {
        let mut url = format!("{}/events", self.config.base_url);
        let query_params = self.build_list_query_params(params);

        if !query_params.is_empty() {
            url.push('?');
            url.push_str(&query_params);
        }

        debug!(%url, "Fetching events");

        let response = self.client.get(&url).send().await?;
        self.handle_response::<Vec<Event>>(response).await
    }

    /// Get a specific event by ID
    #[instrument(skip(self), level = "debug")]
    pub async fn get_event(&self, event_id: &str) -> Result<Option<Event>> {
        let url = format!("{}/events/{}", self.config.base_url, event_id);
        debug!(%url, "Fetching event");

        let response = self.client.get(&url).send().await?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        }

        self.handle_response::<Event>(response).await.map(Some)
    }

    /// Get tags with optional pagination
    #[instrument(skip(self), level = "debug")]
    pub async fn get_tags_paginated(&self, limit: u32, offset: u32) -> Result<Vec<Tag>> {
        let url = format!(
            "{}/tags?limit={}&offset={}",
            self.config.base_url, limit, offset
        );
        debug!(%url, "Fetching tags page");

        let response = self.client.get(&url).send().await?;
        self.handle_response::<Vec<Tag>>(response).await
    }

    /// Get all tags (single page, for backwards compatibility)
    #[instrument(skip(self), level = "debug")]
    pub async fn get_tags(&self) -> Result<Vec<Tag>> {
        // For backwards compatibility, get all tags with pagination
        self.get_all_tags().await
    }

    /// Get all tags using pagination loop
    ///
    /// Fetches all tags from the Gamma API using pagination.
    /// Page size is 300 (maximum allowed by API).
    #[instrument(skip(self), level = "debug")]
    pub async fn get_all_tags(&self) -> Result<Vec<Tag>> {
        let mut all_tags = Vec::new();
        let page_size = 300u32; // Maximum page size
        let mut offset = 0u32;

        tracing::info!(
            "Starting to fetch all tags with pagination (page_size={})",
            page_size
        );

        loop {
            tracing::debug!(
                "Fetching tags page {} (offset={})",
                offset / page_size + 1,
                offset
            );

            let tags = self.get_tags_paginated(page_size, offset).await?;
            let count = tags.len();

            if count == 0 {
                tracing::debug!("Reached last page, stopping pagination");
                break;
            }

            tracing::info!(
                "Page {}: fetched {} tags, total so far: {}",
                offset / page_size + 1,
                count,
                all_tags.len() + count
            );

            all_tags.extend(tags);

            // If we got less than page_size, we've reached the last page
            if count < page_size as usize {
                tracing::debug!("Last page reached (got {} < {})", count, page_size);
                break;
            }

            offset += page_size;
        }

        tracing::info!("Finished fetching all tags, total: {}", all_tags.len());
        Ok(all_tags)
    }

    /// Get markets by tag
    #[instrument(skip(self), level = "debug")]
    pub async fn get_markets_by_tag(
        &self,
        tag_slug: &str,
        params: Option<ListParams>,
    ) -> Result<Vec<Market>> {
        let mut url = format!("{}/markets?tag_slug={}", self.config.base_url, tag_slug);

        if let Some(params) = params {
            if let Some(limit) = params.pagination.limit {
                url.push_str(&format!("&limit={limit}"));
            }
            if let Some(offset) = params.pagination.offset {
                url.push_str(&format!("&offset={offset}"));
            }
        }

        debug!(%url, "Fetching markets by tag");

        let response = self.client.get(&url).send().await?;
        self.handle_response::<Vec<Market>>(response).await
    }

    /// Get markets by CLOB token IDs (asset IDs)
    ///
    /// Fetches market information for the given list of CLOB token IDs.
    /// Useful for enriching feed/activity data with market details.
    ///
    /// # Arguments
    /// * `clob_token_ids` - List of CLOB token IDs (asset IDs) to query
    ///
    /// # Example
    /// ```rust,ignore
    /// let client = GammaClient::with_defaults()?;
    /// let token_ids = vec![
    ///     "21742633143463906290569050155826241533067272736897614950488156847949938836455".to_string(),
    ///     "48331043336612883890938759509493159234755048973500640148014422747788308965732".to_string(),
    /// ];
    /// let markets = client.get_markets_by_clob_token_ids(&token_ids).await?;
    /// ```
    #[instrument(skip(self, clob_token_ids), level = "debug", fields(token_count = clob_token_ids.len()))]
    pub async fn get_markets_by_clob_token_ids(
        &self,
        clob_token_ids: &[String],
    ) -> Result<Vec<Market>> {
        if clob_token_ids.is_empty() {
            return Ok(vec![]);
        }

        // Build URL with repeated clob_token_ids params (clob_token_ids=...&clob_token_ids=...)
        let mut url = Url::parse(&format!("{}/markets", self.config.base_url))?;
        {
            let mut pairs = url.query_pairs_mut();
            for token in clob_token_ids {
                pairs.append_pair("clob_token_ids", token);
            }
        }

        let url_str = url.to_string();
        debug!(%url_str, token_count = clob_token_ids.len(), "Fetching markets by CLOB token IDs");
        info!(%url_str, token_count = clob_token_ids.len(), "Gamma get_markets_by_clob_token_ids request");

        let response = self.client.get(url).send().await?;
        self.handle_response::<Vec<Market>>(response).await
    }

    /// Get markets by CLOB token IDs with batching support
    ///
    /// For large lists of token IDs, this method batches requests to avoid
    /// URL length limits. Default batch size is 50 tokens per request.
    ///
    /// # Arguments
    /// * `clob_token_ids` - List of CLOB token IDs to query
    /// * `batch_size` - Optional batch size (default: 50)
    #[instrument(skip(self, clob_token_ids), level = "debug", fields(token_count = clob_token_ids.len()))]
    pub async fn get_markets_by_clob_token_ids_batched(
        &self,
        clob_token_ids: &[String],
        batch_size: Option<usize>,
    ) -> Result<Vec<Market>> {
        if clob_token_ids.is_empty() {
            return Ok(vec![]);
        }

        let batch_size = batch_size.unwrap_or(50);
        let mut all_markets = Vec::new();

        for chunk in clob_token_ids.chunks(batch_size) {
            let markets = self.get_markets_by_clob_token_ids(chunk).await?;
            all_markets.extend(markets);
        }

        Ok(all_markets)
    }

    // =========================================================================
    // Public Search API
    // =========================================================================

    /// Search Polymarket via /public-search endpoint
    ///
    /// This endpoint searches across events, profiles (traders), and tags.
    ///
    /// # Arguments
    /// * `request` - Search request parameters
    ///
    /// # Example
    /// ```rust,ignore
    /// use polymarket_sdk::gamma::GammaClient;
    /// use polymarket_sdk::types::SearchRequest;
    ///
    /// let client = GammaClient::with_defaults()?;
    /// let response = client.public_search(SearchRequest::new("bitcoin")
    ///     .with_limit(10)
    ///     .with_profiles(true)
    /// ).await?;
    ///
    /// println!("Found {} profiles", response.profiles.len());
    /// ```
    #[instrument(skip(self), level = "debug")]
    pub async fn public_search(
        &self,
        request: crate::types::SearchRequest,
    ) -> Result<crate::types::SearchResponse> {
        let url = format!("{}/public-search", self.config.base_url);

        debug!(
            %url,
            query = %request.q,
            limit = ?request.limit_per_type,
            "Performing public search"
        );

        let response = self.client.get(&url).query(&request).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let text = response.text().await?;

        // Debug log response preview
        if text.len() > 500 {
            debug!("Search response (first 500 chars): {}", &text[..500]);
        } else {
            debug!("Search response: {}", text);
        }

        let search_response: crate::types::SearchResponse =
            serde_json::from_str(&text).map_err(|e| {
                tracing::error!("Failed to parse search response: {}", e);
                PolymarketError::parse_with_source(format!("Search JSON parse error: {e}"), e)
            })?;

        Ok(search_response)
    }

    /// Search for traders/profiles only
    ///
    /// Convenience method that calls public_search with profiles enabled.
    ///
    /// # Arguments
    /// * `query` - Search query string
    /// * `limit` - Maximum number of results
    #[instrument(skip(self), level = "debug")]
    pub async fn search_traders(
        &self,
        query: &str,
        limit: u32,
    ) -> Result<Vec<crate::types::SearchProfile>> {
        let request = crate::types::SearchRequest::new(query)
            .with_limit(limit)
            .with_profiles(true)
            .with_tags(false);

        let response = self.public_search(request).await?;
        Ok(response.profiles)
    }

    /// Search for all types (events, profiles, tags)
    ///
    /// # Arguments
    /// * `query` - Search query string
    /// * `limit` - Maximum number of results per type
    #[instrument(skip(self), level = "debug")]
    pub async fn search_all(
        &self,
        query: &str,
        limit: u32,
    ) -> Result<crate::types::SearchResponse> {
        let request = crate::types::SearchRequest::new(query)
            .with_limit(limit)
            .with_profiles(true)
            .with_tags(true);

        self.public_search(request).await
    }

    // =========================================================================
    // Helper Methods
    // =========================================================================

    /// Build query parameters from ListParams
    fn build_list_query_params(&self, params: Option<ListParams>) -> String {
        let Some(params) = params else {
            return String::new();
        };

        let mut query_parts = Vec::new();

        if let Some(limit) = params.pagination.limit {
            query_parts.push(format!("limit={limit}"));
        }
        if let Some(offset) = params.pagination.offset {
            query_parts.push(format!("offset={offset}"));
        }
        if let Some(closed) = params.closed {
            query_parts.push(format!("closed={closed}"));
        }
        if let Some(active) = params.active {
            query_parts.push(format!("active={active}"));
        }
        if let Some(order) = params.order {
            query_parts.push(format!("order={order}"));
        }
        if let Some(ascending) = params.ascending {
            query_parts.push(format!("ascending={ascending}"));
        }

        query_parts.join("&")
    }

    /// Handle API response
    async fn handle_response<T: for<'de> Deserialize<'de>>(
        &self,
        response: reqwest::Response,
    ) -> Result<T> {
        let status = response.status();

        if status.is_success() {
            let body = response.text().await?;
            serde_json::from_str(&body).map_err(|e| {
                PolymarketError::parse_with_source(format!("Failed to parse response: {e}"), e)
            })
        } else {
            let body = response.text().await.unwrap_or_default();
            Err(PolymarketError::api(status.as_u16(), body))
        }
    }
}

/// Response wrapper for paginated results
#[derive(Debug, Deserialize)]
pub struct PaginatedResponse<T> {
    /// Results
    pub data: Vec<T>,
    /// Total count
    pub count: Option<i64>,
    /// Limit used
    pub limit: Option<i64>,
    /// Next cursor for pagination
    pub next_cursor: Option<String>,
}

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

    #[test]
    fn test_config_builder() {
        let config = GammaConfig::builder()
            .with_base_url("https://custom.example.com")
            .with_timeout(Duration::from_secs(60));

        assert_eq!(config.base_url, "https://custom.example.com");
        assert_eq!(config.timeout, Duration::from_secs(60));
    }

    #[test]
    fn test_query_params_building() {
        let client = GammaClient::with_defaults().unwrap();

        let params = ListParams::new()
            .with_limit(10)
            .with_offset(20)
            .with_closed(false);

        let query = client.build_list_query_params(Some(params));
        assert!(query.contains("limit=10"));
        assert!(query.contains("offset=20"));
        assert!(query.contains("closed=false"));
    }
}