Skip to main content

nautilus_blockchain/python/
config.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 for blockchain configuration.
17
18use std::sync::Arc;
19
20use nautilus_infrastructure::sql::pg::PostgresConnectOptions;
21use nautilus_model::defi::{Chain, DexType};
22use pyo3::prelude::*;
23
24use crate::config::{BlockchainDataClientConfig, DexPoolFilters};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods(module = "nautilus_trader.blockchain")]
28impl DexPoolFilters {
29    /// Defines filtering criteria for the DEX pool universe that the data client will operate on.
30    #[new]
31    #[must_use]
32    pub fn py_new(remove_pools_with_empty_erc20_fields: Option<bool>) -> Self {
33        Self::builder()
34            .maybe_remove_pools_with_empty_erc20fields(remove_pools_with_empty_erc20_fields)
35            .build()
36    }
37}
38
39#[pymethods]
40#[pyo3_stub_gen::derive::gen_stub_pymethods(module = "nautilus_trader.blockchain")]
41impl BlockchainDataClientConfig {
42    /// Configuration for blockchain data clients.
43    #[new]
44    #[expect(clippy::too_many_arguments)]
45    #[pyo3(signature = (chain, dex_ids, http_rpc_url, rpc_requests_per_second=None, multicall_calls_per_rpc_request=None, wss_rpc_url=None, use_hypersync_for_live_data=true, from_block=None, pool_filters=None, postgres_cache_database_config=None, proxy_url=None))]
46    fn py_new(
47        #[gen_stub(
48            override_type(
49                type_repr = "nautilus_trader.model.Chain",
50                imports = ("nautilus_trader.model",),
51            ),
52        )]
53        chain: &Chain,
54        #[gen_stub(
55            override_type(
56                type_repr = "typing.Sequence[nautilus_trader.model.DexType]",
57                imports = ("typing", "nautilus_trader.model"),
58            ),
59        )]
60        dex_ids: Vec<DexType>,
61        http_rpc_url: String,
62        rpc_requests_per_second: Option<u32>,
63        multicall_calls_per_rpc_request: Option<u32>,
64        wss_rpc_url: Option<String>,
65        use_hypersync_for_live_data: bool,
66        from_block: Option<u64>,
67        pool_filters: Option<DexPoolFilters>,
68        #[gen_stub(
69            override_type(
70                type_repr = "typing.Optional[nautilus_trader.infrastructure.PostgresConnectOptions]",
71                imports = ("typing", "nautilus_trader.infrastructure"),
72            ),
73        )]
74        postgres_cache_database_config: Option<PostgresConnectOptions>,
75        proxy_url: Option<String>,
76    ) -> Self {
77        Self::builder()
78            .chain(Arc::new(chain.clone()))
79            .dex_ids(dex_ids)
80            .http_rpc_url(http_rpc_url)
81            .maybe_rpc_requests_per_second(rpc_requests_per_second)
82            .maybe_multicall_calls_per_rpc_request(multicall_calls_per_rpc_request)
83            .maybe_wss_rpc_url(wss_rpc_url)
84            .use_hypersync_for_live_data(use_hypersync_for_live_data)
85            .maybe_from_block(from_block)
86            .maybe_pool_filters(pool_filters)
87            .maybe_postgres_cache_database_config(postgres_cache_database_config)
88            .maybe_proxy_url(proxy_url)
89            .build()
90    }
91
92    /// Returns the chain configuration.
93    #[getter]
94    #[gen_stub(
95        override_return_type(
96            type_repr = "nautilus_trader.model.Chain",
97            imports = ("nautilus_trader.model",),
98        ),
99    )]
100    fn chain(&self) -> Chain {
101        (*self.chain).clone()
102    }
103
104    /// Returns the HTTP RPC URL.
105    #[getter]
106    fn http_rpc_url(&self) -> String {
107        self.http_rpc_url.clone()
108    }
109
110    /// Returns the WebSocket RPC URL.
111    #[getter]
112    fn wss_rpc_url(&self) -> Option<String> {
113        self.wss_rpc_url.clone()
114    }
115
116    /// Returns the RPC requests per second limit.
117    #[getter]
118    const fn rpc_requests_per_second(&self) -> Option<u32> {
119        self.rpc_requests_per_second
120    }
121
122    /// Returns whether to use HyperSync for live data.
123    #[getter]
124    const fn use_hypersync_for_live_data(&self) -> bool {
125        self.use_hypersync_for_live_data
126    }
127
128    /// Returns the starting block for sync.
129    #[getter]
130    #[expect(clippy::wrong_self_convention)]
131    const fn from_block(&self) -> Option<u64> {
132        self.from_block
133    }
134
135    /// Returns the optional proxy URL for HTTP and WebSocket transports.
136    #[getter]
137    fn proxy_url(&self) -> Option<String> {
138        self.proxy_url.clone()
139    }
140
141    /// Returns a string representation of the configuration.
142    fn __repr__(&self) -> String {
143        format!(
144            "BlockchainDataClientConfig(chain={:?}, http_rpc_url={}, wss_rpc_url={:?}, use_hypersync_for_live_data={}, from_block={:?})",
145            self.chain.name,
146            self.http_rpc_url,
147            self.wss_rpc_url,
148            self.use_hypersync_for_live_data,
149            self.from_block
150        )
151    }
152}