Skip to main content

nautilus_deribit/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 enums;
25pub mod factories;
26pub mod http;
27pub mod urls;
28pub mod websocket;
29
30use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
31use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
32use nautilus_system::get_global_pyo3_registry;
33use pyo3::prelude::*;
34
35use crate::{
36    config::{DeribitDataClientConfig, DeribitExecClientConfig},
37    factories::{DeribitDataClientFactory, DeribitExecutionClientFactory},
38};
39
40#[expect(clippy::needless_pass_by_value)]
41fn extract_deribit_data_factory(
42    py: Python<'_>,
43    factory: Py<PyAny>,
44) -> PyResult<Box<dyn DataClientFactory>> {
45    match factory.extract::<DeribitDataClientFactory>(py) {
46        Ok(f) => Ok(Box::new(f)),
47        Err(e) => Err(to_pyvalue_err(format!(
48            "Failed to extract DeribitDataClientFactory: {e}"
49        ))),
50    }
51}
52
53#[expect(clippy::needless_pass_by_value)]
54fn extract_deribit_exec_factory(
55    py: Python<'_>,
56    factory: Py<PyAny>,
57) -> PyResult<Box<dyn ExecutionClientFactory>> {
58    match factory.extract::<DeribitExecutionClientFactory>(py) {
59        Ok(f) => Ok(Box::new(f)),
60        Err(e) => Err(to_pyvalue_err(format!(
61            "Failed to extract DeribitExecutionClientFactory: {e}"
62        ))),
63    }
64}
65
66#[expect(clippy::needless_pass_by_value)]
67fn extract_deribit_data_config(
68    py: Python<'_>,
69    config: Py<PyAny>,
70) -> PyResult<Box<dyn ClientConfig>> {
71    match config.extract::<DeribitDataClientConfig>(py) {
72        Ok(c) => Ok(Box::new(c)),
73        Err(e) => Err(to_pyvalue_err(format!(
74            "Failed to extract DeribitDataClientConfig: {e}"
75        ))),
76    }
77}
78
79#[expect(clippy::needless_pass_by_value)]
80fn extract_deribit_exec_config(
81    py: Python<'_>,
82    config: Py<PyAny>,
83) -> PyResult<Box<dyn ClientConfig>> {
84    match config.extract::<DeribitExecClientConfig>(py) {
85        Ok(c) => Ok(Box::new(c)),
86        Err(e) => Err(to_pyvalue_err(format!(
87            "Failed to extract DeribitExecClientConfig: {e}"
88        ))),
89    }
90}
91
92/// Loaded as `nautilus_pyo3.deribit`.
93///
94/// # Errors
95///
96/// Returns an error if any bindings fail to register with the Python module.
97#[pymodule]
98pub fn deribit(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
99    m.add_class::<super::http::client::DeribitHttpClient>()?;
100    m.add_class::<super::websocket::client::DeribitWebSocketClient>()?;
101    m.add_class::<crate::common::enums::DeribitCurrency>()?;
102    m.add_class::<crate::common::enums::DeribitProductType>()?;
103    m.add_class::<crate::common::enums::DeribitEnvironment>()?;
104    m.add_class::<crate::websocket::enums::DeribitUpdateInterval>()?;
105    m.add_class::<DeribitDataClientConfig>()?;
106    m.add_class::<DeribitExecClientConfig>()?;
107    m.add_class::<DeribitDataClientFactory>()?;
108    m.add_class::<DeribitExecutionClientFactory>()?;
109    m.add_function(wrap_pyfunction!(urls::py_get_deribit_http_base_url, m)?)?;
110    m.add_function(wrap_pyfunction!(urls::py_get_deribit_ws_url, m)?)?;
111
112    let registry = get_global_pyo3_registry();
113
114    if let Err(e) =
115        registry.register_factory_extractor("DERIBIT".to_string(), extract_deribit_data_factory)
116    {
117        return Err(to_pyruntime_err(format!(
118            "Failed to register Deribit data factory extractor: {e}"
119        )));
120    }
121
122    if let Err(e) = registry
123        .register_exec_factory_extractor("DERIBIT".to_string(), extract_deribit_exec_factory)
124    {
125        return Err(to_pyruntime_err(format!(
126            "Failed to register Deribit exec factory extractor: {e}"
127        )));
128    }
129
130    if let Err(e) = registry.register_config_extractor(
131        "DeribitDataClientConfig".to_string(),
132        extract_deribit_data_config,
133    ) {
134        return Err(to_pyruntime_err(format!(
135            "Failed to register Deribit data config extractor: {e}"
136        )));
137    }
138
139    if let Err(e) = registry.register_config_extractor(
140        "DeribitExecClientConfig".to_string(),
141        extract_deribit_exec_config,
142    ) {
143        return Err(to_pyruntime_err(format!(
144            "Failed to register Deribit exec config extractor: {e}"
145        )));
146    }
147
148    Ok(())
149}