loom_core_topology/
topology_config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::collections::HashMap;
use std::fs;

use alloy_provider::RootProvider;
use alloy_transport::BoxTransport;
use eyre::Result;
use loom_broadcast_flashbots::client::RelayConfig;
use serde::Deserialize;
use strum_macros::Display;

#[derive(Debug, Deserialize)]
pub struct BlockchainConfig {
    pub chain_id: Option<i64>,
}

#[derive(Clone, Debug, Default, Deserialize, Display)]
#[strum(ascii_case_insensitive, serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum NodeType {
    #[default]
    Geth,
    Reth,
}

#[derive(Clone, Debug, Default, Deserialize, Display)]
#[strum(ascii_case_insensitive, serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum TransportType {
    #[default]
    #[serde(rename = "ws")]
    Ws,
    #[serde(rename = "http")]
    Http,
    #[serde(rename = "ipc")]
    Ipc,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct InfluxDbConfig {
    pub url: String,
    pub database: String,
    pub tags: HashMap<String, String>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct ClientConfigParams {
    pub url: String,
    pub node: NodeType,
    pub transport: TransportType,
    pub db_path: Option<String>,
    pub exex: Option<String>,
    #[serde(skip)]
    pub provider: Option<RootProvider<BoxTransport>>,
}

impl ClientConfigParams {
    pub fn client(&self) -> Option<&RootProvider<BoxTransport>> {
        self.provider.as_ref()
    }
}

#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum ClientConfig {
    String(String),
    Params(ClientConfigParams),
}

impl ClientConfig {
    pub fn url(&self) -> String {
        match &self {
            Self::String(s) => s.clone(),
            ClientConfig::Params(p) => p.url.clone(),
        }
    }

    pub fn config_params(&self) -> ClientConfigParams {
        match self {
            ClientConfig::String(s) => ClientConfigParams { url: s.clone(), ..ClientConfigParams::default() },
            ClientConfig::Params(p) => p.clone(),
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct EnvSingerConfig {
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum SignersConfig {
    #[serde(rename = "env")]
    Env(EnvSingerConfig),
}

#[derive(Debug, Deserialize)]
pub struct PreloaderConfig {
    pub client: Option<String>,
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub encoder: Option<String>,
    pub signers: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct SwapStepEncoderConfig {
    pub address: String,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum EncoderConfig {
    #[serde(rename = "swapstep")]
    SwapStep(SwapStepEncoderConfig),
}

#[derive(Debug, Deserialize)]
pub struct BlockchainClientConfig {
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub client: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct ExExClientConfig {
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub url: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct FlashbotsRelayConfig {
    id: u16,
    name: String,
    url: String,
    no_sign: Option<bool>,
}

impl From<FlashbotsRelayConfig> for RelayConfig {
    fn from(config: FlashbotsRelayConfig) -> Self {
        RelayConfig { id: config.id, name: config.name, url: config.url, no_sign: config.no_sign }
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct FlashbotsBroadcasterConfig {
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub client: Option<String>,
    pub smart: Option<bool>,
    pub relays: Option<Vec<FlashbotsRelayConfig>>,
}

impl FlashbotsBroadcasterConfig {
    pub fn relays(&self) -> Vec<RelayConfig> {
        self.relays.as_ref().map(|relays| relays.iter().map(|r| r.clone().into()).collect()).unwrap_or_default()
    }
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum BroadcasterConfig {
    #[serde(rename = "flashbots")]
    Flashbots(FlashbotsBroadcasterConfig),
}

#[derive(Debug, Deserialize)]
pub struct EvmEstimatorConfig {
    pub client: Option<String>,
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub encoder: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct GethEstimatorConfig {
    pub client: Option<String>,
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub encoder: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum EstimatorConfig {
    #[serde(rename = "evm")]
    Evm(EvmEstimatorConfig),
    #[serde(rename = "geth")]
    Geth(GethEstimatorConfig),
}

#[derive(Debug, Deserialize)]
pub struct PoolsConfig {
    #[serde(rename = "bc")]
    pub blockchain: Option<String>,
    pub client: Option<String>,
    pub history: bool,
    pub new: bool,
    pub protocol: bool,
}

#[derive(Debug, Deserialize)]
pub struct WebserverConfig {
    pub host: String,
}

impl Default for WebserverConfig {
    fn default() -> Self {
        WebserverConfig { host: "127.0.0.1:3333".to_string() }
    }
}

#[derive(Debug, Deserialize)]
pub struct DatabaseConfig {
    pub url: String,
}

#[derive(Debug, Deserialize)]
pub struct ActorConfig {
    pub broadcaster: Option<HashMap<String, BroadcasterConfig>>,
    pub node: Option<HashMap<String, BlockchainClientConfig>>,
    pub node_exex: Option<HashMap<String, ExExClientConfig>>,
    pub mempool: Option<HashMap<String, BlockchainClientConfig>>,
    pub price: Option<HashMap<String, BlockchainClientConfig>>,
    pub pools: Option<HashMap<String, PoolsConfig>>,
    pub noncebalance: Option<HashMap<String, BlockchainClientConfig>>,
    pub estimator: Option<HashMap<String, EstimatorConfig>>,
}

#[derive(Debug, Deserialize)]
pub struct TopologyConfig {
    pub influxdb: Option<InfluxDbConfig>,
    pub clients: HashMap<String, ClientConfig>,
    pub blockchains: HashMap<String, BlockchainConfig>,
    pub actors: ActorConfig,
    pub signers: HashMap<String, SignersConfig>,
    pub encoders: HashMap<String, EncoderConfig>,
    pub preloaders: Option<HashMap<String, PreloaderConfig>>,
    pub webserver: Option<WebserverConfig>,
    pub database: Option<DatabaseConfig>,
}

impl TopologyConfig {
    pub fn load_from_file(file_name: String) -> Result<TopologyConfig> {
        let contents = fs::read_to_string(file_name)?;
        let config: TopologyConfig = toml::from_str(&contents)?;
        Ok(config)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_load() {
        match TopologyConfig::load_from_file("../../config.toml".to_string()) {
            Ok(c) => {
                println!("{:?}", c);
            }
            Err(e) => {
                println!("Error:{e}")
            }
        }
    }
}