loom_anvil/test_config/
mod.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
use std::collections::HashMap;

use alloy_primitives::{Address, TxHash};
use eyre::Result;
use serde::Deserialize;
use tokio::fs;

use loom::types::entities::PoolClass;

#[derive(Deserialize, Debug)]
pub struct TestConfig {
    pub modules: Modules,
    pub settings: Settings,
    pub pools: HashMap<String, PoolConfig>,
    pub txs: HashMap<String, TransactionConfig>,
    pub tokens: HashMap<String, TokenConfig>,
    pub assertions: AssertionsConfig,
}

fn default_true() -> bool {
    true
}
fn default_false() -> bool {
    false
}

#[allow(dead_code)]
#[derive(Default, Deserialize, Debug, Clone)]
pub struct AssertionsConfig {
    pub swaps_encoded: Option<usize>,
    pub swaps_ok: Option<usize>,
    pub best_profit_eth: Option<f64>,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Modules {
    #[serde(default = "default_true")]
    pub price: bool,
    #[serde(default = "default_true")]
    pub signer: bool,
    #[serde(default = "default_true")]
    pub encoder: bool,
    #[serde(default = "default_true")]
    pub arb_path_merger: bool,
    #[serde(default = "default_true")]
    pub same_path_merger: bool,
    #[serde(default = "default_true")]
    pub diff_path_merger: bool,
    #[serde(default)]
    pub arb_block: bool,
    #[serde(default = "default_true")]
    pub arb_mempool: bool,
    #[serde(default = "default_false")]
    pub flashbots: bool,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct Settings {
    pub block: u64,
    pub coinbase: Option<Address>,
    pub multicaller: Option<Address>,
}

#[derive(Deserialize, Debug)]
pub struct PoolConfig {
    pub address: Address,
    pub class: PoolClass,
}

#[derive(Deserialize, Debug)]
pub struct TransactionConfig {
    pub hash: TxHash,
    pub send: String,
}

#[derive(Deserialize, Debug)]
pub struct TokenConfig {
    pub address: Address,
    pub symbol: Option<String>,
    pub name: Option<String>,
    pub decimals: Option<u8>,
    pub basic: Option<bool>,
    pub middle: Option<bool>,
    pub price: Option<f64>,
}

impl TestConfig {
    pub async fn from_file(filename: String) -> Result<TestConfig> {
        let toml_content = fs::read_to_string(filename.as_str()).await?;
        let config: TestConfig = toml::from_str(&toml_content)?;
        Ok(config)
    }
}

#[cfg(test)]
mod test {
    use crate::test_config::TestConfig;

    #[test]
    fn test_deserialization() {
        let cfg = r#"
[settings]
block = 19101579
coinbase = "0x1dd35b4da6534230ff53048f7477f17f7f4e7a70"
multicaller = "0x3dd35b4da6534230ff53048f7477f17f7f4e7a70"
skip_default = false

[modules]

[pools]
a = { address = "0x2dd35b4da6534230ff53048f7477f17f7f4e7a70", class = "uniswap2" }

[txs]
tx_1 = { hash = "0xf9fb98fe76dc5f4e836cdc3d80cd7902150a8609c617064f1447c3980fd6776b", send = "mempool" }
tx_2 = { hash = "0x1ec982c2d4eb5475192b26f7208b797328eab88f8e5be053f797f74bcb87a20c", send = "mempool" }

[tokens]
weth = { address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", symbol = "WETH", decimals = 18, basic = true, middle = false }

[assertions]
swaps_encoded = 14
swaps_ok = 11
best_profit_eth = 181.37
        "#;
        let config: TestConfig = toml::from_str(cfg).unwrap();
        assert_eq!(config.settings.block, 19101579);
        assert_eq!(config.assertions.swaps_encoded.unwrap_or_default(), 14);
        assert_eq!(config.assertions.swaps_ok.unwrap_or_default(), 11);
        assert_eq!(config.assertions.best_profit_eth.unwrap_or_default(), 181.37);
    }
}