kabu_core_node/
context.rs

1//! Context for Kabu node configuration
2
3use alloy_network::Ethereum;
4use alloy_primitives::Address;
5use alloy_provider::Provider;
6use std::sync::Arc;
7use tokio::sync::RwLock;
8
9use kabu_core_blockchain::{Blockchain, BlockchainState};
10use kabu_core_components::MevComponentChannels;
11use kabu_core_config::KabuConfig;
12use kabu_evm_db::{DatabaseKabuExt, KabuDBError};
13use kabu_execution_multicaller::MulticallerSwapEncoder;
14use kabu_storage_db::DbPool;
15use kabu_strategy_backrun::BackrunConfig;
16use kabu_types_blockchain::{ChainParameters, Mempool};
17use kabu_types_entities::{BlockHistory, BlockHistoryState};
18use kabu_types_market::{Market, MarketState};
19use reth::revm::{Database, DatabaseCommit, DatabaseRef};
20use reth_ethereum_primitives::EthPrimitives;
21
22/// Context that holds all shared resources for node components
23#[derive(Clone)]
24pub struct KabuContext<R, P, DB, Evm>
25where
26    R: Send + Sync + Clone + 'static,
27    P: Provider<Ethereum> + Send + Sync + Clone + 'static,
28    DB: DatabaseRef<Error = KabuDBError>
29        + Database<Error = KabuDBError>
30        + DatabaseCommit
31        + DatabaseKabuExt
32        + BlockHistoryState<EthPrimitives>
33        + Send
34        + Sync
35        + Clone
36        + Default
37        + 'static,
38    Evm: Clone + Send + Sync + 'static,
39{
40    /// Reth provider for state access and subscriptions
41    pub reth_provider: R,
42    /// Provider for blockchain access
43    pub provider: P,
44    /// EVM configuration
45    pub evm_config: Evm,
46    /// Blockchain state and event channels
47    pub blockchain: Blockchain,
48    /// Blockchain state with database
49    pub blockchain_state: BlockchainState<DB, EthPrimitives>,
50    /// MEV component communication channels
51    pub channels: MevComponentChannels<DB>,
52    /// Shared market state (tokens and pools)
53    pub market: Arc<RwLock<Market>>,
54    /// Shared market state with DB
55    pub market_state: Arc<RwLock<MarketState<DB>>>,
56    /// Shared mempool
57    pub mempool: Arc<RwLock<Mempool<EthPrimitives>>>,
58    /// Shared block history
59    pub block_history: Arc<RwLock<BlockHistory<DB, EthPrimitives>>>,
60    /// Configuration
61    pub config: NodeConfig,
62}
63
64/// Node configuration
65#[derive(Clone)]
66pub struct NodeConfig {
67    /// Chain ID
68    pub chain_id: u64,
69    /// Chain parameters
70    pub chain_params: ChainParameters,
71    /// Kabu configuration
72    pub config: KabuConfig,
73    /// Backrun strategy configuration
74    pub backrun_config: BackrunConfig,
75    /// Multicaller contract address
76    pub multicaller_address: Address,
77    /// Swap encoder
78    pub swap_encoder: MulticallerSwapEncoder,
79    /// Database pool (optional - for web server)
80    pub db_pool: Option<DbPool>,
81    /// Enable web server
82    pub enable_web_server: bool,
83    /// Pool loading configuration
84    pub pools_config: kabu_defi_pools::PoolsLoadingConfig,
85}
86
87impl Default for NodeConfig {
88    fn default() -> Self {
89        Self::new()
90    }
91}
92
93impl NodeConfig {
94    /// Create a new node configuration with defaults
95    pub fn new() -> Self {
96        let multicaller_address = Address::default();
97        let swap_encoder = MulticallerSwapEncoder::default_with_address(multicaller_address);
98
99        // Default pools config
100        let pools_config = kabu_defi_pools::PoolsLoadingConfig::disable_all(kabu_defi_pools::PoolsLoadingConfig::default())
101            .enable(kabu_types_market::PoolClass::UniswapV2)
102            .enable(kabu_types_market::PoolClass::UniswapV3);
103
104        // Create a minimal config with defaults
105        let config = KabuConfig {
106            remote_node: None, // No remote node for local/embedded mode
107            signers: Vec::new(),
108            builders: Vec::new(),
109            multicaller_address,
110            influxdb: None,
111            webserver: None,
112            database: None,
113        };
114
115        Self {
116            chain_id: 1, // Ethereum mainnet
117            chain_params: ChainParameters::ethereum(),
118            config,
119            backrun_config: BackrunConfig::default(),
120            multicaller_address,
121            swap_encoder,
122            db_pool: None,
123            enable_web_server: false,
124            pools_config,
125        }
126    }
127
128    /// Set the chain ID
129    pub fn chain_id(mut self, chain_id: u64) -> Self {
130        self.chain_id = chain_id;
131        self
132    }
133
134    /// Set the chain parameters
135    pub fn chain_params(mut self, chain_params: ChainParameters) -> Self {
136        self.chain_params = chain_params;
137        self
138    }
139
140    /// Set the configuration
141    pub fn config(mut self, config: KabuConfig) -> Self {
142        self.config = config;
143        self
144    }
145
146    /// Set the backrun configuration
147    pub fn backrun_config(mut self, backrun_config: BackrunConfig) -> Self {
148        self.backrun_config = backrun_config;
149        self
150    }
151
152    /// Set the multicaller address
153    pub fn multicaller_address(mut self, multicaller_address: Address) -> Self {
154        self.multicaller_address = multicaller_address;
155        self
156    }
157
158    /// Set the swap encoder
159    pub fn swap_encoder(mut self, swap_encoder: MulticallerSwapEncoder) -> Self {
160        self.swap_encoder = swap_encoder;
161        self
162    }
163
164    /// Set the database pool
165    pub fn db_pool(mut self, db_pool: DbPool) -> Self {
166        self.db_pool = Some(db_pool);
167        self
168    }
169
170    /// Enable web server
171    pub fn enable_web_server(mut self, enable: bool) -> Self {
172        self.enable_web_server = enable;
173        self
174    }
175
176    /// Set the pools configuration
177    pub fn pools_config(mut self, pools_config: kabu_defi_pools::PoolsLoadingConfig) -> Self {
178        self.pools_config = pools_config;
179        self
180    }
181}
182
183impl<R, P, DB, Evm> KabuContext<R, P, DB, Evm>
184where
185    R: Send + Sync + Clone + 'static,
186    P: Provider<Ethereum> + Send + Sync + Clone + 'static,
187    DB: DatabaseRef<Error = KabuDBError>
188        + Database<Error = KabuDBError>
189        + DatabaseCommit
190        + DatabaseKabuExt
191        + BlockHistoryState<EthPrimitives>
192        + Send
193        + Sync
194        + Clone
195        + Default
196        + 'static,
197    Evm: Clone + Send + Sync + 'static,
198{
199    /// Create a new context with the given configuration
200    pub fn new(
201        reth_provider: R,
202        provider: P,
203        evm_config: Evm,
204        blockchain: Blockchain,
205        blockchain_state: BlockchainState<DB, EthPrimitives>,
206        config: NodeConfig,
207    ) -> Self {
208        // Get shared state from blockchain
209        let market = blockchain.market();
210        let market_state = blockchain_state.market_state_commit();
211        let mempool = blockchain.mempool();
212        let block_history = Arc::new(RwLock::new(BlockHistory::new(10)));
213
214        Self {
215            reth_provider,
216            provider,
217            evm_config,
218            blockchain,
219            blockchain_state,
220            channels: MevComponentChannels::default(),
221            market,
222            market_state,
223            mempool,
224            block_history,
225            config,
226        }
227    }
228
229    /// Get the chain parameters
230    pub fn chain_params(&self) -> &ChainParameters {
231        &self.config.chain_params
232    }
233}