Skip to main content

nautilus_blockchain/
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
16use std::any::Any;
17
18use nautilus_common::factories::ClientConfig;
19use nautilus_infrastructure::sql::pg::PostgresConnectOptions;
20use nautilus_model::{
21    defi::{Chain, DexType, SharedChain},
22    identifiers::{AccountId, TraderId},
23};
24use nautilus_network::websocket::TransportBackend;
25use serde::{Deserialize, Serialize};
26
27/// Defines filtering criteria for the DEX pool universe that the data client will operate on.
28#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
29#[serde(default, deny_unknown_fields)]
30#[cfg_attr(
31    feature = "python",
32    pyo3::pyclass(
33        module = "nautilus_trader.core.nautilus_pyo3.blockchain",
34        from_py_object
35    )
36)]
37#[cfg_attr(
38    feature = "python",
39    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
40)]
41pub struct DexPoolFilters {
42    /// Whether to exclude pools containing tokens with empty name or symbol fields.
43    #[builder(default = true)]
44    pub remove_pools_with_empty_erc20fields: bool,
45}
46
47impl Default for DexPoolFilters {
48    fn default() -> Self {
49        Self::builder().build()
50    }
51}
52
53/// Configuration for blockchain data clients.
54#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
55#[serde(deny_unknown_fields)]
56#[cfg_attr(
57    feature = "python",
58    pyo3::pyclass(
59        module = "nautilus_trader.core.nautilus_pyo3.blockchain",
60        from_py_object
61    )
62)]
63#[cfg_attr(
64    feature = "python",
65    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
66)]
67pub struct BlockchainDataClientConfig {
68    /// The blockchain chain configuration.
69    pub chain: SharedChain,
70    /// List of decentralized exchange IDs to register and sync during connection.
71    #[builder(default)]
72    #[serde(default)]
73    pub dex_ids: Vec<DexType>,
74    /// Determines if the client should use Hypersync for live data streaming.
75    #[builder(default)]
76    #[serde(default)]
77    pub use_hypersync_for_live_data: bool,
78    /// The HTTP URL for the blockchain RPC endpoint.
79    pub http_rpc_url: String,
80    /// The maximum number of RPC requests allowed per second.
81    pub rpc_requests_per_second: Option<u32>,
82    /// The maximum number of Multicall calls per one RPC request.
83    #[builder(default = 200)]
84    #[serde(default = "default_multicall_calls_per_rpc_request")]
85    pub multicall_calls_per_rpc_request: u32,
86    /// The WebSocket secure URL for the blockchain RPC endpoint.
87    pub wss_rpc_url: Option<String>,
88    /// Optional proxy URL for HTTP and WebSocket transports.
89    pub proxy_url: Option<String>,
90    /// The block from which to sync historical data.
91    pub from_block: Option<u64>,
92    /// Filtering criteria that define which DEX pools to include in the data universe.
93    #[builder(default)]
94    #[serde(default)]
95    pub pool_filters: DexPoolFilters,
96    /// Optional configuration for data client's Postgres cache database
97    pub postgres_cache_database_config: Option<PostgresConnectOptions>,
98    /// WebSocket transport backend (defaults to `Tungstenite`).
99    #[builder(default)]
100    #[serde(default)]
101    pub transport_backend: TransportBackend,
102}
103
104const fn default_multicall_calls_per_rpc_request() -> u32 {
105    200
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
109#[serde(deny_unknown_fields)]
110pub struct BlockchainExecutionClientConfig {
111    /// The trader ID for the client.
112    pub trader_id: TraderId,
113    /// The account ID for the client.
114    pub client_id: AccountId,
115    /// The blockchain chain configuration.
116    pub chain: Chain,
117    /// The wallet address of the execution client.
118    pub wallet_address: String,
119    /// Token universe: set of ERC-20 token addresses to monitor for balance tracking.
120    pub tokens: Option<Vec<String>>,
121    /// The HTTP URL for the blockchain RPC endpoint.
122    pub http_rpc_url: String,
123    /// The maximum number of RPC requests allowed per second.
124    pub rpc_requests_per_second: Option<u32>,
125    /// WebSocket transport backend (defaults to `Tungstenite`).
126    #[builder(default)]
127    #[serde(default)]
128    pub transport_backend: TransportBackend,
129}
130
131impl ClientConfig for BlockchainExecutionClientConfig {
132    fn as_any(&self) -> &dyn Any {
133        self
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use rstest::rstest;
140
141    use super::*;
142
143    #[rstest]
144    fn test_data_config_toml_minimal() {
145        let config: BlockchainDataClientConfig = toml::from_str(
146            r#"
147http_rpc_url = "https://eth-mainnet.example.com"
148
149[chain]
150name = "Ethereum"
151chain_id = 1
152hypersync_url = "https://1.hypersync.xyz"
153native_currency_decimals = 18
154"#,
155        )
156        .unwrap();
157
158        assert_eq!(config.http_rpc_url, "https://eth-mainnet.example.com");
159        assert_eq!(config.chain.chain_id, 1);
160        assert!(config.dex_ids.is_empty());
161        assert!(!config.use_hypersync_for_live_data);
162        assert_eq!(config.multicall_calls_per_rpc_request, 200);
163        assert!(config.pool_filters.remove_pools_with_empty_erc20fields);
164        assert_eq!(config.transport_backend, TransportBackend::default());
165    }
166
167    #[rstest]
168    fn test_execution_config_toml_minimal() {
169        let config: BlockchainExecutionClientConfig = toml::from_str(
170            r#"
171trader_id = "TRADER-001"
172client_id = "BLOCKCHAIN-001"
173wallet_address = "0x0000000000000000000000000000000000000000"
174http_rpc_url = "https://eth-mainnet.example.com"
175
176[chain]
177name = "Ethereum"
178chain_id = 1
179hypersync_url = "https://1.hypersync.xyz"
180native_currency_decimals = 18
181"#,
182        )
183        .unwrap();
184
185        assert_eq!(config.http_rpc_url, "https://eth-mainnet.example.com");
186        assert_eq!(config.chain.chain_id, 1);
187        assert_eq!(
188            config.wallet_address,
189            "0x0000000000000000000000000000000000000000",
190        );
191        assert!(config.tokens.is_none());
192        assert!(config.rpc_requests_per_second.is_none());
193        assert_eq!(config.transport_backend, TransportBackend::default());
194    }
195}