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