Skip to main content

nautilus_blockchain/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](https://pyo3.rs).
17
18pub mod config;
19
20#[cfg(feature = "hypersync")]
21pub mod factories;
22
23#[cfg(feature = "hypersync")]
24use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
25#[cfg(feature = "hypersync")]
26use nautilus_system::{
27    factories::{ClientConfig, DataClientFactory},
28    get_global_pyo3_registry,
29};
30use pyo3::prelude::*;
31
32/// Extractor function for `BlockchainDataClientFactory`.
33#[cfg(feature = "hypersync")]
34fn extract_blockchain_factory(
35    py: Python<'_>,
36    factory: Py<PyAny>,
37) -> PyResult<Box<dyn DataClientFactory>> {
38    match factory.extract::<crate::factories::BlockchainDataClientFactory>(py) {
39        Ok(concrete_factory) => Ok(Box::new(concrete_factory)),
40        Err(e) => Err(to_pyvalue_err(format!(
41            "Failed to extract BlockchainDataClientFactory: {e}"
42        ))),
43    }
44}
45
46/// Extractor function for `BlockchainDataClientConfig`.
47#[cfg(feature = "hypersync")]
48fn extract_blockchain_config(py: Python<'_>, config: Py<PyAny>) -> PyResult<Box<dyn ClientConfig>> {
49    match config.extract::<crate::config::BlockchainDataClientConfig>(py) {
50        Ok(concrete_config) => Ok(Box::new(concrete_config)),
51        Err(e) => Err(to_pyvalue_err(format!(
52            "Failed to extract BlockchainDataClientConfig: {e}"
53        ))),
54    }
55}
56
57/// Loaded as `nautilus_pyo3.blockchain`.
58///
59/// # Errors
60///
61/// Returns a `PyErr` if registering any module components fails.
62#[pymodule]
63pub fn blockchain(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
64    m.add_class::<crate::config::BlockchainDataClientConfig>()?;
65    m.add_class::<crate::config::DexPoolFilters>()?;
66    #[cfg(feature = "hypersync")]
67    m.add_class::<crate::factories::BlockchainDataClientFactory>()?;
68
69    // Register extractors with the global registry
70    #[cfg(feature = "hypersync")]
71    {
72        let registry = get_global_pyo3_registry();
73
74        if let Err(e) = registry
75            .register_factory_extractor("BLOCKCHAIN".to_string(), extract_blockchain_factory)
76        {
77            return Err(to_pyruntime_err(format!(
78                "Failed to register blockchain factory extractor: {e}"
79            )));
80        }
81
82        if let Err(e) = registry.register_config_extractor(
83            "BlockchainDataClientConfig".to_string(),
84            extract_blockchain_config,
85        ) {
86            return Err(to_pyruntime_err(format!(
87                "Failed to register blockchain config extractor: {e}"
88            )));
89        }
90    }
91
92    Ok(())
93}