Skip to main content

nautilus_coinbase/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
18pub mod config;
19pub mod enums;
20pub mod factories;
21
22use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
23use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
24use nautilus_system::get_global_pyo3_registry;
25use pyo3::prelude::*;
26
27use crate::{
28    common::consts::{COINBASE, COINBASE_CLIENT_ID, COINBASE_VENUE},
29    config::{CoinbaseDataClientConfig, CoinbaseExecutionClientConfig},
30    factories::{CoinbaseDataClientFactory, CoinbaseExecutionClientFactory},
31};
32
33#[expect(clippy::needless_pass_by_value)]
34fn extract_coinbase_data_factory(
35    py: Python<'_>,
36    factory: Py<PyAny>,
37) -> PyResult<Box<dyn DataClientFactory>> {
38    match factory.extract::<CoinbaseDataClientFactory>(py) {
39        Ok(f) => Ok(Box::new(f)),
40        Err(e) => Err(to_pyvalue_err(format!(
41            "Failed to extract CoinbaseDataClientFactory: {e}"
42        ))),
43    }
44}
45
46#[expect(clippy::needless_pass_by_value)]
47fn extract_coinbase_exec_factory(
48    py: Python<'_>,
49    factory: Py<PyAny>,
50) -> PyResult<Box<dyn ExecutionClientFactory>> {
51    match factory.extract::<CoinbaseExecutionClientFactory>(py) {
52        Ok(f) => Ok(Box::new(f)),
53        Err(e) => Err(to_pyvalue_err(format!(
54            "Failed to extract CoinbaseExecutionClientFactory: {e}"
55        ))),
56    }
57}
58
59#[expect(clippy::needless_pass_by_value)]
60fn extract_coinbase_data_config(
61    py: Python<'_>,
62    config: Py<PyAny>,
63) -> PyResult<Box<dyn ClientConfig>> {
64    match config.extract::<CoinbaseDataClientConfig>(py) {
65        Ok(c) => Ok(Box::new(c)),
66        Err(e) => Err(to_pyvalue_err(format!(
67            "Failed to extract CoinbaseDataClientConfig: {e}"
68        ))),
69    }
70}
71
72#[expect(clippy::needless_pass_by_value)]
73fn extract_coinbase_exec_config(
74    py: Python<'_>,
75    config: Py<PyAny>,
76) -> PyResult<Box<dyn ClientConfig>> {
77    match config.extract::<CoinbaseExecutionClientConfig>(py) {
78        Ok(c) => Ok(Box::new(c)),
79        Err(e) => Err(to_pyvalue_err(format!(
80            "Failed to extract CoinbaseExecutionClientConfig: {e}"
81        ))),
82    }
83}
84
85/// Exposed through `nautilus_trader.adapters.coinbase`.
86///
87/// # Errors
88///
89/// Returns an error if any bindings fail to register with the Python module.
90#[pymodule]
91pub fn coinbase(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
92    m.add(stringify!(COINBASE), COINBASE)?;
93    m.add(stringify!(COINBASE_CLIENT_ID), *COINBASE_CLIENT_ID)?;
94    m.add(stringify!(COINBASE_VENUE), *COINBASE_VENUE)?;
95    m.add_class::<crate::common::enums::CoinbaseEnvironment>()?;
96    m.add_class::<crate::common::enums::CoinbaseMarginType>()?;
97    m.add_class::<CoinbaseDataClientConfig>()?;
98    m.add_class::<CoinbaseDataClientFactory>()?;
99    m.add_class::<CoinbaseExecutionClientConfig>()?;
100    m.add_class::<CoinbaseExecutionClientFactory>()?;
101
102    let registry = get_global_pyo3_registry();
103
104    if let Err(e) =
105        registry.register_factory_extractor(COINBASE.to_string(), extract_coinbase_data_factory)
106    {
107        return Err(to_pyruntime_err(format!(
108            "Failed to register Coinbase data factory extractor: {e}"
109        )));
110    }
111
112    if let Err(e) = registry
113        .register_exec_factory_extractor(COINBASE.to_string(), extract_coinbase_exec_factory)
114    {
115        return Err(to_pyruntime_err(format!(
116            "Failed to register Coinbase exec factory extractor: {e}"
117        )));
118    }
119
120    if let Err(e) = registry.register_config_extractor(
121        "CoinbaseDataClientConfig".to_string(),
122        extract_coinbase_data_config,
123    ) {
124        return Err(to_pyruntime_err(format!(
125            "Failed to register Coinbase data config extractor: {e}"
126        )));
127    }
128
129    if let Err(e) = registry.register_config_extractor(
130        "CoinbaseExecutionClientConfig".to_string(),
131        extract_coinbase_exec_config,
132    ) {
133        return Err(to_pyruntime_err(format!(
134            "Failed to register Coinbase exec config extractor: {e}"
135        )));
136    }
137
138    Ok(())
139}