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
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Chain {
    pub name: String,
    pub short_name: String,
    pub chain: String,
    pub chain_id: u64,
    #[serde(default)]
    pub rpc: Vec<String>,
}

const CHAINS_JSON: &str = include_str!("chains.json");

pub fn chains() -> Result<Vec<Chain>, anyhow::Error> {
    let chains = serde_json::from_str(CHAINS_JSON)?;
    Ok(chains)
}

pub fn chain_from_str(value: &str) -> Result<Chain, anyhow::Error> {
    let chains = chains()?;

    let chain = chains
        .into_iter()
        .find(|chain| {
            vec![
                chain.name.to_lowercase(),
                chain.chain.to_lowercase(),
                chain.short_name.to_lowercase(),
                chain.chain_id.to_string(),
            ]
            .contains(&value.to_lowercase())
        })
        .ok_or_else(|| anyhow::anyhow!("Chain not found"))?;
    Ok(chain)
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_chain_name() {
        use super::chain_from_str;
        chain_from_str("eth").expect("could not get eth chain");
        chain_from_str("matic").expect("could not get matic chain");
        chain_from_str("goerli").expect("could not get goerli chain");
        chain_from_str("gor").expect("could not get goerli chain");
    }

    #[test]
    fn test_chain_number() {
        use super::chain_from_str;
        chain_from_str("1").expect("could not get chain 1");
        chain_from_str("137").expect("could not get chain 137");
    }
}