Skip to main content

nautilus_polymarket/python/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings from `pyo3`.
17//!
18//! The Python v2 Polymarket boundary exposes configuration, factory registration,
19//! and a thin discovery and historical-data facade backed by Rust clients.
20
21#![expect(
22    clippy::missing_errors_doc,
23    reason = "errors documented on underlying Rust methods"
24)]
25
26pub mod config;
27pub mod factories;
28pub mod loader;
29pub mod positions;
30pub mod session;
31pub mod sort;
32
33use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
34use nautilus_core::{
35    python::{to_pyruntime_err, to_pytype_err, to_pyvalue_err},
36    string::secret::SecretString,
37};
38use nautilus_execution::{models::fee::FeeModel, python::fee::PyFeeModel};
39use nautilus_model::{
40    data::ensure_rust_extractor_registered,
41    identifiers::InstrumentId,
42    python::{instruments::pyobject_to_instrument_any, orders::pyobject_to_order_any},
43    types::{Money, Price, Quantity},
44};
45use nautilus_network::websocket::TransportBackend;
46use nautilus_system::get_global_pyo3_registry;
47use pyo3::{prelude::*, types::PyDict};
48
49use crate::{
50    common::consts::{POLYMARKET, POLYMARKET_CLIENT_ID, POLYMARKET_VENUE},
51    config::{
52        PolymarketDataClientConfig, PolymarketExecutionClientConfig,
53        PolymarketInstrumentProviderConfig, PolymarketUpDownEventSlugConfig,
54    },
55    data_types::{
56        PolymarketCryptoPrice, PolymarketCryptoTwap, PolymarketEquityPrice,
57        register_polymarket_custom_data,
58    },
59    factories::{PolymarketDataClientFactory, PolymarketExecutionClientFactory},
60    models::PolymarketFeeModel,
61};
62
63#[pyo3_stub_gen::derive::gen_stub_pymethods]
64#[pymethods]
65impl PolymarketFeeModel {
66    /// Polymarket fee model for binary-option backtests.
67    ///
68    /// Taker fills pay the market's fee-equivalent amount. Maker fills receive a
69    /// per-fill approximation of the daily maker rebate by applying the market's
70    /// configured rebate rate to that fee-equivalent amount.
71    #[new]
72    #[gen_stub(override_return_type(type_repr = "typing.Self", imports = ("typing",)))]
73    fn py_new() -> PyClassInitializer<Self> {
74        PyClassInitializer::from(PyFeeModel).add_subclass(Self)
75    }
76
77    fn __repr__(&self) -> String {
78        format!("{self:?}")
79    }
80
81    fn get_commission(
82        &self,
83        order: &Bound<'_, PyAny>,
84        fill_quantity: Quantity,
85        fill_px: Price,
86        instrument: &Bound<'_, PyAny>,
87    ) -> PyResult<Money> {
88        let py = order.py();
89        let instrument =
90            pyobject_to_instrument_any(py, instrument.clone().unbind()).map_err(|_| {
91                let type_name = instrument
92                    .get_type()
93                    .name()
94                    .map_or_else(|_| "unknown".to_string(), |name| name.to_string());
95                to_pytype_err(format!(
96                    "`instrument` must be an `Instrument`, was `{type_name}`"
97                ))
98            })?;
99        let order = pyobject_to_order_any(py, order.clone().unbind()).map_err(|_| {
100            let type_name = order
101                .get_type()
102                .name()
103                .map_or_else(|_| "unknown".to_string(), |name| name.to_string());
104            to_pytype_err(format!("`order` must be an `Order`, was `{type_name}`"))
105        })?;
106
107        FeeModel::get_commission(self, &order, fill_quantity, fill_px, &instrument)
108            .map_err(to_pyruntime_err)
109    }
110}
111
112fn getattr_optional<'py>(
113    obj: &Bound<'py, PyAny>,
114    name: &str,
115) -> PyResult<Option<Bound<'py, PyAny>>> {
116    if !obj.hasattr(name)? {
117        return Ok(None);
118    }
119
120    let value = obj.getattr(name)?;
121    if value.is_none() {
122        Ok(None)
123    } else {
124        Ok(Some(value))
125    }
126}
127
128fn getattr_optional_option_u64(
129    obj: &Bound<'_, PyAny>,
130    name: &str,
131    default: Option<u64>,
132) -> PyResult<Option<u64>> {
133    if !obj.hasattr(name)? {
134        return Ok(default);
135    }
136
137    let value = obj.getattr(name)?;
138    if value.is_none() {
139        Ok(None)
140    } else {
141        value.extract::<u64>().map(Some)
142    }
143}
144
145fn py_scalar_to_string(value: &Bound<'_, PyAny>) -> PyResult<String> {
146    if let Ok(v) = value.extract::<bool>() {
147        return Ok(v.to_string().to_lowercase());
148    }
149
150    if let Ok(v) = value.extract::<i64>() {
151        return Ok(v.to_string());
152    }
153
154    if let Ok(v) = value.extract::<u64>() {
155        return Ok(v.to_string());
156    }
157
158    if let Ok(v) = value.extract::<f64>() {
159        if !v.is_finite() {
160            return Err(to_pyvalue_err("Gamma filter values must be finite"));
161        }
162        return Ok(v.to_string());
163    }
164
165    if let Ok(v) = value.extract::<String>() {
166        return Ok(v);
167    }
168
169    Err(to_pyvalue_err(
170        "Gamma filter values must be bool, int, float, string, or a list of those values",
171    ))
172}
173
174fn py_filter_value_to_string(value: &Bound<'_, PyAny>) -> PyResult<String> {
175    if value.extract::<f64>().is_ok_and(|value| !value.is_finite()) {
176        return Err(to_pyvalue_err("Gamma filter values must be finite"));
177    }
178
179    if let Ok(value) = py_scalar_to_string(value) {
180        return Ok(value);
181    }
182
183    if value.cast::<PyDict>().is_ok() {
184        return Err(to_pyvalue_err("Gamma filter values cannot be dictionaries"));
185    }
186
187    if let Ok(iter) = value.try_iter() {
188        let values = iter
189            .map(|item| py_scalar_to_string(&item?))
190            .collect::<PyResult<Vec<_>>>()?;
191
192        if values.is_empty() {
193            return Err(to_pyvalue_err("Gamma filter lists cannot be empty"));
194        }
195        return Ok(values.join(","));
196    }
197
198    Err(to_pyvalue_err(
199        "Gamma filter values must be bool, int, float, string, or a list of those values",
200    ))
201}
202
203pub(super) fn extract_string_map(
204    value: &Bound<'_, PyAny>,
205) -> PyResult<std::collections::HashMap<String, String>> {
206    let dict = value.cast::<PyDict>()?;
207    let mut map = std::collections::HashMap::with_capacity(dict.len());
208    for (key, value) in dict.iter() {
209        if value.is_none() {
210            continue;
211        }
212        map.insert(key.extract::<String>()?, py_filter_value_to_string(&value)?);
213    }
214    Ok(map)
215}
216
217fn validate_provider_config(config: &PolymarketInstrumentProviderConfig) -> PyResult<()> {
218    config.validate().map_err(to_pyvalue_err)?;
219    Ok(())
220}
221
222fn validate_data_config(config: &PolymarketDataClientConfig) -> PyResult<()> {
223    config.validate().map_err(to_pyvalue_err)?;
224    config
225        .validated_proxy_url()
226        .map_err(|e| to_pyvalue_err(format!("Invalid Polymarket proxy URL: {e}")))?;
227    Ok(())
228}
229
230fn extract_event_slug_builder(
231    value: &Bound<'_, PyAny>,
232) -> PyResult<PolymarketUpDownEventSlugConfig> {
233    if let Ok(builder) = value.extract::<PolymarketUpDownEventSlugConfig>() {
234        return Ok(builder);
235    }
236
237    if value.extract::<String>().is_ok() {
238        return Err(to_pyvalue_err(
239            "Python callable event_slug_builder is not supported by the Rust Polymarket adapter; \
240             pass event_slugs, market_slugs, or PolymarketUpDownEventSlugConfig",
241        ));
242    }
243
244    Err(to_pyvalue_err(
245        "event_slug_builder must be PolymarketUpDownEventSlugConfig",
246    ))
247}
248
249fn extract_provider_config_from_pyobject(
250    obj: &Bound<'_, PyAny>,
251) -> PyResult<PolymarketInstrumentProviderConfig> {
252    if let Ok(config) = obj.extract::<PolymarketInstrumentProviderConfig>() {
253        validate_provider_config(&config)?;
254        return Ok(config);
255    }
256
257    let default = PolymarketInstrumentProviderConfig::default();
258    let load_all = getattr_optional(obj, "load_all")?
259        .map(|value| value.extract::<bool>())
260        .transpose()?
261        .unwrap_or(default.load_all);
262    let load_ids = getattr_optional(obj, "load_ids")?
263        .map(|value| value.extract::<Vec<InstrumentId>>())
264        .transpose()?;
265    let filters = getattr_optional(obj, "filters")?
266        .map(|value| extract_string_map(&value))
267        .transpose()?;
268    let event_slugs = getattr_optional(obj, "event_slugs")?
269        .map(|value| value.extract::<Vec<String>>())
270        .transpose()?;
271    let market_slugs = getattr_optional(obj, "market_slugs")?
272        .map(|value| value.extract::<Vec<String>>())
273        .transpose()?;
274    let event_slug_builder = getattr_optional(obj, "event_slug_builder")?
275        .map(|value| extract_event_slug_builder(&value))
276        .transpose()?;
277    let series_ids = getattr_optional(obj, "series_ids")?
278        .map(|value| value.extract::<Vec<u64>>())
279        .transpose()?;
280    let log_warnings = getattr_optional(obj, "log_warnings")?
281        .map(|value| value.extract::<bool>())
282        .transpose()?
283        .unwrap_or(default.log_warnings);
284    let use_gamma_markets = getattr_optional(obj, "use_gamma_markets")?
285        .map(|value| value.extract::<bool>())
286        .transpose()?
287        .unwrap_or(default.use_gamma_markets);
288
289    let config = PolymarketInstrumentProviderConfig {
290        load_all: load_all || event_slug_builder.is_some(),
291        load_ids,
292        filters,
293        event_slugs,
294        market_slugs,
295        event_slug_builder,
296        series_ids,
297        log_warnings,
298        use_gamma_markets,
299    };
300    validate_provider_config(&config)?;
301    Ok(config)
302}
303
304fn extract_data_config_from_pyobject(
305    py: Python<'_>,
306    config: &Py<PyAny>,
307) -> PyResult<PolymarketDataClientConfig> {
308    if let Ok(config) = config.extract::<PolymarketDataClientConfig>(py) {
309        validate_data_config(&config)?;
310        return Ok(config);
311    }
312
313    let obj = config.bind(py);
314    let default = PolymarketDataClientConfig::default();
315    let instrument_config = getattr_optional(obj, "instrument_config")?
316        .map(|value| extract_provider_config_from_pyobject(&value))
317        .transpose()?;
318    let base_url_http = getattr_optional(obj, "base_url_http")?
319        .map(|value| value.extract::<String>())
320        .transpose()?;
321    let base_url_ws = getattr_optional(obj, "base_url_ws")?
322        .map(|value| value.extract::<String>())
323        .transpose()?;
324    let base_url_reference_prices = getattr_optional(obj, "base_url_reference_prices")?
325        .map(|value| value.extract::<String>())
326        .transpose()?;
327    let api_key = getattr_optional(obj, "api_key")?
328        .map(|value| value.extract::<String>())
329        .transpose()?
330        .map(SecretString::from);
331    let api_secret = getattr_optional(obj, "api_secret")?
332        .map(|value| value.extract::<String>())
333        .transpose()?
334        .map(SecretString::from);
335    let passphrase = getattr_optional(obj, "passphrase")?
336        .map(|value| value.extract::<String>())
337        .transpose()?
338        .map(SecretString::from);
339    let base_url_gamma = getattr_optional(obj, "base_url_gamma")?
340        .map(|value| value.extract::<String>())
341        .transpose()?;
342    let base_url_data_api = getattr_optional(obj, "base_url_data_api")?
343        .map(|value| value.extract::<String>())
344        .transpose()?;
345    let proxy_url = getattr_optional(obj, "proxy_url")?
346        .map(|value| value.extract::<String>())
347        .transpose()?
348        .map(SecretString::from);
349    let http_timeout_secs = getattr_optional(obj, "http_timeout_secs")?
350        .map(|value| value.extract::<u64>())
351        .transpose()?
352        .unwrap_or(default.http_timeout_secs);
353    let ws_timeout_secs = getattr_optional(obj, "ws_timeout_secs")?
354        .map(|value| value.extract::<u64>())
355        .transpose()?
356        .unwrap_or(default.ws_timeout_secs);
357    let ws_max_subscriptions = getattr_optional(obj, "ws_max_subscriptions")?
358        .map(|value| value.extract::<usize>())
359        .transpose()?
360        .unwrap_or(default.ws_max_subscriptions);
361    let update_instruments_interval_mins = getattr_optional_option_u64(
362        obj,
363        "update_instruments_interval_mins",
364        default.update_instruments_interval_mins,
365    )?;
366    let subscribe_new_markets = getattr_optional(obj, "subscribe_new_markets")?
367        .map(|value| value.extract::<bool>())
368        .transpose()?
369        .unwrap_or(default.subscribe_new_markets);
370    let new_market_fetch_max_concurrency =
371        getattr_optional(obj, "new_market_fetch_max_concurrency")?
372            .map(|value| value.extract::<usize>())
373            .transpose()?
374            .unwrap_or(default.new_market_fetch_max_concurrency);
375    let drop_quotes_missing_side = getattr_optional(obj, "drop_quotes_missing_side")?
376        .map(|value| value.extract::<bool>())
377        .transpose()?
378        .unwrap_or(default.drop_quotes_missing_side);
379    let compute_effective_deltas = getattr_optional(obj, "compute_effective_deltas")?
380        .map(|value| value.extract::<bool>())
381        .transpose()?
382        .unwrap_or(default.compute_effective_deltas);
383    let auto_load_missing_instruments = getattr_optional(obj, "auto_load_missing_instruments")?
384        .map(|value| value.extract::<bool>())
385        .transpose()?
386        .unwrap_or(default.auto_load_missing_instruments);
387    let auto_load_debounce_ms = getattr_optional(obj, "auto_load_debounce_ms")?
388        .map(|value| value.extract::<u64>())
389        .transpose()?
390        .unwrap_or(default.auto_load_debounce_ms);
391    let auto_load_max_retries = getattr_optional(obj, "auto_load_max_retries")?
392        .map(|value| value.extract::<u32>())
393        .transpose()?
394        .unwrap_or(default.auto_load_max_retries);
395    let auto_load_retry_delay_initial_secs =
396        getattr_optional(obj, "auto_load_retry_delay_initial_secs")?
397            .map(|value| value.extract::<f64>())
398            .transpose()?
399            .unwrap_or(default.auto_load_retry_delay_initial_secs);
400    let auto_load_retry_delay_max_secs = getattr_optional(obj, "auto_load_retry_delay_max_secs")?
401        .map(|value| value.extract::<f64>())
402        .transpose()?
403        .unwrap_or(default.auto_load_retry_delay_max_secs);
404    let resolve_poll_enabled = getattr_optional(obj, "resolve_poll_enabled")?
405        .map(|value| value.extract::<bool>())
406        .transpose()?
407        .unwrap_or(default.resolve_poll_enabled);
408    let resolve_poll_interval_secs = getattr_optional(obj, "resolve_poll_interval_secs")?
409        .map(|value| value.extract::<u64>())
410        .transpose()?
411        .unwrap_or(default.resolve_poll_interval_secs);
412    let resolve_poll_grace_secs = getattr_optional(obj, "resolve_poll_grace_secs")?
413        .map(|value| value.extract::<u64>())
414        .transpose()?
415        .unwrap_or(default.resolve_poll_grace_secs);
416    let resolve_poll_max_wait_secs = getattr_optional(obj, "resolve_poll_max_wait_secs")?
417        .map(|value| value.extract::<u64>())
418        .transpose()?
419        .unwrap_or(default.resolve_poll_max_wait_secs);
420    let transport_backend = match getattr_optional(obj, "transport_backend")? {
421        Some(value) => value.extract::<TransportBackend>()?,
422        None => default.transport_backend,
423    };
424
425    let book_snapshot_timeout_secs = getattr_optional(obj, "book_snapshot_timeout_secs")?
426        .map(|value| value.extract::<u64>())
427        .transpose()?
428        .unwrap_or(default.book_snapshot_timeout_secs);
429    let book_stale_check_interval_secs = getattr_optional(obj, "book_stale_check_interval_secs")?
430        .map(|value| value.extract::<u64>())
431        .transpose()?
432        .unwrap_or(default.book_stale_check_interval_secs);
433    let book_stale_threshold_secs = getattr_optional(obj, "book_stale_threshold_secs")?
434        .map(|value| value.extract::<u64>())
435        .transpose()?
436        .unwrap_or(default.book_stale_threshold_secs);
437
438    let config = PolymarketDataClientConfig {
439        instrument_config,
440        filters: Vec::new(),
441        base_url_http,
442        base_url_ws,
443        base_url_reference_prices,
444        base_url_gamma,
445        base_url_data_api,
446        api_key,
447        api_secret,
448        passphrase,
449        proxy_url,
450        http_timeout_secs,
451        ws_timeout_secs,
452        ws_max_subscriptions,
453        update_instruments_interval_mins,
454        subscribe_new_markets,
455        new_market_filter: None,
456        new_market_fetch_max_concurrency,
457        drop_quotes_missing_side,
458        compute_effective_deltas,
459        auto_load_missing_instruments,
460        auto_load_debounce_ms,
461        auto_load_max_retries,
462        auto_load_retry_delay_initial_secs,
463        auto_load_retry_delay_max_secs,
464        resolve_poll_enabled,
465        resolve_poll_interval_secs,
466        resolve_poll_grace_secs,
467        resolve_poll_max_wait_secs,
468        transport_backend,
469        book_snapshot_timeout_secs,
470        book_stale_check_interval_secs,
471        book_stale_threshold_secs,
472    };
473    validate_data_config(&config)?;
474    Ok(config)
475}
476
477#[expect(clippy::needless_pass_by_value)]
478fn extract_polymarket_data_factory(
479    py: Python<'_>,
480    factory: Py<PyAny>,
481) -> PyResult<Box<dyn DataClientFactory>> {
482    match factory.extract::<PolymarketDataClientFactory>(py) {
483        Ok(f) => Ok(Box::new(f)),
484        Err(e) => Err(to_pyvalue_err(format!(
485            "Failed to extract PolymarketDataClientFactory: {e}"
486        ))),
487    }
488}
489
490#[expect(clippy::needless_pass_by_value)]
491fn extract_polymarket_exec_factory(
492    py: Python<'_>,
493    factory: Py<PyAny>,
494) -> PyResult<Box<dyn ExecutionClientFactory>> {
495    match factory.extract::<PolymarketExecutionClientFactory>(py) {
496        Ok(f) => Ok(Box::new(f)),
497        Err(e) => Err(to_pyvalue_err(format!(
498            "Failed to extract PolymarketExecutionClientFactory: {e}"
499        ))),
500    }
501}
502
503#[expect(clippy::needless_pass_by_value)]
504fn extract_polymarket_data_config(
505    py: Python<'_>,
506    config: Py<PyAny>,
507) -> PyResult<Box<dyn ClientConfig>> {
508    match extract_data_config_from_pyobject(py, &config) {
509        Ok(c) => Ok(Box::new(c)),
510        Err(e) => Err(to_pyvalue_err(format!(
511            "Failed to extract PolymarketDataClientConfig: {e}"
512        ))),
513    }
514}
515
516#[expect(clippy::needless_pass_by_value)]
517fn extract_polymarket_exec_config(
518    py: Python<'_>,
519    config: Py<PyAny>,
520) -> PyResult<Box<dyn ClientConfig>> {
521    match config.extract::<PolymarketExecutionClientConfig>(py) {
522        Ok(c) => {
523            c.validated_proxy_url()
524                .map_err(|e| to_pyvalue_err(format!("Invalid Polymarket proxy URL: {e}")))?;
525            Ok(Box::new(c))
526        }
527        Err(e) => Err(to_pyvalue_err(format!(
528            "Failed to extract PolymarketExecutionClientConfig: {e}"
529        ))),
530    }
531}
532
533/// Exposed through `nautilus_trader.adapters.polymarket`.
534#[pymodule]
535pub fn polymarket(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
536    m.add(stringify!(POLYMARKET), POLYMARKET)?;
537    m.add(stringify!(POLYMARKET_CLIENT_ID), *POLYMARKET_CLIENT_ID)?;
538    m.add(stringify!(POLYMARKET_VENUE), *POLYMARKET_VENUE)?;
539    m.add_class::<crate::common::enums::PolymarketSignatureType>()?;
540    m.add_class::<crate::common::enums::PolymarketSignerType>()?;
541    m.add_class::<PolymarketUpDownEventSlugConfig>()?;
542    m.add_class::<PolymarketInstrumentProviderConfig>()?;
543    m.add_class::<PolymarketDataClientConfig>()?;
544    m.add_class::<PolymarketDataClientFactory>()?;
545    m.add_class::<PolymarketExecutionClientConfig>()?;
546    m.add_class::<PolymarketExecutionClientFactory>()?;
547    m.add_class::<PolymarketFeeModel>()?;
548    m.add_class::<loader::PyPolymarketDataLoader>()?;
549    m.add_class::<session::PyPolymarketSessionKeyClient>()?;
550    m.add_class::<session::PyPolymarketSessionKeyClientConfig>()?;
551    m.add_class::<session::PyPolymarketSessionKey>()?;
552    m.add_class::<positions::PyPolymarketPositionClient>()?;
553    m.add_class::<positions::PyPolymarketPositionOutcome>()?;
554    m.add_class::<positions::PyPolymarketPositionTransaction>()?;
555    m.add_class::<PolymarketCryptoPrice>()?;
556    m.add_class::<PolymarketCryptoTwap>()?;
557    m.add_class::<PolymarketEquityPrice>()?;
558    m.add_function(pyo3::wrap_pyfunction!(
559        sort::py_polymarket_trade_sort_key,
560        m
561    )?)?;
562    m.add_function(pyo3::wrap_pyfunction!(sort::py_polymarket_trade_id, m)?)?;
563
564    register_polymarket_custom_data();
565    let _result = ensure_rust_extractor_registered::<PolymarketCryptoPrice>();
566    let _result = ensure_rust_extractor_registered::<PolymarketCryptoTwap>();
567    let _result = ensure_rust_extractor_registered::<PolymarketEquityPrice>();
568
569    let registry = get_global_pyo3_registry();
570
571    if let Err(e) =
572        registry.register_factory_extractor(POLYMARKET.to_string(), extract_polymarket_data_factory)
573    {
574        return Err(to_pyruntime_err(format!(
575            "Failed to register Polymarket data factory extractor: {e}"
576        )));
577    }
578
579    if let Err(e) = registry
580        .register_exec_factory_extractor(POLYMARKET.to_string(), extract_polymarket_exec_factory)
581    {
582        return Err(to_pyruntime_err(format!(
583            "Failed to register Polymarket exec factory extractor: {e}"
584        )));
585    }
586
587    if let Err(e) = registry.register_config_extractor(
588        "PolymarketDataClientConfig".to_string(),
589        extract_polymarket_data_config,
590    ) {
591        return Err(to_pyruntime_err(format!(
592            "Failed to register Polymarket data config extractor: {e}"
593        )));
594    }
595
596    if let Err(e) = registry.register_config_extractor(
597        "PolymarketExecutionClientConfig".to_string(),
598        extract_polymarket_exec_config,
599    ) {
600        return Err(to_pyruntime_err(format!(
601            "Failed to register Polymarket exec config extractor: {e}"
602        )));
603    }
604
605    Ok(())
606}
607
608#[cfg(all(test, feature = "python"))]
609mod tests {
610    use std::sync::Arc;
611
612    use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
613    use nautilus_core::Params;
614    use nautilus_model::data::{
615        CustomData, DataType, custom::CustomDataTrait, ensure_rust_extractor_registered,
616    };
617    use pyo3::{exceptions::PyValueError, prelude::*, types::PyDict};
618    use rstest::rstest;
619    use rust_decimal_macros::dec;
620    use serde_json::json;
621
622    use super::extract_data_config_from_pyobject;
623    use crate::{
624        config::{
625            PolymarketDataClientConfig, PolymarketInstrumentProviderConfig,
626            PolymarketUpDownEventSlugConfig,
627        },
628        data_types::{PolymarketCryptoPrice, register_polymarket_custom_data},
629    };
630
631    #[rstest]
632    fn extract_data_config_supports_python_style_namespace() {
633        Python::initialize();
634        Python::attach(|py| {
635            let types = py.import("types").expect("types");
636            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
637            let event_slug_builder = Py::new(
638                py,
639                PolymarketUpDownEventSlugConfig {
640                    assets: vec!["btc".to_string(), "eth".to_string()],
641                    interval_mins: 5,
642                    periods: 2,
643                    start_offset_periods: 0,
644                },
645            )
646            .expect("event slug builder should convert to Python object");
647
648            let instrument_kwargs = PyDict::new(py);
649            instrument_kwargs
650                .set_item("event_slug_builder", event_slug_builder)
651                .unwrap();
652            instrument_kwargs
653                .set_item("event_slugs", vec!["event-a", "event-b"])
654                .unwrap();
655            instrument_kwargs
656                .set_item("market_slugs", vec!["market-a"])
657                .unwrap();
658            instrument_kwargs.set_item("load_all", false).unwrap();
659            instrument_kwargs.set_item("log_warnings", false).unwrap();
660            let instrument_config = namespace
661                .call((), Some(&instrument_kwargs))
662                .expect("instrument namespace");
663
664            let config_kwargs = PyDict::new(py);
665            config_kwargs
666                .set_item("instrument_config", instrument_config)
667                .unwrap();
668            config_kwargs
669                .set_item("update_instruments_interval_mins", 1)
670                .unwrap();
671            config_kwargs
672                .set_item("subscribe_new_markets", false)
673                .unwrap();
674            config_kwargs
675                .set_item("new_market_fetch_max_concurrency", 13)
676                .unwrap();
677            config_kwargs
678                .set_item("drop_quotes_missing_side", false)
679                .unwrap();
680            config_kwargs
681                .set_item("compute_effective_deltas", true)
682                .unwrap();
683            config_kwargs
684                .set_item("base_url_gamma", "https://gamma.example")
685                .unwrap();
686            config_kwargs
687                .set_item("base_url_reference_prices", "wss://ws-live-data.example")
688                .unwrap();
689            let api_secret = BASE64_STANDARD.encode(b"namespace-secret");
690            config_kwargs.set_item("api_key", "namespace-key").unwrap();
691            config_kwargs.set_item("api_secret", api_secret).unwrap();
692            config_kwargs
693                .set_item("passphrase", "namespace-passphrase")
694                .unwrap();
695            config_kwargs
696                .set_item("base_url_data_api", "https://data.example")
697                .unwrap();
698            config_kwargs
699                .set_item("proxy_url", "http://proxy.example:18085")
700                .unwrap();
701            config_kwargs.set_item("ws_timeout_secs", 41).unwrap();
702            config_kwargs.set_item("ws_max_subscriptions", 512).unwrap();
703            config_kwargs
704                .set_item("auto_load_missing_instruments", true)
705                .unwrap();
706            config_kwargs
707                .set_item("auto_load_debounce_ms", 100)
708                .unwrap();
709            config_kwargs.set_item("auto_load_max_retries", 12).unwrap();
710            config_kwargs
711                .set_item("auto_load_retry_delay_initial_secs", 5.0)
712                .unwrap();
713            config_kwargs
714                .set_item("auto_load_retry_delay_max_secs", 15.0)
715                .unwrap();
716            config_kwargs
717                .set_item("resolve_poll_enabled", false)
718                .unwrap();
719            config_kwargs
720                .set_item("resolve_poll_interval_secs", 45)
721                .unwrap();
722            config_kwargs
723                .set_item("resolve_poll_grace_secs", 12)
724                .unwrap();
725            config_kwargs
726                .set_item("resolve_poll_max_wait_secs", 2400)
727                .unwrap();
728            config_kwargs
729                .set_item("book_snapshot_timeout_secs", 42)
730                .unwrap();
731            config_kwargs
732                .set_item("book_stale_check_interval_secs", 7)
733                .unwrap();
734            config_kwargs
735                .set_item("book_stale_threshold_secs", 9)
736                .unwrap();
737            let config_obj = namespace
738                .call((), Some(&config_kwargs))
739                .expect("config namespace");
740
741            let rust_config = extract_data_config_from_pyobject(py, &config_obj.unbind())
742                .expect("extract rust config");
743            let instrument_config = rust_config
744                .instrument_config
745                .expect("instrument_config should be extracted");
746
747            assert!(
748                instrument_config.load_all,
749                "event_slug_builder should imply scoped load_all bootstrap"
750            );
751            let event_slug_builder = instrument_config
752                .event_slug_builder
753                .expect("event_slug_builder should be extracted");
754            assert_eq!(
755                event_slug_builder.assets,
756                ["btc".to_string(), "eth".to_string()]
757            );
758            assert_eq!(event_slug_builder.interval_mins, 5);
759            assert_eq!(event_slug_builder.periods, 2);
760            assert_eq!(event_slug_builder.start_offset_periods, 0);
761            assert_eq!(
762                instrument_config.event_slugs.as_deref(),
763                Some(&["event-a".to_string(), "event-b".to_string()][..])
764            );
765            assert_eq!(
766                instrument_config.market_slugs.as_deref(),
767                Some(&["market-a".to_string()][..])
768            );
769            assert!(!instrument_config.log_warnings);
770            assert_eq!(rust_config.update_instruments_interval_mins, Some(1));
771            assert!(!rust_config.subscribe_new_markets);
772            assert_eq!(rust_config.new_market_fetch_max_concurrency, 13);
773            assert!(!rust_config.drop_quotes_missing_side);
774            assert!(rust_config.compute_effective_deltas);
775            assert_eq!(
776                rust_config.base_url_gamma.as_deref(),
777                Some("https://gamma.example")
778            );
779            assert_eq!(
780                rust_config.base_url_reference_prices.as_deref(),
781                Some("wss://ws-live-data.example")
782            );
783            assert_eq!(
784                rust_config.base_url_data_api.as_deref(),
785                Some("https://data.example")
786            );
787            assert_eq!(rust_config.ws_timeout_secs, 41);
788            assert_eq!(rust_config.ws_max_subscriptions, 512);
789            assert_eq!(
790                rust_config
791                    .proxy_url
792                    .as_ref()
793                    .map(|value| value.expose_secret()),
794                Some("http://proxy.example:18085")
795            );
796            assert!(!rust_config.resolve_poll_enabled);
797            assert_eq!(rust_config.resolve_poll_interval_secs, 45);
798            assert_eq!(rust_config.resolve_poll_grace_secs, 12);
799            assert_eq!(rust_config.resolve_poll_max_wait_secs, 2400);
800            assert_eq!(rust_config.book_snapshot_timeout_secs, 42);
801            assert_eq!(rust_config.book_stale_check_interval_secs, 7);
802            assert_eq!(rust_config.book_stale_threshold_secs, 9);
803            assert_eq!(
804                rust_config
805                    .api_key
806                    .as_ref()
807                    .map(|value| value.expose_secret()),
808                Some("namespace-key")
809            );
810            assert_eq!(
811                rust_config
812                    .api_secret
813                    .as_ref()
814                    .map(|value| value.expose_secret()),
815                Some("bmFtZXNwYWNlLXNlY3JldA==")
816            );
817            assert_eq!(
818                rust_config
819                    .passphrase
820                    .as_ref()
821                    .map(|value| value.expose_secret()),
822                Some("namespace-passphrase")
823            );
824        });
825    }
826
827    #[rstest]
828    fn data_config_py_constructor_accepts_book_sync_kwargs() {
829        Python::initialize();
830        Python::attach(|py| {
831            let kwargs = PyDict::new(py);
832            kwargs.set_item("book_snapshot_timeout_secs", 42).unwrap();
833            kwargs
834                .set_item("book_stale_check_interval_secs", 7)
835                .unwrap();
836            kwargs.set_item("book_stale_threshold_secs", 9).unwrap();
837
838            let config = py
839                .get_type::<PolymarketDataClientConfig>()
840                .call((), Some(&kwargs))
841                .expect("data config should accept book sync kwargs");
842
843            assert_eq!(
844                config
845                    .getattr("book_snapshot_timeout_secs")
846                    .expect("timeout getter")
847                    .extract::<u64>()
848                    .expect("u64 timeout"),
849                42
850            );
851            assert_eq!(
852                config
853                    .getattr("book_stale_check_interval_secs")
854                    .expect("interval getter")
855                    .extract::<u64>()
856                    .expect("u64 interval"),
857                7
858            );
859            assert_eq!(
860                config
861                    .getattr("book_stale_threshold_secs")
862                    .expect("threshold getter")
863                    .extract::<u64>()
864                    .expect("u64 threshold"),
865                9
866            );
867        });
868    }
869
870    #[rstest]
871    fn extract_data_config_rejects_python_callable_event_slug_builder() {
872        Python::initialize();
873        Python::attach(|py| {
874            let types = py.import("types").expect("types");
875            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
876
877            let instrument_kwargs = PyDict::new(py);
878            instrument_kwargs
879                .set_item("event_slug_builder", "pkg.module:build_event_slugs")
880                .unwrap();
881            let instrument_config = namespace
882                .call((), Some(&instrument_kwargs))
883                .expect("instrument namespace");
884
885            let config_kwargs = PyDict::new(py);
886            config_kwargs
887                .set_item("instrument_config", instrument_config)
888                .unwrap();
889            let config_obj = namespace
890                .call((), Some(&config_kwargs))
891                .expect("config namespace");
892
893            let err = extract_data_config_from_pyobject(py, &config_obj.unbind())
894                .expect_err("Python callable event_slug_builder should be rejected");
895
896            assert!(
897                err.to_string()
898                    .contains("Python callable event_slug_builder is not supported")
899            );
900        });
901    }
902
903    #[rstest]
904    fn native_provider_config_rejects_invalid_gamma_filters_as_value_error() {
905        Python::initialize();
906        Python::attach(|py| {
907            let filters = PyDict::new(py);
908            filters.set_item("active", "yes").unwrap();
909            let kwargs = PyDict::new(py);
910            kwargs.set_item("filters", filters).unwrap();
911
912            let err = py
913                .get_type::<PolymarketInstrumentProviderConfig>()
914                .call((), Some(&kwargs))
915                .unwrap_err();
916
917            assert!(err.is_instance_of::<PyValueError>(py));
918            assert!(err.to_string().contains("must be true or false"));
919        });
920    }
921
922    #[rstest]
923    fn extract_data_config_accepts_v1_shaped_filter_values() {
924        Python::initialize();
925        Python::attach(|py| {
926            let types = py.import("types").expect("types");
927            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
928            let filters = PyDict::new(py);
929            filters.set_item("is_active", true).unwrap();
930            filters.set_item("id", vec![1, 2]).unwrap();
931            filters.set_item("volume_num_min", 1.25).unwrap();
932            filters.set_item("tag_id", py.None()).unwrap();
933            let instrument_kwargs = PyDict::new(py);
934            instrument_kwargs.set_item("filters", filters).unwrap();
935            let instrument_config = namespace
936                .call((), Some(&instrument_kwargs))
937                .expect("instrument namespace");
938            let config_kwargs = PyDict::new(py);
939            config_kwargs
940                .set_item("instrument_config", instrument_config)
941                .unwrap();
942            let config_obj = namespace
943                .call((), Some(&config_kwargs))
944                .expect("config namespace");
945
946            let rust_config = extract_data_config_from_pyobject(py, &config_obj.unbind())
947                .expect("v1-shaped filters should convert");
948            let filters = rust_config
949                .instrument_config
950                .and_then(|config| config.filters)
951                .expect("filters should be extracted");
952
953            assert_eq!(filters.get("is_active").map(String::as_str), Some("true"));
954            assert_eq!(filters.get("id").map(String::as_str), Some("1,2"));
955            assert_eq!(
956                filters.get("volume_num_min").map(String::as_str),
957                Some("1.25")
958            );
959            assert!(!filters.contains_key("tag_id"));
960        });
961    }
962
963    #[rstest]
964    fn extract_data_config_rejects_non_finite_filter_as_value_error() {
965        Python::initialize();
966        Python::attach(|py| {
967            let types = py.import("types").expect("types");
968            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
969            let filters = PyDict::new(py);
970            filters.set_item("volume_num_min", f64::NAN).unwrap();
971            let instrument_kwargs = PyDict::new(py);
972            instrument_kwargs.set_item("filters", filters).unwrap();
973            let instrument_config = namespace
974                .call((), Some(&instrument_kwargs))
975                .expect("instrument namespace");
976            let config_kwargs = PyDict::new(py);
977            config_kwargs
978                .set_item("instrument_config", instrument_config)
979                .unwrap();
980            let config_obj = namespace
981                .call((), Some(&config_kwargs))
982                .expect("config namespace");
983
984            let err = extract_data_config_from_pyobject(py, &config_obj.unbind()).unwrap_err();
985
986            assert!(err.is_instance_of::<PyValueError>(py));
987            assert!(err.to_string().contains("must be finite"));
988        });
989    }
990
991    #[rstest]
992    fn extract_data_config_rejects_v1_shaped_unknown_filter_as_value_error() {
993        Python::initialize();
994        Python::attach(|py| {
995            let types = py.import("types").expect("types");
996            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
997            let filters = PyDict::new(py);
998            filters.set_item("unsupported_filter", true).unwrap();
999            let instrument_kwargs = PyDict::new(py);
1000            instrument_kwargs.set_item("filters", filters).unwrap();
1001            let instrument_config = namespace
1002                .call((), Some(&instrument_kwargs))
1003                .expect("instrument namespace");
1004            let config_kwargs = PyDict::new(py);
1005            config_kwargs
1006                .set_item("instrument_config", instrument_config)
1007                .unwrap();
1008            let config_obj = namespace
1009                .call((), Some(&config_kwargs))
1010                .expect("config namespace");
1011
1012            let err = extract_data_config_from_pyobject(py, &config_obj.unbind()).unwrap_err();
1013
1014            assert!(err.is_instance_of::<PyValueError>(py));
1015            assert!(err.to_string().contains("Unknown Gamma market filter key"));
1016        });
1017    }
1018
1019    #[rstest]
1020    fn extract_data_config_preserves_none_update_interval() {
1021        Python::initialize();
1022        Python::attach(|py| {
1023            let types = py.import("types").expect("types");
1024            let namespace = types.getattr("SimpleNamespace").expect("SimpleNamespace");
1025            let config_kwargs = PyDict::new(py);
1026            config_kwargs
1027                .set_item("update_instruments_interval_mins", py.None())
1028                .unwrap();
1029            let config_obj = namespace
1030                .call((), Some(&config_kwargs))
1031                .expect("config namespace");
1032
1033            let rust_config = extract_data_config_from_pyobject(py, &config_obj.unbind())
1034                .expect("extract rust config");
1035
1036            assert_eq!(rust_config.update_instruments_interval_mins, None);
1037        });
1038    }
1039
1040    #[rstest]
1041    fn custom_data_getter_unwraps_polybolt_payload_to_python_class() {
1042        Python::initialize();
1043        Python::attach(|py| {
1044            register_polymarket_custom_data();
1045            let _result = ensure_rust_extractor_registered::<PolymarketCryptoPrice>();
1046
1047            let metadata = Params::new([("symbol".to_string(), json!("btcusd"))].into());
1048
1049            let payload = Arc::new(PolymarketCryptoPrice::new(
1050                "btcusd".to_string(),
1051                "chainlink".to_string(),
1052                dec!(67234.50),
1053                1_753_314_088_395,
1054                1_753_314_088_421,
1055                nautilus_core::UnixNanos::from_millis(1_753_314_088_395),
1056                nautilus_core::UnixNanos::from_millis(1_753_314_088_421),
1057            ));
1058            let custom = CustomData::new(
1059                payload,
1060                DataType::new(
1061                    PolymarketCryptoPrice::type_name_static(),
1062                    Some(metadata),
1063                    None,
1064                ),
1065            );
1066
1067            let py_custom = Py::new(py, custom).expect("create Python CustomData");
1068            let py_payload = py_custom.bind(py).getattr("data").expect("CustomData.data");
1069
1070            assert_eq!(
1071                py_payload.get_type().name().expect("type name"),
1072                "PolymarketCryptoPrice"
1073            );
1074            assert_eq!(
1075                py_payload
1076                    .getattr("symbol")
1077                    .expect("symbol")
1078                    .extract::<String>()
1079                    .expect("extract symbol"),
1080                "btcusd"
1081            );
1082            assert_eq!(
1083                py_payload
1084                    .getattr("value")
1085                    .expect("value")
1086                    .str()
1087                    .expect("value str")
1088                    .to_string(),
1089                "67234.50"
1090            );
1091        });
1092    }
1093}