nautilus-polymarket 0.59.0

Polymarket integration adapter for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Instrument provider for the Polymarket adapter.

use std::{collections::HashMap, fmt::Debug, sync::Arc};

use ahash::{AHashMap, AHashSet};
use async_trait::async_trait;
use nautilus_common::providers::{InstrumentProvider, InstrumentStore};
use nautilus_model::{
    identifiers::InstrumentId,
    instruments::{Instrument, InstrumentAny},
};
use ustr::Ustr;

use crate::{
    common::consts::GAMMA_CONDITION_IDS_BATCH_SIZE,
    config::PolymarketInstrumentProviderConfig,
    filters::InstrumentFilter,
    http::{gamma::PolymarketGammaHttpClient, models::GammaTag, query::GetGammaMarketsParams},
};

/// Provides Polymarket instruments via the Gamma API.
///
/// Wraps [`PolymarketGammaHttpClient`] with an [`InstrumentStore`] and a
/// token_id index for resolving WebSocket asset IDs to instruments.
///
/// Optional [`InstrumentFilter`]s control which instruments are loaded
/// during `load_all()`. Without filters, all active markets are fetched.
pub struct PolymarketInstrumentProvider {
    store: InstrumentStore,
    http_client: PolymarketGammaHttpClient,
    token_index: AHashMap<Ustr, InstrumentId>,
    filters: Vec<Arc<dyn InstrumentFilter>>,
    config: PolymarketInstrumentProviderConfig,
}

impl Debug for PolymarketInstrumentProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(PolymarketInstrumentProvider))
            .field("store", &self.store)
            .field("http_client", &self.http_client)
            .field("token_index_len", &self.token_index.len())
            .field("filters", &self.filters)
            .field("config", &self.config)
            .finish()
    }
}

impl PolymarketInstrumentProvider {
    /// Creates a new [`PolymarketInstrumentProvider`] with an empty store and no filters.
    #[must_use]
    pub fn new(
        http_client: PolymarketGammaHttpClient,
        config: Option<PolymarketInstrumentProviderConfig>,
    ) -> Self {
        Self {
            store: InstrumentStore::new(),
            http_client,
            token_index: AHashMap::new(),
            filters: Vec::new(),
            config: config.unwrap_or_default(),
        }
    }

    /// Creates a new [`PolymarketInstrumentProvider`] with multiple filters.
    #[must_use]
    pub fn with_filters(
        http_client: PolymarketGammaHttpClient,
        config: Option<PolymarketInstrumentProviderConfig>,
        filters: Vec<Arc<dyn InstrumentFilter>>,
    ) -> Self {
        Self {
            store: InstrumentStore::new(),
            http_client,
            token_index: AHashMap::new(),
            filters,
            config: config.unwrap_or_default(),
        }
    }

    /// Creates a new [`PolymarketInstrumentProvider`] with a single filter.
    #[must_use]
    pub fn with_filter(
        http_client: PolymarketGammaHttpClient,
        config: Option<PolymarketInstrumentProviderConfig>,
        filter: Arc<dyn InstrumentFilter>,
    ) -> Self {
        Self {
            store: InstrumentStore::new(),
            http_client,
            token_index: AHashMap::new(),
            filters: vec![filter],
            config: config.unwrap_or_default(),
        }
    }

    /// Adds an instrument filter for subsequent `load_all()` calls.
    pub fn add_filter(&mut self, filter: Arc<dyn InstrumentFilter>) {
        self.filters.push(filter);
    }

    /// Clears all instrument filters, reverting to bulk load behavior.
    pub fn clear_filters(&mut self) {
        self.filters.clear();
    }

    /// Returns the instrument for the given token ID, if found.
    #[must_use]
    pub fn get_by_token_id(&self, token_id: &Ustr) -> Option<&InstrumentAny> {
        let instrument_id = self.token_index.get(token_id)?;
        self.store.find(instrument_id)
    }

    /// Builds a frozen snapshot mapping token IDs to instruments.
    ///
    /// Used to provide the WS handler task with a read-only lookup
    /// table after instruments have been loaded.
    #[must_use]
    pub fn build_token_map(&self) -> AHashMap<Ustr, InstrumentAny> {
        self.token_index
            .iter()
            .filter_map(|(token_id, instrument_id)| {
                self.store
                    .find(instrument_id)
                    .map(|inst| (*token_id, inst.clone()))
            })
            .collect()
    }

    /// Loads instruments for the given slugs additively into the store.
    ///
    /// Unlike [`Self::load_all`], this does **not** clear existing instruments or
    /// mark the store as initialized, allowing incremental loading of
    /// slug-based markets alongside bulk data.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP request or parsing fails.
    pub async fn load_by_slugs(&mut self, slugs: Vec<String>) -> anyhow::Result<()> {
        let instruments = self.http_client.request_instruments_by_slugs(slugs).await?;

        for instrument in &instruments {
            self.token_index.insert(
                Ustr::from(instrument.raw_symbol().as_str()),
                instrument.id(),
            );
        }

        self.store.add_bulk(instruments);

        Ok(())
    }

    /// Returns a clone of the configured instrument filters.
    #[must_use]
    pub fn filters(&self) -> Vec<Arc<dyn InstrumentFilter>> {
        self.filters.clone()
    }

    /// Returns a reference to the underlying HTTP client.
    #[must_use]
    pub fn http_client(&self) -> &PolymarketGammaHttpClient {
        &self.http_client
    }

    /// Returns the configured provider config.
    #[must_use]
    pub fn config(&self) -> &PolymarketInstrumentProviderConfig {
        &self.config
    }

    /// Fetches available tags from the Gamma API.
    pub async fn list_tags(&self) -> anyhow::Result<Vec<GammaTag>> {
        self.http_client.request_tags().await
    }

    pub fn add_instruments(&mut self, instruments: Vec<InstrumentAny>) {
        for inst in &instruments {
            self.token_index
                .insert(Ustr::from(inst.raw_symbol().as_str()), inst.id());
        }
        self.store.add_bulk(instruments);
    }

    /// Loads instruments for the given event slugs additively into the store.
    ///
    /// Unlike [`Self::load_all`], this does **not** clear existing instruments or
    /// mark the store as initialized, allowing incremental loading of
    /// event-scoped markets alongside bulk data.
    pub async fn load_by_event_slugs(&mut self, slugs: Vec<String>) -> anyhow::Result<()> {
        let instruments = self
            .http_client
            .request_instruments_by_event_slugs(slugs)
            .await?;
        self.add_instruments(instruments);
        Ok(())
    }

    /// Initializes the provider using its configured bootstrap scope.
    pub async fn initialize(&mut self, reload: bool) -> anyhow::Result<()> {
        if self.store.is_initialized() && !reload {
            return Ok(());
        }

        if self.config.should_load_all() {
            self.load_scoped_all().await?;
            self.store.set_initialized();
            return Ok(());
        }

        if self.config.has_load_ids() {
            let load_ids = self.config.load_ids.clone().unwrap_or_default();
            let filters = self.config.filters.clone();
            self.load_ids(&load_ids, filters.as_ref()).await?;
            self.store.set_initialized();
            return Ok(());
        }

        if self.config.log_warnings {
            log::warn!(
                "No Polymarket instrument bootstrap configured: set instrument_config.load_all, instrument_config.load_ids, instrument_config.event_slugs, instrument_config.market_slugs, or instrument_config.event_slug_builder"
            );
        }
        Ok(())
    }

    async fn load_scoped_all(&mut self) -> anyhow::Result<()> {
        let has_explicit_slug_scope = self.config.event_slug_builder.is_some()
            || self
                .config
                .event_slugs
                .as_ref()
                .is_some_and(|slugs| !slugs.is_empty())
            || self
                .config
                .market_slugs
                .as_ref()
                .is_some_and(|slugs| !slugs.is_empty());
        let event_slugs = self.resolve_event_slugs()?;
        let market_slugs = self
            .config
            .market_slugs
            .clone()
            .unwrap_or_default()
            .into_iter()
            .filter(|slug| !slug.trim().is_empty())
            .collect::<Vec<_>>();

        if !event_slugs.is_empty() {
            self.load_by_event_slugs(event_slugs).await?;
        }

        if !market_slugs.is_empty() {
            self.load_by_slugs(market_slugs).await?;
        }

        if has_explicit_slug_scope {
            return Ok(());
        }

        let filters = self.config.filters.clone();
        self.load_all(filters.as_ref()).await
    }

    fn resolve_event_slugs(&self) -> anyhow::Result<Vec<String>> {
        if let Some(builder) = self.config.event_slug_builder.as_ref() {
            return builder.build_event_slugs();
        }

        Ok(self
            .config
            .event_slugs
            .clone()
            .unwrap_or_default()
            .into_iter()
            .filter(|slug| !slug.trim().is_empty())
            .collect())
    }

    /// Loads instruments using all configured filters, combining results from
    /// each filter's methods that return `Some`.
    async fn load_filtered(&self) -> anyhow::Result<Vec<InstrumentAny>> {
        fetch_instruments(&self.http_client, &self.filters).await
    }
}

/// Fetches instruments from the Gamma API, respecting any configured filters.
pub async fn fetch_instruments(
    http_client: &PolymarketGammaHttpClient,
    filters: &[Arc<dyn InstrumentFilter>],
) -> anyhow::Result<Vec<InstrumentAny>> {
    if filters.is_empty() {
        return http_client.request_instruments().await;
    }

    let mut instruments = Vec::new();

    for filter in filters {
        if let Some(slugs) = filter.market_slugs()
            && !slugs.is_empty()
        {
            let result = http_client.request_instruments_by_slugs(slugs).await?;
            instruments.extend(result);
        }

        if let Some(event_slugs) = filter.event_slugs()
            && !event_slugs.is_empty()
        {
            let result = http_client
                .request_instruments_by_event_slugs(event_slugs)
                .await?;
            instruments.extend(result);
        }

        if let Some(params) = filter.query_params() {
            let result = http_client.request_instruments_by_params(params).await?;
            instruments.extend(result);
        }

        if let Some(event_queries) = filter.event_queries() {
            for (event_slug, params) in event_queries {
                let result = http_client
                    .request_instruments_by_event_query(&event_slug, params)
                    .await?;
                instruments.extend(result);
            }
        }

        if let Some(params) = filter.event_params() {
            let result = http_client
                .request_instruments_by_event_params(params)
                .await?;
            instruments.extend(result);
        }

        if let Some(params) = filter.search_params() {
            let result = http_client.request_instruments_by_search(params).await?;
            instruments.extend(result);
        }
    }

    let mut seen = AHashSet::new();
    instruments.retain(|inst| seen.insert(inst.id()));
    instruments.retain(|inst| filters.iter().all(|f| f.accept(inst)));

    Ok(instruments)
}

/// Fetches instruments using the configured provider bootstrap scope without
/// mutating any provider state.
pub async fn fetch_configured_instruments(
    http_client: &PolymarketGammaHttpClient,
    config: &PolymarketInstrumentProviderConfig,
    filters: &[Arc<dyn InstrumentFilter>],
) -> anyhow::Result<Vec<InstrumentAny>> {
    let mut instruments = Vec::new();

    if config.should_load_all() {
        let has_explicit_slug_scope = config.event_slug_builder.is_some()
            || config
                .event_slugs
                .as_ref()
                .is_some_and(|slugs| !slugs.is_empty())
            || config
                .market_slugs
                .as_ref()
                .is_some_and(|slugs| !slugs.is_empty());
        let event_slugs = if let Some(builder) = config.event_slug_builder.as_ref() {
            builder.build_event_slugs()?
        } else {
            config
                .event_slugs
                .clone()
                .unwrap_or_default()
                .into_iter()
                .filter(|slug| !slug.trim().is_empty())
                .collect::<Vec<_>>()
        };

        let market_slugs = config
            .market_slugs
            .clone()
            .unwrap_or_default()
            .into_iter()
            .filter(|slug| !slug.trim().is_empty())
            .collect::<Vec<_>>();

        if !event_slugs.is_empty() {
            instruments.extend(
                http_client
                    .request_instruments_by_event_slugs(event_slugs)
                    .await?,
            );
        }

        if !market_slugs.is_empty() {
            instruments.extend(
                http_client
                    .request_instruments_by_slugs(market_slugs)
                    .await?,
            );
        }

        if has_explicit_slug_scope {
            // Explicit slug scoping should never broaden into a full-universe fetch.
        } else if filters.is_empty() {
            if let Some(map) = config.filters.as_ref() {
                if map.is_empty() {
                    instruments.extend(http_client.request_instruments().await?);
                } else {
                    let params = build_gamma_params_from_hashmap(map);
                    instruments.extend(http_client.request_instruments_by_params(params).await?);
                }
            } else {
                instruments.extend(http_client.request_instruments().await?);
            }
        } else {
            instruments.extend(fetch_instruments(http_client, filters).await?);
        }
    } else if config.has_load_ids() {
        let base_params = config
            .filters
            .as_ref()
            .map(build_gamma_params_from_hashmap)
            .unwrap_or_default();

        let condition_ids = config
            .load_ids
            .clone()
            .unwrap_or_default()
            .into_iter()
            .filter_map(|id| extract_condition_id(&id).ok())
            .collect::<AHashSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();

        for chunk in condition_ids.chunks(GAMMA_CONDITION_IDS_BATCH_SIZE) {
            let params = GetGammaMarketsParams {
                condition_ids: Some(chunk.join(",")),
                ..base_params.clone()
            };
            instruments.extend(http_client.request_instruments_by_params(params).await?);
        }
    }

    let mut seen = AHashSet::new();
    instruments.retain(|inst| seen.insert(inst.id()));
    instruments.retain(|inst| filters.iter().all(|f| f.accept(inst)));
    Ok(instruments)
}

/// Extracts the condition ID from an instrument symbol.
///
/// Polymarket instrument symbols follow the pattern `{condition_id}-{token_id}`.
/// The condition_id is a hex string (e.g. `0xabc123...`) and the token_id is a
/// large decimal number. This extracts the condition_id by splitting at the last `-`.
pub fn extract_condition_id(instrument_id: &InstrumentId) -> anyhow::Result<String> {
    let symbol = instrument_id.symbol.as_str();
    symbol
        .rfind('-')
        .map(|idx| symbol[..idx].to_string())
        .ok_or_else(|| {
            anyhow::anyhow!("Cannot extract condition_id from symbol '{symbol}': no '-' separator")
        })
}

/// Builds `GetGammaMarketsParams` from a `HashMap<String, String>`.
pub fn build_gamma_params_from_hashmap(map: &HashMap<String, String>) -> GetGammaMarketsParams {
    let mut params = GetGammaMarketsParams::default();

    if let Some(v) = map.get("active") {
        params.active = v.parse().ok();
    }

    if let Some(v) = map.get("closed") {
        params.closed = v.parse().ok();
    }

    if let Some(v) = map.get("archived") {
        params.archived = v.parse().ok();
    }

    if let Some(v) = map.get("slug") {
        params.slug = Some(v.clone());
    }

    if let Some(v) = map.get("tag_id") {
        params.tag_id = Some(v.clone());
    }

    if let Some(v) = map.get("condition_ids") {
        params.condition_ids = Some(v.clone());
    }

    if let Some(v) = map.get("clob_token_ids") {
        params.clob_token_ids = Some(v.clone());
    }

    if let Some(v) = map.get("liquidity_num_min") {
        params.liquidity_num_min = v.parse().ok();
    }

    if let Some(v) = map.get("liquidity_num_max") {
        params.liquidity_num_max = v.parse().ok();
    }

    if let Some(v) = map.get("volume_num_min") {
        params.volume_num_min = v.parse().ok();
    }

    if let Some(v) = map.get("volume_num_max") {
        params.volume_num_max = v.parse().ok();
    }

    if let Some(v) = map.get("order") {
        params.order = Some(v.clone());
    }

    if let Some(v) = map.get("ascending") {
        params.ascending = v.parse().ok();
    }

    if let Some(v) = map.get("limit") {
        params.limit = v.parse().ok();
    }

    if let Some(v) = map.get("max_markets") {
        params.max_markets = v.parse().ok();
    }

    params
}

/// Resolves a tag slug to a tag ID by querying the Gamma tags endpoint.
pub async fn resolve_tag_slug(
    client: &PolymarketGammaHttpClient,
    slug: &str,
) -> anyhow::Result<String> {
    let tags = client.request_tags().await?;
    tags.iter()
        .find(|t| t.slug.as_deref() == Some(slug))
        .map(|t| t.id.clone())
        .ok_or_else(|| anyhow::anyhow!("Tag slug '{slug}' not found"))
}

#[async_trait(?Send)]
impl InstrumentProvider for PolymarketInstrumentProvider {
    fn store(&self) -> &InstrumentStore {
        &self.store
    }

    fn store_mut(&mut self) -> &mut InstrumentStore {
        &mut self.store
    }

    async fn load_all(&mut self, filters: Option<&HashMap<String, String>>) -> anyhow::Result<()> {
        let instruments = if self.filters.is_empty() {
            // If HashMap filters are provided, convert to Gamma params
            if let Some(map) = filters {
                if map.is_empty() {
                    self.http_client.request_instruments().await?
                } else {
                    let params = build_gamma_params_from_hashmap(map);
                    self.http_client
                        .request_instruments_by_params(params)
                        .await?
                }
            } else {
                self.http_client.request_instruments().await?
            }
        } else {
            self.load_filtered().await?
        };

        self.store.clear();
        self.token_index.clear();
        self.add_instruments(instruments);
        self.store.set_initialized();

        Ok(())
    }

    async fn load_ids(
        &mut self,
        instrument_ids: &[InstrumentId],
        filters: Option<&HashMap<String, String>>,
    ) -> anyhow::Result<()> {
        let missing: Vec<_> = instrument_ids
            .iter()
            .filter(|id| !self.store.contains(id))
            .collect();

        if missing.is_empty() {
            return Ok(());
        }

        // Extract unique condition IDs from instrument symbols
        // Symbol format: "{condition_id}-{token_id}"
        let mut condition_ids: Vec<String> = missing
            .iter()
            .filter_map(|id| extract_condition_id(id).ok())
            .collect();
        condition_ids.sort();
        condition_ids.dedup();

        if condition_ids.is_empty() {
            return Ok(());
        }

        let base_params = filters
            .map(build_gamma_params_from_hashmap)
            .unwrap_or_default();

        for chunk in condition_ids.chunks(GAMMA_CONDITION_IDS_BATCH_SIZE) {
            let params = GetGammaMarketsParams {
                condition_ids: Some(chunk.join(",")),
                ..base_params.clone()
            };
            let instruments = self
                .http_client
                .request_instruments_by_params(params)
                .await?;
            self.add_instruments(instruments);
        }

        Ok(())
    }

    async fn load(
        &mut self,
        instrument_id: &InstrumentId,
        filters: Option<&HashMap<String, String>>,
    ) -> anyhow::Result<()> {
        if self.store.contains(instrument_id) {
            return Ok(());
        }

        // Try direct fetch via condition_id extracted from symbol
        if let Ok(cid) = extract_condition_id(instrument_id) {
            let params = GetGammaMarketsParams {
                condition_ids: Some(cid),
                ..Default::default()
            };

            if let Ok(instruments) = self.http_client.request_instruments_by_params(params).await {
                self.add_instruments(instruments);

                if self.store.contains(instrument_id) {
                    return Ok(());
                }
            }
        }

        // Fallback: full load_all if not initialized
        if !self.store.is_initialized() {
            self.load_all(filters).await?;
        }

        if self.store.contains(instrument_id) {
            Ok(())
        } else {
            anyhow::bail!("Instrument {instrument_id} not found on Polymarket")
        }
    }
}