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
pub const ROOM_MARKET_ABI: &str = include_str!("../public/ABI/RoomMarket.json");
pub const TOKEN_ABI: &str = include_str!("../public/ABI/Token.json");
pub const SIMPLE_GAME_ABI: &str = include_str!("../public/ABI/SimpleGame.json");

/// Default network
pub const DEFAULT_NETWORK: Network = Network::Localhost;

/// Network types
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
pub enum Network {
    Localhost,
    Holesky,
    Sepolia,
    OpBNBTestnet,
    Other,
}

impl Network {
    /// Get the network from lower-case string
    pub fn from_str(s: &str) -> Self {
        match s {
            "localhost" => Network::Localhost,
            "holesky" => Network::Holesky,
            "sepolia" => Network::Sepolia,
            "opbnbtestnet" => Network::OpBNBTestnet,
            _ => Network::Other,
        }
    }

    pub fn to_str<'a>(&self) -> &'a str {
        match self {
            Network::Localhost => "localhost",
            Network::Holesky => "holesky",
            Network::Sepolia => "sepolia",
            Network::OpBNBTestnet => "opbnbtestnet",
            Network::Other => "other",
        }
    }

    pub fn from_chain_id(chain_id: u64) -> Self {
        match chain_id {
            17000 => Network::Holesky,
            11155111 => Network::Sepolia,
            5611 => Network::OpBNBTestnet,
            _ => DEFAULT_NETWORK,
        }
    }
}

/// Network native currency config
#[derive(Default)]
pub struct NetworkCurrency {
    /// Currency name
    pub name: String,
    /// Currency symbol
    pub symbol: String,
    /// Currency decimals
    pub decimals: i32,
}

/// Network config informato, use EIP-3085
#[derive(Default)]
pub struct NetworkConfig {
    /// Chain id
    pub chain_id: i32,
    /// Chain name
    pub chain_name: String,
    /// List of endpoints
    pub rpc_urls: Vec<String>,
    /// list of block explorer urls
    pub block_explorer_urls: Vec<String>,
    /// List of chain icon urls
    pub icon_urls: Vec<String>,
    /// Native currency config
    pub native_currency: NetworkCurrency,
}

impl NetworkConfig {
    /// Get the network config
    pub fn from(network: Network) -> NetworkConfig {
        match network {
            Network::Localhost => {
                let mut nc = NetworkConfig::default();
                nc.rpc_urls = vec!["http://127.0.0.1:8545".to_owned()];
                nc
            }
            Network::Holesky => NetworkConfig {
                chain_id: 17000,
                chain_name: "Holesky".to_owned(),
                rpc_urls: vec!["https://1rpc.io/holesky".to_owned()],
                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_ethereum.jpg".to_owned()],
                block_explorer_urls: vec!["https://holesky.beaconcha.in/".to_owned()],
                native_currency: NetworkCurrency {
                    name: "Holesky ETH".to_owned(),
                    symbol: "ETH".to_owned(),
                    decimals: 18,
                },
            },
            Network::Sepolia => NetworkConfig {
                chain_id: 11155111,
                chain_name: "Sepolia".to_owned(),
                rpc_urls: vec!["https://rpc.sepolia.org".to_owned()],
                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_ethereum.jpg".to_owned()],
                block_explorer_urls: vec!["https://sepolia.etherscan.io/".to_owned()],
                native_currency: NetworkCurrency {
                    name: "Sepolia ETH".to_owned(),
                    symbol: "ETH".to_owned(),
                    decimals: 18,
                },
            },
            Network::OpBNBTestnet => NetworkConfig {
                chain_id: 5611,
                chain_name: "opBNB Testnet".to_owned(),
                rpc_urls: vec!["https://opbnb-testnet-rpc.bnbchain.org".to_owned()],
                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_opbnb.jpg".to_owned()],
                block_explorer_urls: vec!["http://testnet.opbnbscan.com/".to_owned()],
                native_currency: NetworkCurrency {
                    name: "opBNB ETH".to_owned(),
                    symbol: "tBNB".to_owned(),
                    decimals: 18,
                },
            },
            Network::Other => panic!("No config for other network"),
        }
    }
}