Skip to main content

nautilus_dydx/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#![expect(
19    clippy::missing_errors_doc,
20    reason = "errors documented on underlying Rust methods"
21)]
22
23pub mod config;
24pub mod encoder;
25pub mod enums;
26pub mod factories;
27pub mod grpc;
28pub mod http;
29pub mod submitter;
30pub mod types;
31pub mod urls;
32pub mod wallet;
33pub mod websocket;
34
35use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
36use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
37use nautilus_system::get_global_pyo3_registry;
38use pyo3::prelude::*;
39
40use crate::{
41    config::{DydxDataClientConfig, DydxExecClientConfig},
42    factories::{DydxDataClientFactory, DydxExecutionClientFactory},
43};
44
45#[expect(clippy::needless_pass_by_value)]
46fn extract_dydx_data_factory(
47    py: Python<'_>,
48    factory: Py<PyAny>,
49) -> PyResult<Box<dyn DataClientFactory>> {
50    match factory.extract::<DydxDataClientFactory>(py) {
51        Ok(f) => Ok(Box::new(f)),
52        Err(e) => Err(to_pyvalue_err(format!(
53            "Failed to extract DydxDataClientFactory: {e}"
54        ))),
55    }
56}
57
58#[expect(clippy::needless_pass_by_value)]
59fn extract_dydx_exec_factory(
60    py: Python<'_>,
61    factory: Py<PyAny>,
62) -> PyResult<Box<dyn ExecutionClientFactory>> {
63    match factory.extract::<DydxExecutionClientFactory>(py) {
64        Ok(f) => Ok(Box::new(f)),
65        Err(e) => Err(to_pyvalue_err(format!(
66            "Failed to extract DydxExecutionClientFactory: {e}"
67        ))),
68    }
69}
70
71#[expect(clippy::needless_pass_by_value)]
72fn extract_dydx_data_config(py: Python<'_>, config: Py<PyAny>) -> PyResult<Box<dyn ClientConfig>> {
73    match config.extract::<DydxDataClientConfig>(py) {
74        Ok(c) => Ok(Box::new(c)),
75        Err(e) => Err(to_pyvalue_err(format!(
76            "Failed to extract DydxDataClientConfig: {e}"
77        ))),
78    }
79}
80
81#[expect(clippy::needless_pass_by_value)]
82fn extract_dydx_exec_config(py: Python<'_>, config: Py<PyAny>) -> PyResult<Box<dyn ClientConfig>> {
83    match config.extract::<DydxExecClientConfig>(py) {
84        Ok(c) => Ok(Box::new(c)),
85        Err(e) => Err(to_pyvalue_err(format!(
86            "Failed to extract DydxExecClientConfig: {e}"
87        ))),
88    }
89}
90
91#[pymodule]
92pub fn dydx(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
93    m.add_class::<crate::http::client::DydxHttpClient>()?;
94    m.add_class::<crate::websocket::client::DydxWebSocketClient>()?;
95    m.add_class::<crate::common::enums::DydxNetwork>()?;
96    m.add_class::<crate::common::enums::DydxOrderSide>()?;
97    m.add_class::<crate::common::enums::DydxOrderType>()?;
98    m.add_class::<crate::types::DydxOraclePrice>()?;
99    m.add_class::<wallet::PyDydxWallet>()?;
100    m.add_class::<grpc::PyDydxGrpcClient>()?;
101    m.add_class::<submitter::PyDydxOrderSubmitter>()?;
102    m.add_class::<encoder::PyDydxClientOrderIdEncoder>()?;
103    m.add_class::<DydxDataClientConfig>()?;
104    m.add_class::<DydxExecClientConfig>()?;
105    m.add_class::<DydxDataClientFactory>()?;
106    m.add_class::<DydxExecutionClientFactory>()?;
107    m.add_function(wrap_pyfunction!(urls::py_get_dydx_grpc_urls, m)?)?;
108    m.add_function(wrap_pyfunction!(urls::py_get_dydx_grpc_url, m)?)?;
109    m.add_function(wrap_pyfunction!(urls::py_get_dydx_http_url, m)?)?;
110    m.add_function(wrap_pyfunction!(urls::py_get_dydx_ws_url, m)?)?;
111
112    let registry = get_global_pyo3_registry();
113
114    if let Err(e) =
115        registry.register_factory_extractor("DYDX".to_string(), extract_dydx_data_factory)
116    {
117        return Err(to_pyruntime_err(format!(
118            "Failed to register dYdX data factory extractor: {e}"
119        )));
120    }
121
122    if let Err(e) =
123        registry.register_exec_factory_extractor("DYDX".to_string(), extract_dydx_exec_factory)
124    {
125        return Err(to_pyruntime_err(format!(
126            "Failed to register dYdX exec factory extractor: {e}"
127        )));
128    }
129
130    if let Err(e) = registry
131        .register_config_extractor("DydxDataClientConfig".to_string(), extract_dydx_data_config)
132    {
133        return Err(to_pyruntime_err(format!(
134            "Failed to register dYdX data config extractor: {e}"
135        )));
136    }
137
138    if let Err(e) = registry
139        .register_config_extractor("DydxExecClientConfig".to_string(), extract_dydx_exec_config)
140    {
141        return Err(to_pyruntime_err(format!(
142            "Failed to register dYdX exec config extractor: {e}"
143        )));
144    }
145
146    Ok(())
147}