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