z4_types/
network.rs

1pub const ROOM_MARKET_ABI: &str = include_str!("../public/ABI/RoomMarket.json");
2pub const TOKEN_ABI: &str = include_str!("../public/ABI/Token.json");
3pub const SIMPLE_GAME_ABI: &str = include_str!("../public/ABI/SimpleGame.json");
4
5/// Default network
6pub const DEFAULT_NETWORK: Network = Network::Localhost;
7
8/// Network types
9#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
10pub enum Network {
11    Localhost,
12    Holesky,
13    Sepolia,
14    OpBNBTestnet,
15    Other,
16}
17
18impl Network {
19    /// Get the network from lower-case string
20    pub fn from_str(s: &str) -> Self {
21        match s {
22            "localhost" => Network::Localhost,
23            "holesky" => Network::Holesky,
24            "sepolia" => Network::Sepolia,
25            "opbnbtestnet" => Network::OpBNBTestnet,
26            _ => Network::Other,
27        }
28    }
29
30    pub fn to_str<'a>(&self) -> &'a str {
31        match self {
32            Network::Localhost => "localhost",
33            Network::Holesky => "holesky",
34            Network::Sepolia => "sepolia",
35            Network::OpBNBTestnet => "opbnbtestnet",
36            Network::Other => "other",
37        }
38    }
39
40    pub fn from_chain_id(chain_id: u64) -> Self {
41        match chain_id {
42            17000 => Network::Holesky,
43            11155111 => Network::Sepolia,
44            5611 => Network::OpBNBTestnet,
45            _ => DEFAULT_NETWORK,
46        }
47    }
48}
49
50/// Network native currency config
51#[derive(Default)]
52pub struct NetworkCurrency {
53    /// Currency name
54    pub name: String,
55    /// Currency symbol
56    pub symbol: String,
57    /// Currency decimals
58    pub decimals: i32,
59}
60
61/// Network config informato, use EIP-3085
62#[derive(Default)]
63pub struct NetworkConfig {
64    /// Chain id
65    pub chain_id: i32,
66    /// Chain name
67    pub chain_name: String,
68    /// List of endpoints
69    pub rpc_urls: Vec<String>,
70    /// list of block explorer urls
71    pub block_explorer_urls: Vec<String>,
72    /// List of chain icon urls
73    pub icon_urls: Vec<String>,
74    /// Native currency config
75    pub native_currency: NetworkCurrency,
76}
77
78impl NetworkConfig {
79    /// Get the network config
80    pub fn from(network: Network) -> NetworkConfig {
81        match network {
82            Network::Localhost => {
83                let mut nc = NetworkConfig::default();
84                nc.rpc_urls = vec!["http://127.0.0.1:8545".to_owned()];
85                nc
86            }
87            Network::Holesky => NetworkConfig {
88                chain_id: 17000,
89                chain_name: "Holesky".to_owned(),
90                rpc_urls: vec!["https://1rpc.io/holesky".to_owned()],
91                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_ethereum.jpg".to_owned()],
92                block_explorer_urls: vec!["https://holesky.beaconcha.in/".to_owned()],
93                native_currency: NetworkCurrency {
94                    name: "Holesky ETH".to_owned(),
95                    symbol: "ETH".to_owned(),
96                    decimals: 18,
97                },
98            },
99            Network::Sepolia => NetworkConfig {
100                chain_id: 11155111,
101                chain_name: "Sepolia".to_owned(),
102                rpc_urls: vec!["https://rpc.sepolia.org".to_owned()],
103                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_ethereum.jpg".to_owned()],
104                block_explorer_urls: vec!["https://sepolia.etherscan.io/".to_owned()],
105                native_currency: NetworkCurrency {
106                    name: "Sepolia ETH".to_owned(),
107                    symbol: "ETH".to_owned(),
108                    decimals: 18,
109                },
110            },
111            Network::OpBNBTestnet => NetworkConfig {
112                chain_id: 5611,
113                chain_name: "opBNB Testnet".to_owned(),
114                rpc_urls: vec!["https://opbnb-testnet-rpc.bnbchain.org".to_owned()],
115                icon_urls: vec!["https://icons.llamao.fi/icons/chains/rsz_opbnb.jpg".to_owned()],
116                block_explorer_urls: vec!["http://testnet.opbnbscan.com/".to_owned()],
117                native_currency: NetworkCurrency {
118                    name: "opBNB ETH".to_owned(),
119                    symbol: "tBNB".to_owned(),
120                    decimals: 18,
121                },
122            },
123            Network::Other => panic!("No config for other network"),
124        }
125    }
126}