1use 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#[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 pub reth_provider: R,
42 pub provider: P,
44 pub evm_config: Evm,
46 pub blockchain: Blockchain,
48 pub blockchain_state: BlockchainState<DB, EthPrimitives>,
50 pub channels: MevComponentChannels<DB>,
52 pub market: Arc<RwLock<Market>>,
54 pub market_state: Arc<RwLock<MarketState<DB>>>,
56 pub mempool: Arc<RwLock<Mempool<EthPrimitives>>>,
58 pub block_history: Arc<RwLock<BlockHistory<DB, EthPrimitives>>>,
60 pub config: NodeConfig,
62}
63
64#[derive(Clone)]
66pub struct NodeConfig {
67 pub chain_id: u64,
69 pub chain_params: ChainParameters,
71 pub config: KabuConfig,
73 pub backrun_config: BackrunConfig,
75 pub multicaller_address: Address,
77 pub swap_encoder: MulticallerSwapEncoder,
79 pub db_pool: Option<DbPool>,
81 pub enable_web_server: bool,
83 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 pub fn new() -> Self {
96 let multicaller_address = Address::default();
97 let swap_encoder = MulticallerSwapEncoder::default_with_address(multicaller_address);
98
99 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 let config = KabuConfig {
106 remote_node: None, 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, 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 pub fn chain_id(mut self, chain_id: u64) -> Self {
130 self.chain_id = chain_id;
131 self
132 }
133
134 pub fn chain_params(mut self, chain_params: ChainParameters) -> Self {
136 self.chain_params = chain_params;
137 self
138 }
139
140 pub fn config(mut self, config: KabuConfig) -> Self {
142 self.config = config;
143 self
144 }
145
146 pub fn backrun_config(mut self, backrun_config: BackrunConfig) -> Self {
148 self.backrun_config = backrun_config;
149 self
150 }
151
152 pub fn multicaller_address(mut self, multicaller_address: Address) -> Self {
154 self.multicaller_address = multicaller_address;
155 self
156 }
157
158 pub fn swap_encoder(mut self, swap_encoder: MulticallerSwapEncoder) -> Self {
160 self.swap_encoder = swap_encoder;
161 self
162 }
163
164 pub fn db_pool(mut self, db_pool: DbPool) -> Self {
166 self.db_pool = Some(db_pool);
167 self
168 }
169
170 pub fn enable_web_server(mut self, enable: bool) -> Self {
172 self.enable_web_server = enable;
173 self
174 }
175
176 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 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 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 pub fn chain_params(&self) -> &ChainParameters {
231 &self.config.chain_params
232 }
233}