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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::types::U256;
use serde::Deserialize;
use std::{
    convert::{TryFrom, TryInto},
    fmt,
    str::FromStr,
    time::Duration,
};
use strum::EnumVariantNames;
use thiserror::Error;

#[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")]
pub struct ParseChainError(String);

/// Enum for all known chains
///
/// When adding a new chain:
///   1. add new variant
///   2. update Display/FromStr impl
///   3. add etherscan_keys if supported
#[repr(u64)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize, EnumVariantNames)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "kebab-case")]
pub enum Chain {
    Mainnet = 1,
    Morden = 2,
    Ropsten = 3,
    Rinkeby = 4,
    Goerli = 5,
    Kovan = 42,
    #[strum(serialize = "xdai")]
    XDai = 100,
    Polygon = 137,
    Fantom = 250,
    Dev = 1337,
    AnvilHardhat = 31337,
    FantomTestnet = 4002,
    PolygonMumbai = 80001,
    Avalanche = 43114,
    AvalancheFuji = 43113,
    Sepolia = 11155111,
    Moonbeam = 1284,
    Moonbase = 1287,
    MoonbeamDev = 1281,
    Moonriver = 1285,
    Optimism = 10,
    OptimismGoerli = 420,
    OptimismKovan = 69,
    Arbitrum = 42161,
    ArbitrumTestnet = 421611,
    ArbitrumGoerli = 421613,
    Cronos = 25,
    CronosTestnet = 338,
    #[strum(serialize = "bsc")]
    BinanceSmartChain = 56,
    #[strum(serialize = "bsc-testnet")]
    BinanceSmartChainTestnet = 97,
    Poa = 99,
    Sokol = 77,
    Rsk = 30,
    Oasis = 26863,
    Emerald = 42262,
    EmeraldTestnet = 42261,
    Evmos = 9001,
    EvmosTestnet = 9000,
    Aurora = 1313161554,
    AuroraTestnet = 1313161555,
}

// === impl Chain ===

impl Chain {
    /// The blocktime varies from chain to chain
    ///
    /// It can be beneficial to know the average blocktime to adjust the polling of an Http provider
    /// for example.
    ///
    /// **Note:** this will not return the accurate average depending on the time but is rather a
    /// sensible default derived from blocktime charts like <https://etherscan.com/chart/blocktime>
    /// <https://polygonscan.com/chart/blocktime>
    pub fn average_blocktime_hint(&self) -> Option<Duration> {
        let ms = match self {
            Chain::Arbitrum | Chain::ArbitrumTestnet | Chain::ArbitrumGoerli => 1_300,
            Chain::Mainnet | Chain::Optimism => 13_000,
            Chain::Polygon | Chain::PolygonMumbai => 2_100,
            Chain::Moonbeam | Chain::Moonriver => 12_500,
            Chain::BinanceSmartChain | Chain::BinanceSmartChainTestnet => 3_000,
            Chain::Avalanche | Chain::AvalancheFuji => 2_000,
            Chain::Fantom | Chain::FantomTestnet => 1_200,
            Chain::Cronos | Chain::CronosTestnet => 5_700,
            Chain::Evmos | Chain::EvmosTestnet => 1_900,
            Chain::Aurora | Chain::AuroraTestnet => 1_100,
            Chain::Oasis => 5_500,
            Chain::Emerald => 6_000,
            Chain::Dev | Chain::AnvilHardhat => 200,
            // Explictly handle all network to make it easier not to forget this match when new
            // networks are added.
            Chain::Morden |
            Chain::Ropsten |
            Chain::Rinkeby |
            Chain::Goerli |
            Chain::Kovan |
            Chain::XDai |
            Chain::Sepolia |
            Chain::Moonbase |
            Chain::MoonbeamDev |
            Chain::OptimismGoerli |
            Chain::OptimismKovan |
            Chain::Poa |
            Chain::Sokol |
            Chain::Rsk |
            Chain::EmeraldTestnet => return None,
        };

        Some(Duration::from_millis(ms))
    }

    /// Returns the corresponding etherscan URLs
    ///
    /// Returns `(API URL, BASE_URL)`, like `("https://api(-chain).etherscan.io/api", "https://etherscan.io")`
    pub fn etherscan_urls(&self) -> Option<(&'static str, &'static str)> {
        let urls = match self {
            Chain::Mainnet => ("https://api.etherscan.io/api", "https://etherscan.io"),
            Chain::Ropsten => {
                ("https://api-ropsten.etherscan.io/api", "https://ropsten.etherscan.io")
            }
            Chain::Kovan => ("https://api-kovan.etherscan.io/api", "https://kovan.etherscan.io"),
            Chain::Rinkeby => {
                ("https://api-rinkeby.etherscan.io/api", "https://rinkeby.etherscan.io")
            }
            Chain::Goerli => ("https://api-goerli.etherscan.io/api", "https://goerli.etherscan.io"),
            Chain::Sepolia => {
                ("https://api-sepolia.etherscan.io/api", "https://sepolia.etherscan.io")
            }
            Chain::Polygon => ("https://api.polygonscan.com/api", "https://polygonscan.com"),
            Chain::PolygonMumbai => {
                ("https://api-testnet.polygonscan.com/api", "https://mumbai.polygonscan.com")
            }
            Chain::Avalanche => ("https://api.snowtrace.io/api", "https://snowtrace.io"),
            Chain::AvalancheFuji => {
                ("https://api-testnet.snowtrace.io/api", "https://testnet.snowtrace.io")
            }
            Chain::Optimism => {
                ("https://api-optimistic.etherscan.io/api", "https://optimistic.etherscan.io")
            }
            Chain::OptimismGoerli => (
                "https://api-goerli-optimistic.etherscan.io/api",
                "https://goerli-optimism.etherscan.io",
            ),
            Chain::OptimismKovan => (
                "https://api-kovan-optimistic.etherscan.io/api",
                "https://kovan-optimistic.etherscan.io",
            ),
            Chain::Fantom => ("https://api.ftmscan.com/api", "https://ftmscan.com"),
            Chain::FantomTestnet => {
                ("https://api-testnet.ftmscan.com/api", "https://testnet.ftmscan.com")
            }
            Chain::BinanceSmartChain => ("https://api.bscscan.com/api", "https://bscscan.com"),
            Chain::BinanceSmartChainTestnet => {
                ("https://api-testnet.bscscan.com/api", "https://testnet.bscscan.com")
            }
            Chain::Arbitrum => ("https://api.arbiscan.io/api", "https://arbiscan.io"),
            Chain::ArbitrumTestnet => {
                ("https://api-testnet.arbiscan.io/api", "https://testnet.arbiscan.io")
            }
            Chain::ArbitrumGoerli => (
                "https://goerli-rollup-explorer.arbitrum.io/api",
                "https://goerli-rollup-explorer.arbitrum.io",
            ),
            Chain::Cronos => ("https://api.cronoscan.com/api", "https://cronoscan.com"),
            Chain::CronosTestnet => {
                ("https://api-testnet.cronoscan.com/api", "https://testnet.cronoscan.com")
            }
            Chain::Moonbeam => {
                ("https://api-moonbeam.moonscan.io/api", "https://moonbeam.moonscan.io/")
            }
            Chain::Moonbase => {
                ("https://api-moonbase.moonscan.io/api", "https://moonbase.moonscan.io/")
            }
            Chain::Moonriver => {
                ("https://api-moonriver.moonscan.io/api", "https://moonriver.moonscan.io")
            }
            // blockscout API is etherscan compatible
            Chain::XDai => {
                ("https://blockscout.com/xdai/mainnet/api", "https://blockscout.com/xdai/mainnet")
            }
            Chain::Sokol => {
                ("https://blockscout.com/poa/sokol/api", "https://blockscout.com/poa/sokol")
            }
            Chain::Poa => {
                ("https://blockscout.com/poa/core/api", "https://blockscout.com/poa/core")
            }
            Chain::Rsk => {
                ("https://blockscout.com/rsk/mainnet/api", "https://blockscout.com/rsk/mainnet")
            }
            Chain::Oasis => ("https://scan.oasischain.io/api", "https://scan.oasischain.io/"),
            Chain::Emerald => {
                ("https://explorer.emerald.oasis.dev/api", "https://explorer.emerald.oasis.dev/")
            }
            Chain::EmeraldTestnet => (
                "https://testnet.explorer.emerald.oasis.dev/api",
                "https://testnet.explorer.emerald.oasis.dev/",
            ),
            Chain::Aurora => ("https://api.aurorascan.dev/api", "https://aurorascan.dev"),
            Chain::AuroraTestnet => {
                ("https://testnet.aurorascan.dev/api", "https://testnet.aurorascan.dev")
            }
            Chain::Evmos => ("https://evm.evmos.org/api", "https://evm.evmos.org/"),
            Chain::EvmosTestnet => ("https://evm.evmos.dev/api", "https://evm.evmos.dev/"),
            Chain::AnvilHardhat | Chain::Dev | Chain::Morden | Chain::MoonbeamDev => {
                // this is explicitly exhaustive so we don't forget to add new urls when adding a
                // new chain
                return None
            }
        };

        Some(urls)
    }
}

impl fmt::Display for Chain {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let chain = match self {
            Chain::Mainnet => "mainnet",
            Chain::Morden => "morden",
            Chain::Ropsten => "ropsten",
            Chain::Rinkeby => "rinkeby",
            Chain::Goerli => "goerli",
            Chain::Kovan => "kovan",
            Chain::XDai => "xdai",
            Chain::Polygon => "polygon",
            Chain::PolygonMumbai => "mumbai",
            Chain::Avalanche => "avalanche",
            Chain::AvalancheFuji => "fuji",
            Chain::Sepolia => "sepolia",
            Chain::Moonbeam => "moonbeam",
            Chain::Moonbase => "moonbase",
            Chain::MoonbeamDev => "moonbeam-dev",
            Chain::Moonriver => "moonriver",
            Chain::Optimism => "optimism",
            Chain::OptimismGoerli => "optimism-goerli",
            Chain::OptimismKovan => "optimism-kovan",
            Chain::Fantom => "fantom",
            Chain::Dev => "dev",
            Chain::FantomTestnet => "fantom-testnet",
            Chain::BinanceSmartChain => "bsc",
            Chain::BinanceSmartChainTestnet => "bsc-testnet",
            Chain::Arbitrum => "arbitrum",
            Chain::ArbitrumTestnet => "arbitrum-testnet",
            Chain::ArbitrumGoerli => "arbitrum-goerli",
            Chain::Cronos => "cronos",
            Chain::CronosTestnet => "cronos-testnet",
            Chain::Poa => "poa",
            Chain::Sokol => "sokol",
            Chain::Rsk => "rsk",
            Chain::Oasis => "oasis",
            Chain::Emerald => "emerald",
            Chain::EmeraldTestnet => "emerald-testnet",
            Chain::AnvilHardhat => "anvil-hardhat",
            Chain::Evmos => "evmos",
            Chain::EvmosTestnet => "evmos-testnet",
            Chain::Aurora => "aurora",
            Chain::AuroraTestnet => "aurora-testnet",
        };

        write!(formatter, "{}", chain)
    }
}

impl From<Chain> for u32 {
    fn from(chain: Chain) -> Self {
        chain as u32
    }
}

impl From<Chain> for U256 {
    fn from(chain: Chain) -> Self {
        u32::from(chain).into()
    }
}

impl From<Chain> for u64 {
    fn from(chain: Chain) -> Self {
        u32::from(chain).into()
    }
}

impl TryFrom<u64> for Chain {
    type Error = ParseChainError;

    fn try_from(chain: u64) -> Result<Chain, Self::Error> {
        Ok(match chain {
            1 => Chain::Mainnet,
            2 => Chain::Morden,
            3 => Chain::Ropsten,
            4 => Chain::Rinkeby,
            5 => Chain::Goerli,
            42 => Chain::Kovan,
            100 => Chain::XDai,
            137 => Chain::Polygon,
            1337 => Chain::Dev,
            31337 => Chain::AnvilHardhat,
            250 => Chain::Fantom,
            4002 => Chain::FantomTestnet,
            80001 => Chain::PolygonMumbai,
            43114 => Chain::Avalanche,
            43113 => Chain::AvalancheFuji,
            11155111 => Chain::Sepolia,
            1284 => Chain::Moonbeam,
            1287 => Chain::Moonbase,
            1281 => Chain::MoonbeamDev,
            1285 => Chain::Moonriver,
            10 => Chain::Optimism,
            420 => Chain::OptimismGoerli,
            69 => Chain::OptimismKovan,
            56 => Chain::BinanceSmartChain,
            97 => Chain::BinanceSmartChainTestnet,
            42161 => Chain::Arbitrum,
            421611 => Chain::ArbitrumTestnet,
            421613 => Chain::ArbitrumGoerli,
            25 => Chain::Cronos,
            338 => Chain::CronosTestnet,
            99 => Chain::Poa,
            77 => Chain::Sokol,
            30 => Chain::Rsk,
            26863 => Chain::Oasis,
            42262 => Chain::Emerald,
            42261 => Chain::EmeraldTestnet,
            9001 => Chain::Evmos,
            9000 => Chain::EvmosTestnet,
            1313161554 => Chain::Aurora,
            1313161555 => Chain::AuroraTestnet,
            _ => return Err(ParseChainError(chain.to_string())),
        })
    }
}

impl TryFrom<U256> for Chain {
    type Error = ParseChainError;

    fn try_from(chain: U256) -> Result<Chain, Self::Error> {
        if chain.bits() > 64 {
            return Err(ParseChainError(chain.to_string()))
        }
        chain.as_u64().try_into()
    }
}

impl FromStr for Chain {
    type Err = ParseChainError;
    fn from_str(chain: &str) -> Result<Self, Self::Err> {
        Ok(match chain {
            "mainnet" => Chain::Mainnet,
            "morden" => Chain::Morden,
            "ropsten" => Chain::Ropsten,
            "rinkeby" => Chain::Rinkeby,
            "goerli" => Chain::Goerli,
            "kovan" => Chain::Kovan,
            "xdai" => Chain::XDai,
            "polygon" => Chain::Polygon,
            "mumbai" | "polygon-mumbai" => Chain::PolygonMumbai,
            "avalanche" => Chain::Avalanche,
            "fuji" | "avalanche-fuji" => Chain::AvalancheFuji,
            "sepolia" => Chain::Sepolia,
            "moonbeam" => Chain::Moonbeam,
            "moonbase" => Chain::Moonbase,
            "moonbeam-dev" => Chain::MoonbeamDev,
            "moonriver" => Chain::Moonriver,
            "optimism" => Chain::Optimism,
            "optimism-goerli" => Chain::OptimismGoerli,
            "optimism-kovan" => Chain::OptimismKovan,
            "fantom" => Chain::Fantom,
            "fantom-testnet" => Chain::FantomTestnet,
            "dev" => Chain::Dev,
            "anvil" | "hardhat" | "anvil-hardhat" => Chain::AnvilHardhat,
            "bsc" => Chain::BinanceSmartChain,
            "bsc-testnet" => Chain::BinanceSmartChainTestnet,
            "arbitrum" => Chain::Arbitrum,
            "arbitrum-testnet" => Chain::ArbitrumTestnet,
            "arbitrum-goerli" => Chain::ArbitrumGoerli,
            "cronos" => Chain::Cronos,
            "cronos-testnet" => Chain::CronosTestnet,
            "poa" => Chain::Poa,
            "sokol" => Chain::Sokol,
            "rsk" => Chain::Rsk,
            "oasis" => Chain::Oasis,
            "emerald" => Chain::Emerald,
            "emerald-testnet" => Chain::EmeraldTestnet,
            "aurora" => Chain::Aurora,
            "aurora-testnet" => Chain::AuroraTestnet,
            _ => return Err(ParseChainError(chain.to_owned())),
        })
    }
}

impl Chain {
    /// Helper function for checking if a chainid corresponds to a legacy chainid
    /// without eip1559
    pub fn is_legacy(&self) -> bool {
        // TODO: Add other chains which do not support EIP1559.
        matches!(
            self,
            Chain::Optimism |
                Chain::OptimismGoerli |
                Chain::OptimismKovan |
                Chain::Fantom |
                Chain::FantomTestnet |
                Chain::BinanceSmartChain |
                Chain::BinanceSmartChainTestnet |
                Chain::Arbitrum |
                Chain::ArbitrumTestnet |
                Chain::ArbitrumGoerli |
                Chain::Rsk |
                Chain::Oasis |
                Chain::Emerald |
                Chain::EmeraldTestnet,
        )
    }
}

impl Default for Chain {
    fn default() -> Self {
        Chain::Mainnet
    }
}

#[test]
fn test_default_chain() {
    assert_eq!(Chain::default(), Chain::Mainnet);
}