nautilus_blockchain/
config.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
16use std::any::Any;
17
18use nautilus_infrastructure::sql::pg::PostgresConnectOptions;
19use nautilus_model::{
20    defi::{Chain, DexType, SharedChain},
21    identifiers::{AccountId, TraderId},
22};
23use nautilus_system::ClientConfig;
24
25/// Defines filtering criteria for the DEX pool universe that the data client will operate on.
26#[derive(Debug, Clone)]
27#[cfg_attr(
28    feature = "python",
29    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.blockchain")
30)]
31#[cfg_attr(
32    feature = "python",
33    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
34)]
35pub struct DexPoolFilters {
36    /// Whether to exclude pools containing tokens with empty name or symbol fields.
37    pub remove_pools_with_empty_erc20fields: bool,
38}
39
40impl DexPoolFilters {
41    /// Creates a new [`DexPoolFilters`] instance.
42    #[must_use]
43    pub fn new(remove_pools_with_empty_erc20fields: Option<bool>) -> Self {
44        Self {
45            remove_pools_with_empty_erc20fields: remove_pools_with_empty_erc20fields
46                .unwrap_or(true),
47        }
48    }
49}
50
51impl Default for DexPoolFilters {
52    fn default() -> Self {
53        Self {
54            remove_pools_with_empty_erc20fields: true,
55        }
56    }
57}
58
59/// Configuration for blockchain data clients.
60#[derive(Debug, Clone)]
61#[cfg_attr(
62    feature = "python",
63    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.blockchain")
64)]
65#[cfg_attr(
66    feature = "python",
67    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
68)]
69pub struct BlockchainDataClientConfig {
70    /// The blockchain chain configuration.
71    pub chain: SharedChain,
72    /// List of decentralized exchange IDs to register and sync during connection.
73    pub dex_ids: Vec<DexType>,
74    /// Determines if the client should use Hypersync for live data streaming.
75    pub use_hypersync_for_live_data: bool,
76    /// The HTTP URL for the blockchain RPC endpoint.
77    pub http_rpc_url: String,
78    /// The maximum number of RPC requests allowed per second.
79    pub rpc_requests_per_second: Option<u32>,
80    /// The maximum number of Multicall calls per one RPC request.
81    pub multicall_calls_per_rpc_request: u32,
82    /// The WebSocket secure URL for the blockchain RPC endpoint.
83    pub wss_rpc_url: Option<String>,
84    /// Optional HTTP proxy URL for RPC requests.
85    pub http_proxy_url: Option<String>,
86    /// Optional WebSocket proxy URL for RPC connections.
87    ///
88    /// Note: WebSocket proxy support is not yet implemented. This field is reserved
89    /// for future functionality. Use `http_proxy_url` for REST API proxy support.
90    pub ws_proxy_url: Option<String>,
91    /// The block from which to sync historical data.
92    pub from_block: Option<u64>,
93    /// Filtering criteria that define which DEX pools to include in the data universe.
94    pub pool_filters: DexPoolFilters,
95    /// Optional configuration for data client's Postgres cache database
96    pub postgres_cache_database_config: Option<PostgresConnectOptions>,
97}
98
99impl BlockchainDataClientConfig {
100    /// Creates a new [`BlockchainDataClientConfig`] instance.
101    #[allow(clippy::too_many_arguments)]
102    #[must_use]
103    pub fn new(
104        chain: SharedChain,
105        dex_ids: Vec<DexType>,
106        http_rpc_url: String,
107        rpc_requests_per_second: Option<u32>,
108        multicall_calls_per_rpc_request: Option<u32>,
109        wss_rpc_url: Option<String>,
110        use_hypersync_for_live_data: bool,
111        from_block: Option<u64>,
112        pools_filters: Option<DexPoolFilters>,
113        postgres_cache_database_config: Option<PostgresConnectOptions>,
114    ) -> Self {
115        Self {
116            chain,
117            dex_ids,
118            use_hypersync_for_live_data,
119            http_rpc_url,
120            rpc_requests_per_second,
121            multicall_calls_per_rpc_request: multicall_calls_per_rpc_request.unwrap_or(200),
122            wss_rpc_url,
123            http_proxy_url: None,
124            ws_proxy_url: None,
125            from_block,
126            pool_filters: pools_filters.unwrap_or_default(),
127            postgres_cache_database_config,
128        }
129    }
130}
131
132#[derive(Debug, Clone)]
133pub struct BlockchainExecutionClientConfig {
134    /// The trader ID for the client.
135    pub trader_id: TraderId,
136    /// The account ID for the client.
137    pub client_id: AccountId,
138    /// The blockchain chain configuration.
139    pub chain: Chain,
140    /// The wallet address of the execution client.
141    pub wallet_address: String,
142    /// The HTTP URL for the blockchain RPC endpoint.
143    pub http_rpc_url: String,
144    /// The maximum number of RPC requests allowed per second.
145    pub rpc_requests_per_second: Option<u32>,
146}
147
148impl BlockchainExecutionClientConfig {
149    pub fn new(
150        trader_id: TraderId,
151        client_id: AccountId,
152        chain: Chain,
153        wallet_address: String,
154        http_rpc_url: String,
155        rpc_requests_per_second: Option<u32>,
156    ) -> Self {
157        Self {
158            trader_id,
159            client_id,
160            chain,
161            wallet_address,
162            http_rpc_url,
163            rpc_requests_per_second,
164        }
165    }
166}
167
168impl ClientConfig for BlockchainExecutionClientConfig {
169    fn as_any(&self) -> &dyn Any {
170        self
171    }
172}