alloy_chains/
named.rs

1use alloy_primitives::{Address, address};
2use core::{cmp::Ordering, fmt, time::Duration};
3use num_enum::TryFromPrimitiveError;
4
5#[allow(unused_imports)]
6use alloc::string::String;
7
8// When adding a new chain:
9//   1. add new variant to the NamedChain enum;
10//   2. add extra information in the last `impl` block (explorer URLs, block time) when applicable;
11//   3. (optional) add aliases:
12//     - Strum (in kebab-case): `#[strum(to_string = "<main>", serialize = "<aliasX>", ...)]`
13//      `to_string = "<main>"` must be present and will be used in `Display`, `Serialize`
14//      and `FromStr`, while `serialize = "<aliasX>"` will be appended to `FromStr`.
15//      More info: <https://docs.rs/strum/latest/strum/additional_attributes/index.html#attributes-on-variants>
16//     - Serde (in snake_case): `#[cfg_attr(feature = "serde", serde(alias = "<aliasX>", ...))]`
17//      Aliases are appended to the `Deserialize` implementation.
18//      More info: <https://serde.rs/variant-attrs.html>
19//     - Add a test at the bottom of the file
20//   4. run `cargo test --all-features` to update the JSON bindings and schema.
21//   5. run `cargo +nightly fmt --all` to properly format the code.
22
23// We don't derive Serialize because it is manually implemented using AsRef<str> and it would break
24// a lot of things since Serialize is `kebab-case` vs Deserialize `snake_case`. This means that the
25// NamedChain type is not "round-trippable", because the Serialize and Deserialize implementations
26// do not use the same case style.
27
28/// An Ethereum EIP-155 chain.
29#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
30#[derive(strum::IntoStaticStr)] // Into<&'static str>, AsRef<str>, fmt::Display and serde::Serialize
31#[derive(strum::VariantNames)] // NamedChain::VARIANTS
32#[derive(strum::VariantArray)] // NamedChain::VARIANTS
33#[derive(strum::EnumString)] // FromStr, TryFrom<&str>
34#[derive(strum::EnumIter)] // NamedChain::iter
35#[derive(strum::EnumCount)] // NamedChain::COUNT
36#[derive(num_enum::TryFromPrimitive)] // TryFrom<u64>
37#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
38#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
39#[strum(serialize_all = "kebab-case")]
40#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
41#[repr(u64)]
42#[allow(missing_docs)]
43#[non_exhaustive]
44pub enum NamedChain {
45    #[strum(to_string = "mainnet", serialize = "ethlive")]
46    #[cfg_attr(feature = "serde", serde(alias = "ethlive"))]
47    Mainnet = 1,
48    Morden = 2,
49    Ropsten = 3,
50    Rinkeby = 4,
51    Goerli = 5,
52    Kovan = 42,
53    Holesky = 17000,
54    Hoodi = 560048,
55    Sepolia = 11155111,
56
57    #[cfg_attr(feature = "serde", serde(alias = "odyssey"))]
58    Odyssey = 911867,
59
60    Optimism = 10,
61    #[cfg_attr(feature = "serde", serde(alias = "optimism-kovan"))]
62    OptimismKovan = 69,
63    #[cfg_attr(feature = "serde", serde(alias = "optimism-goerli"))]
64    OptimismGoerli = 420,
65    #[cfg_attr(feature = "serde", serde(alias = "optimism-sepolia"))]
66    OptimismSepolia = 11155420,
67
68    #[strum(to_string = "bob")]
69    #[cfg_attr(feature = "serde", serde(alias = "bob"))]
70    Bob = 60808,
71    #[strum(to_string = "bob-sepolia")]
72    #[cfg_attr(feature = "serde", serde(alias = "bob-sepolia"))]
73    BobSepolia = 808813,
74
75    #[cfg_attr(feature = "serde", serde(alias = "arbitrum_one", alias = "arbitrum-one"))]
76    Arbitrum = 42161,
77    ArbitrumTestnet = 421611,
78    #[cfg_attr(feature = "serde", serde(alias = "arbitrum-goerli"))]
79    ArbitrumGoerli = 421613,
80    #[cfg_attr(feature = "serde", serde(alias = "arbitrum-sepolia"))]
81    ArbitrumSepolia = 421614,
82    #[cfg_attr(feature = "serde", serde(alias = "arbitrum-nova"))]
83    ArbitrumNova = 42170,
84
85    Cronos = 25,
86    CronosTestnet = 338,
87
88    Rsk = 30,
89    RskTestnet = 31,
90
91    #[strum(to_string = "telos")]
92    #[cfg_attr(feature = "serde", serde(alias = "telos", alias = "telos_evm"))]
93    TelosEvm = 40,
94    #[strum(to_string = "telos-testnet")]
95    #[cfg_attr(
96        feature = "serde",
97        serde(alias = "telos_testnet", alias = "telos-evm-testnet", alias = "telos_evm_testnet")
98    )]
99    TelosEvmTestnet = 41,
100
101    #[strum(to_string = "crab")]
102    #[cfg_attr(feature = "serde", serde(alias = "crab"))]
103    Crab = 44,
104    #[strum(to_string = "darwinia")]
105    #[cfg_attr(feature = "serde", serde(alias = "darwinia"))]
106    Darwinia = 46,
107    #[strum(to_string = "koi")]
108    #[cfg_attr(feature = "serde", serde(alias = "koi"))]
109    Koi = 701,
110
111    /// Note the correct name for BSC should be `BNB Smart Chain` due to the rebranding: <https://www.bnbchain.org/en/blog/bsc-is-now-bnb-chain-the-infrastructure-for-the-metafi-universe>
112    /// We keep `Binance Smart Chain` for backward compatibility, and the enum could be renamed in
113    /// the future release.
114    #[strum(to_string = "bsc", serialize = "binance-smart-chain", serialize = "bnb-smart-chain")]
115    #[cfg_attr(
116        feature = "serde",
117        serde(alias = "bsc", alias = "bnb-smart-chain", alias = "binance-smart-chain")
118    )]
119    BinanceSmartChain = 56,
120    #[strum(
121        to_string = "bsc-testnet",
122        serialize = "binance-smart-chain-testnet",
123        serialize = "bnb-smart-chain-testnet"
124    )]
125    #[cfg_attr(
126        feature = "serde",
127        serde(
128            alias = "bsc_testnet",
129            alias = "bsc-testnet",
130            alias = "bnb-smart-chain-testnet",
131            alias = "binance-smart-chain-testnet"
132        )
133    )]
134    BinanceSmartChainTestnet = 97,
135
136    Poa = 99,
137    Sokol = 77,
138
139    Scroll = 534352,
140    #[cfg_attr(
141        feature = "serde",
142        serde(alias = "scroll_sepolia_testnet", alias = "scroll-sepolia")
143    )]
144    ScrollSepolia = 534351,
145
146    Metis = 1088,
147
148    #[cfg_attr(feature = "serde", serde(alias = "conflux-espace-testnet"))]
149    CfxTestnet = 71,
150    #[cfg_attr(feature = "serde", serde(alias = "conflux-espace"))]
151    Cfx = 1030,
152
153    #[strum(to_string = "xdai", serialize = "gnosis", serialize = "gnosis-chain")]
154    #[cfg_attr(feature = "serde", serde(alias = "xdai", alias = "gnosis", alias = "gnosis-chain"))]
155    Gnosis = 100,
156
157    Polygon = 137,
158    #[strum(to_string = "mumbai", serialize = "polygon-mumbai")]
159    #[cfg_attr(feature = "serde", serde(alias = "mumbai", alias = "polygon-mumbai"))]
160    PolygonMumbai = 80001,
161    #[strum(to_string = "amoy", serialize = "polygon-amoy")]
162    #[cfg_attr(feature = "serde", serde(alias = "amoy", alias = "polygon-amoy"))]
163    PolygonAmoy = 80002,
164    #[strum(serialize = "polygon-zkevm", serialize = "zkevm")]
165    #[cfg_attr(
166        feature = "serde",
167        serde(alias = "zkevm", alias = "polygon_zkevm", alias = "polygon-zkevm")
168    )]
169    PolygonZkEvm = 1101,
170    #[strum(serialize = "polygon-zkevm-testnet", serialize = "zkevm-testnet")]
171    #[cfg_attr(
172        feature = "serde",
173        serde(
174            alias = "zkevm-testnet",
175            alias = "polygon_zkevm_testnet",
176            alias = "polygon-zkevm-testnet"
177        )
178    )]
179    PolygonZkEvmTestnet = 1442,
180
181    Fantom = 250,
182    FantomTestnet = 4002,
183
184    Moonbeam = 1284,
185    MoonbeamDev = 1281,
186
187    Moonriver = 1285,
188
189    Moonbase = 1287,
190
191    Dev = 1337,
192    #[strum(to_string = "anvil-hardhat", serialize = "anvil", serialize = "hardhat")]
193    #[cfg_attr(
194        feature = "serde",
195        serde(alias = "anvil", alias = "hardhat", alias = "anvil-hardhat")
196    )]
197    AnvilHardhat = 31337,
198
199    #[strum(to_string = "gravity-alpha-mainnet")]
200    #[cfg_attr(feature = "serde", serde(alias = "gravity-alpha-mainnet"))]
201    GravityAlphaMainnet = 1625,
202    #[strum(to_string = "gravity-alpha-testnet-sepolia")]
203    #[cfg_attr(feature = "serde", serde(alias = "gravity-alpha-testnet-sepolia"))]
204    GravityAlphaTestnetSepolia = 13505,
205
206    Evmos = 9001,
207    EvmosTestnet = 9000,
208
209    Chiado = 10200,
210
211    Oasis = 26863,
212
213    Emerald = 42262,
214    EmeraldTestnet = 42261,
215
216    FilecoinMainnet = 314,
217    FilecoinCalibrationTestnet = 314159,
218
219    Avalanche = 43114,
220    #[strum(to_string = "fuji", serialize = "avalanche-fuji")]
221    #[cfg_attr(feature = "serde", serde(alias = "fuji"))]
222    AvalancheFuji = 43113,
223
224    Celo = 42220,
225    CeloAlfajores = 44787,
226    CeloBaklava = 62320,
227
228    Aurora = 1313161554,
229    AuroraTestnet = 1313161555,
230
231    Canto = 7700,
232    CantoTestnet = 740,
233
234    Boba = 288,
235
236    Base = 8453,
237    #[cfg_attr(feature = "serde", serde(alias = "base-goerli"))]
238    BaseGoerli = 84531,
239    #[cfg_attr(feature = "serde", serde(alias = "base-sepolia"))]
240    BaseSepolia = 84532,
241    #[cfg_attr(feature = "serde", serde(alias = "syndr"))]
242    Syndr = 404,
243    #[cfg_attr(feature = "serde", serde(alias = "syndr-sepolia"))]
244    SyndrSepolia = 444444,
245
246    Shimmer = 148,
247
248    Ink = 57073,
249    #[cfg_attr(feature = "serde", serde(alias = "ink_sepolia_testnet", alias = "ink-sepolia"))]
250    InkSepolia = 763373,
251
252    #[strum(to_string = "fraxtal")]
253    #[cfg_attr(feature = "serde", serde(alias = "fraxtal"))]
254    Fraxtal = 252,
255    #[strum(to_string = "fraxtal-testnet")]
256    #[cfg_attr(feature = "serde", serde(alias = "fraxtal-testnet"))]
257    FraxtalTestnet = 2522,
258
259    Blast = 81457,
260    #[cfg_attr(feature = "serde", serde(alias = "blast-sepolia"))]
261    BlastSepolia = 168587773,
262
263    Linea = 59144,
264    #[cfg_attr(feature = "serde", serde(alias = "linea-goerli"))]
265    LineaGoerli = 59140,
266    #[cfg_attr(feature = "serde", serde(alias = "linea-sepolia"))]
267    LineaSepolia = 59141,
268
269    #[strum(to_string = "zksync")]
270    #[cfg_attr(feature = "serde", serde(alias = "zksync"))]
271    ZkSync = 324,
272    #[strum(to_string = "zksync-testnet")]
273    #[cfg_attr(feature = "serde", serde(alias = "zksync_testnet", alias = "zksync-testnet"))]
274    ZkSyncTestnet = 300,
275
276    #[strum(to_string = "mantle")]
277    #[cfg_attr(feature = "serde", serde(alias = "mantle"))]
278    Mantle = 5000,
279    #[strum(to_string = "mantle-testnet")]
280    #[cfg_attr(feature = "serde", serde(alias = "mantle-testnet"))]
281    MantleTestnet = 5001,
282    #[strum(to_string = "mantle-sepolia")]
283    #[cfg_attr(feature = "serde", serde(alias = "mantle-sepolia"))]
284    MantleSepolia = 5003,
285
286    #[strum(to_string = "xai")]
287    #[cfg_attr(feature = "serde", serde(alias = "xai"))]
288    Xai = 660279,
289    #[strum(to_string = "xai-sepolia")]
290    #[cfg_attr(feature = "serde", serde(alias = "xai-sepolia"))]
291    XaiSepolia = 37714555429,
292
293    #[strum(to_string = "happychain-testnet")]
294    #[cfg_attr(feature = "serde", serde(alias = "happychain-testnet"))]
295    HappychainTestnet = 216,
296
297    Viction = 88,
298
299    Zora = 7777777,
300    #[cfg_attr(feature = "serde", serde(alias = "zora-sepolia"))]
301    ZoraSepolia = 999999999,
302
303    Pgn = 424,
304    #[cfg_attr(feature = "serde", serde(alias = "pgn-sepolia"))]
305    PgnSepolia = 58008,
306
307    Mode = 34443,
308    #[cfg_attr(feature = "serde", serde(alias = "mode-sepolia"))]
309    ModeSepolia = 919,
310
311    Elastos = 20,
312
313    #[cfg_attr(
314        feature = "serde",
315        serde(alias = "kakarot-sepolia", alias = "kakarot-starknet-sepolia")
316    )]
317    KakarotSepolia = 920637907288165,
318
319    #[cfg_attr(feature = "serde", serde(alias = "etherlink"))]
320    Etherlink = 42793,
321
322    #[cfg_attr(feature = "serde", serde(alias = "etherlink-testnet"))]
323    EtherlinkTestnet = 128123,
324
325    Degen = 666666666,
326
327    #[strum(to_string = "opbnb-mainnet")]
328    #[cfg_attr(
329        feature = "serde",
330        serde(rename = "opbnb_mainnet", alias = "opbnb-mainnet", alias = "op-bnb-mainnet")
331    )]
332    OpBNBMainnet = 204,
333    #[strum(to_string = "opbnb-testnet")]
334    #[cfg_attr(
335        feature = "serde",
336        serde(rename = "opbnb_testnet", alias = "opbnb-testnet", alias = "op-bnb-testnet")
337    )]
338    OpBNBTestnet = 5611,
339
340    Ronin = 2020,
341
342    #[cfg_attr(feature = "serde", serde(alias = "ronin-testnet"))]
343    RoninTestnet = 2021,
344
345    Taiko = 167000,
346    #[cfg_attr(feature = "serde", serde(alias = "taiko-hekla"))]
347    TaikoHekla = 167009,
348
349    #[strum(to_string = "autonomys-nova-testnet")]
350    #[cfg_attr(
351        feature = "serde",
352        serde(rename = "autonomys_nova_testnet", alias = "autonomys-nova-testnet")
353    )]
354    AutonomysNovaTestnet = 490000,
355
356    Flare = 14,
357    #[cfg_attr(feature = "serde", serde(alias = "flare-coston2"))]
358    FlareCoston2 = 114,
359
360    #[strum(to_string = "acala")]
361    #[cfg_attr(feature = "serde", serde(alias = "acala"))]
362    Acala = 787,
363    #[strum(to_string = "acala-mandala-testnet")]
364    #[cfg_attr(feature = "serde", serde(alias = "acala-mandala-testnet"))]
365    AcalaMandalaTestnet = 595,
366    #[strum(to_string = "acala-testnet")]
367    #[cfg_attr(feature = "serde", serde(alias = "acala-testnet"))]
368    AcalaTestnet = 597,
369
370    #[strum(to_string = "karura")]
371    #[cfg_attr(feature = "serde", serde(alias = "karura"))]
372    Karura = 686,
373    #[strum(to_string = "karura-testnet")]
374    #[cfg_attr(feature = "serde", serde(alias = "karura-testnet"))]
375    KaruraTestnet = 596,
376    #[strum(to_string = "pulsechain")]
377    #[cfg_attr(feature = "serde", serde(alias = "pulsechain"))]
378    Pulsechain = 369,
379    #[strum(to_string = "pulsechain-testnet")]
380    #[cfg_attr(feature = "serde", serde(alias = "pulsechain-testnet"))]
381    PulsechainTestnet = 943,
382
383    #[strum(to_string = "immutable")]
384    #[cfg_attr(feature = "serde", serde(alias = "immutable"))]
385    Immutable = 13371,
386    #[strum(to_string = "immutable-testnet")]
387    #[cfg_attr(feature = "serde", serde(alias = "immutable-testnet"))]
388    ImmutableTestnet = 13473,
389
390    #[strum(to_string = "soneium")]
391    #[cfg_attr(feature = "serde", serde(alias = "soneium"))]
392    Soneium = 1868,
393
394    #[strum(to_string = "soneium-minato-testnet")]
395    #[cfg_attr(feature = "serde", serde(alias = "soneium-minato-testnet"))]
396    SoneiumMinatoTestnet = 1946,
397
398    #[cfg_attr(feature = "serde", serde(alias = "worldchain"))]
399    World = 480,
400    #[strum(to_string = "world-sepolia")]
401    #[cfg_attr(feature = "serde", serde(alias = "worldchain-sepolia", alias = "world-sepolia"))]
402    WorldSepolia = 4801,
403    Iotex = 4689,
404    Core = 1116,
405    Merlin = 4200,
406    Bitlayer = 200901,
407    Vana = 1480,
408    Zeta = 7000,
409    Kaia = 8217,
410    Story = 1514,
411    Sei = 1329,
412
413    Unichain = 130,
414    #[strum(to_string = "unichain-sepolia")]
415    #[cfg_attr(feature = "serde", serde(alias = "unichain-sepolia"))]
416    UnichainSepolia = 1301,
417
418    #[strum(to_string = "apechain")]
419    #[cfg_attr(feature = "serde", serde(alias = "apechain"))]
420    ApeChain = 33139,
421    #[strum(to_string = "curtis", serialize = "apechain-testnet")]
422    #[cfg_attr(feature = "serde", serde(alias = "apechain-testnet", alias = "curtis"))]
423    Curtis = 33111,
424
425    #[strum(to_string = "sonic-blaze")]
426    #[cfg_attr(feature = "serde", serde(alias = "sonic-blaze"))]
427    SonicBlaze = 57054,
428    #[strum(to_string = "sonic-testnet")]
429    #[cfg_attr(feature = "serde", serde(alias = "sonic-testnet"))]
430    SonicTestnet = 64165,
431    #[strum(to_string = "sonic")]
432    #[cfg_attr(feature = "serde", serde(alias = "sonic"))]
433    Sonic = 146,
434
435    #[strum(to_string = "treasure")]
436    #[cfg_attr(feature = "serde", serde(alias = "treasure"))]
437    Treasure = 61166,
438
439    #[strum(to_string = "treasure-topaz", serialize = "treasure-topaz-testnet")]
440    #[cfg_attr(
441        feature = "serde",
442        serde(alias = "treasure-topaz-testnet", alias = "treasure-topaz")
443    )]
444    TreasureTopaz = 978658,
445
446    #[strum(to_string = "berachain-bepolia", serialize = "berachain-bepolia-testnet")]
447    #[cfg_attr(
448        feature = "serde",
449        serde(alias = "berachain-bepolia-testnet", alias = "berachain-bepolia")
450    )]
451    BerachainBepolia = 80069,
452
453    Berachain = 80094,
454
455    #[strum(to_string = "superposition-testnet")]
456    #[cfg_attr(feature = "serde", serde(alias = "superposition-testnet"))]
457    SuperpositionTestnet = 98985,
458
459    #[strum(to_string = "superposition")]
460    #[cfg_attr(feature = "serde", serde(alias = "superposition"))]
461    Superposition = 55244,
462
463    #[strum(serialize = "monad-testnet")]
464    #[cfg_attr(feature = "serde", serde(alias = "monad-testnet"))]
465    MonadTestnet = 10143,
466
467    #[strum(to_string = "hyperliquid")]
468    #[cfg_attr(feature = "serde", serde(alias = "hyperliquid"))]
469    Hyperliquid = 999,
470
471    #[strum(to_string = "abstract")]
472    #[cfg_attr(feature = "serde", serde(alias = "abstract"))]
473    Abstract = 2741,
474
475    #[strum(to_string = "abstract-testnet")]
476    #[cfg_attr(feature = "serde", serde(alias = "abstract-testnet"))]
477    AbstractTestnet = 11124,
478
479    #[strum(to_string = "corn")]
480    #[cfg_attr(feature = "serde", serde(alias = "corn"))]
481    Corn = 21000000,
482
483    #[strum(to_string = "corn-testnet")]
484    #[cfg_attr(feature = "serde", serde(alias = "corn-testnet"))]
485    CornTestnet = 21000001,
486
487    #[strum(to_string = "sophon")]
488    #[cfg_attr(feature = "serde", serde(alias = "sophon"))]
489    Sophon = 50104,
490
491    #[strum(to_string = "sophon-testnet")]
492    #[cfg_attr(feature = "serde", serde(alias = "sophon-testnet"))]
493    SophonTestnet = 531050104,
494
495    #[strum(to_string = "lens")]
496    #[cfg_attr(feature = "serde", serde(alias = "lens"))]
497    Lens = 232,
498
499    #[strum(to_string = "lens-testnet")]
500    #[cfg_attr(feature = "serde", serde(alias = "lens-testnet"))]
501    LensTestnet = 37111,
502
503    #[strum(to_string = "injective-testnet")]
504    #[cfg_attr(feature = "serde", serde(alias = "injective-testnet"))]
505    InjectiveTestnet = 1439,
506}
507
508// This must be implemented manually so we avoid a conflict with `TryFromPrimitive` where it treats
509// the `#[default]` attribute as its own `#[num_enum(default)]`
510impl Default for NamedChain {
511    #[inline]
512    fn default() -> Self {
513        Self::Mainnet
514    }
515}
516
517macro_rules! impl_into_numeric {
518    ($($t:ty)+) => {$(
519        impl From<NamedChain> for $t {
520            #[inline]
521            fn from(chain: NamedChain) -> Self {
522                chain as $t
523            }
524        }
525    )+};
526}
527
528impl_into_numeric!(u64 i64 u128 i128);
529#[cfg(target_pointer_width = "64")]
530impl_into_numeric!(usize isize);
531
532macro_rules! impl_try_from_numeric {
533    ($($native:ty)+) => {
534        $(
535            impl TryFrom<$native> for NamedChain {
536                type Error = TryFromPrimitiveError<NamedChain>;
537
538                #[inline]
539                fn try_from(value: $native) -> Result<Self, Self::Error> {
540                    (value as u64).try_into()
541                }
542            }
543        )+
544    };
545}
546
547impl_try_from_numeric!(u8 i8 u16 i16 u32 i32 usize isize);
548
549impl fmt::Display for NamedChain {
550    #[inline]
551    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
552        self.as_str().fmt(f)
553    }
554}
555
556impl AsRef<str> for NamedChain {
557    #[inline]
558    fn as_ref(&self) -> &str {
559        self.as_str()
560    }
561}
562
563impl PartialEq<u64> for NamedChain {
564    #[inline]
565    fn eq(&self, other: &u64) -> bool {
566        (*self as u64) == *other
567    }
568}
569
570impl PartialOrd<u64> for NamedChain {
571    #[inline]
572    fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
573        (*self as u64).partial_cmp(other)
574    }
575}
576
577#[cfg(feature = "serde")]
578impl serde::Serialize for NamedChain {
579    #[inline]
580    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
581        s.serialize_str(self.as_ref())
582    }
583}
584
585#[cfg(feature = "rlp")]
586impl alloy_rlp::Encodable for NamedChain {
587    #[inline]
588    fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
589        (*self as u64).encode(out)
590    }
591
592    #[inline]
593    fn length(&self) -> usize {
594        (*self as u64).length()
595    }
596}
597
598#[cfg(feature = "rlp")]
599impl alloy_rlp::Decodable for NamedChain {
600    #[inline]
601    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
602        let n = u64::decode(buf)?;
603        Self::try_from(n).map_err(|_| alloy_rlp::Error::Overflow)
604    }
605}
606
607#[cfg(feature = "arbitrary")]
608impl<'a> arbitrary::Arbitrary<'a> for NamedChain {
609    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
610        use strum::{EnumCount, VariantArray};
611        let idx = u.choose_index(NamedChain::COUNT)?;
612        Ok(NamedChain::VARIANTS[idx])
613    }
614}
615
616// NB: all utility functions *should* be explicitly exhaustive (not use `_` matcher) so we don't
617//     forget to update them when adding a new `NamedChain` variant.
618#[allow(clippy::match_like_matches_macro)]
619#[deny(unreachable_patterns, unused_variables)]
620impl NamedChain {
621    /// Returns the string representation of the chain.
622    #[inline]
623    pub fn as_str(&self) -> &'static str {
624        self.into()
625    }
626
627    /// Returns `true` if this chain is Ethereum or an Ethereum testnet.
628    pub const fn is_ethereum(&self) -> bool {
629        use NamedChain::*;
630
631        matches!(self, Mainnet | Morden | Ropsten | Rinkeby | Goerli | Kovan | Holesky | Sepolia)
632    }
633
634    /// Returns true if the chain contains Optimism configuration.
635    pub const fn is_optimism(self) -> bool {
636        use NamedChain::*;
637
638        matches!(
639            self,
640            Optimism
641                | OptimismGoerli
642                | OptimismKovan
643                | OptimismSepolia
644                | Base
645                | BaseGoerli
646                | BaseSepolia
647                | Fraxtal
648                | FraxtalTestnet
649                | Ink
650                | InkSepolia
651                | Mode
652                | ModeSepolia
653                | Pgn
654                | PgnSepolia
655                | Zora
656                | ZoraSepolia
657                | BlastSepolia
658                | OpBNBMainnet
659                | OpBNBTestnet
660                | Soneium
661                | SoneiumMinatoTestnet
662                | Odyssey
663                | World
664                | WorldSepolia
665                | Unichain
666                | UnichainSepolia
667                | HappychainTestnet
668        )
669    }
670
671    /// Returns true if the chain contains Arbitrum configuration.
672    pub const fn is_arbitrum(self) -> bool {
673        use NamedChain::*;
674
675        matches!(self, Arbitrum | ArbitrumTestnet | ArbitrumGoerli | ArbitrumSepolia | ArbitrumNova)
676    }
677
678    /// Returns true if the chain contains Elastic Network configuration.
679    pub const fn is_elastic(self) -> bool {
680        use NamedChain::*;
681
682        matches!(
683            self,
684            ZkSync
685                | ZkSyncTestnet
686                | Abstract
687                | AbstractTestnet
688                | Sophon
689                | SophonTestnet
690                | Lens
691                | LensTestnet
692        )
693    }
694
695    /// Returns the chain's average blocktime, if applicable.
696    ///
697    /// It can be beneficial to know the average blocktime to adjust the polling of an HTTP provider
698    /// for example.
699    ///
700    /// **Note:** this is not an accurate average, but is rather a sensible default derived from
701    /// blocktime charts such as [Etherscan's](https://etherscan.com/chart/blocktime)
702    /// or [Polygonscan's](https://polygonscan.com/chart/blocktime).
703    ///
704    /// # Examples
705    ///
706    /// ```
707    /// use alloy_chains::NamedChain;
708    /// use std::time::Duration;
709    ///
710    /// assert_eq!(NamedChain::Mainnet.average_blocktime_hint(), Some(Duration::from_millis(12_000)),);
711    /// assert_eq!(NamedChain::Optimism.average_blocktime_hint(), Some(Duration::from_millis(2_000)),);
712    /// ```
713    pub const fn average_blocktime_hint(self) -> Option<Duration> {
714        use NamedChain::*;
715
716        Some(Duration::from_millis(match self {
717            Mainnet | Taiko | TaikoHekla => 12_000,
718
719            Arbitrum
720            | ArbitrumTestnet
721            | ArbitrumGoerli
722            | ArbitrumSepolia
723            | GravityAlphaMainnet
724            | GravityAlphaTestnetSepolia
725            | Xai
726            | XaiSepolia
727            | Syndr
728            | SyndrSepolia
729            | ArbitrumNova
730            | ApeChain
731            | Curtis
732            | SuperpositionTestnet
733            | Superposition => 260,
734
735            Optimism | OptimismGoerli | OptimismSepolia | Base | BaseGoerli | BaseSepolia
736            | Blast | BlastSepolia | Fraxtal | FraxtalTestnet | Zora | ZoraSepolia | Mantle
737            | MantleSepolia | Mode | ModeSepolia | Pgn | PgnSepolia | HappychainTestnet
738            | Soneium | SoneiumMinatoTestnet | Bob | BobSepolia => 2_000,
739
740            Ink | InkSepolia | Odyssey => 1_000,
741
742            Viction => 2_000,
743
744            Polygon | PolygonMumbai | PolygonAmoy => 2_100,
745
746            Acala | AcalaMandalaTestnet | AcalaTestnet | Karura | KaruraTestnet | Moonbeam
747            | Moonriver => 12_500,
748
749            BinanceSmartChain | BinanceSmartChainTestnet => 3_000,
750
751            Avalanche | AvalancheFuji => 2_000,
752
753            Fantom | FantomTestnet => 1_200,
754
755            Cronos | CronosTestnet | Canto | CantoTestnet => 5_700,
756
757            Evmos | EvmosTestnet => 1_900,
758
759            Aurora | AuroraTestnet => 1_100,
760
761            Oasis => 5_500,
762
763            Emerald | Darwinia | Crab | Koi => 6_000,
764
765            Dev | AnvilHardhat => 200,
766
767            Celo | CeloAlfajores | CeloBaklava => 5_000,
768
769            FilecoinCalibrationTestnet | FilecoinMainnet => 30_000,
770
771            Scroll | ScrollSepolia => 3_000,
772
773            Shimmer => 5_000,
774
775            Gnosis | Chiado => 5_000,
776
777            Elastos => 5_000,
778
779            Etherlink => 5_000,
780
781            EtherlinkTestnet => 5_000,
782
783            Degen => 600,
784
785            Cfx | CfxTestnet => 500,
786
787            OpBNBMainnet | OpBNBTestnet | AutonomysNovaTestnet => 1_000,
788
789            Ronin | RoninTestnet => 3_000,
790
791            Flare => 1_800,
792
793            FlareCoston2 => 2_500,
794
795            Pulsechain => 10000,
796            PulsechainTestnet => 10101,
797
798            Immutable | ImmutableTestnet => 2_000,
799
800            World | WorldSepolia => 2_000,
801
802            Iotex => 5_000,
803            Core => 3_000,
804            Merlin => 3_000,
805            Bitlayer => 3_000,
806            Vana => 6_000,
807            Zeta => 6_000,
808            Kaia => 1_000,
809            Story => 2_500,
810            Sei => 500,
811
812            Sonic => 1_000,
813
814            TelosEvm | TelosEvmTestnet => 500,
815
816            UnichainSepolia | Unichain => 1_000,
817
818            BerachainBepolia | Berachain => 2_000,
819
820            MonadTestnet => 500,
821
822            Hyperliquid => 2_000,
823
824            Abstract | AbstractTestnet => 1_000,
825            ZkSync | ZkSyncTestnet => 1_000,
826            Sophon | SophonTestnet => 1_000,
827            Lens | LensTestnet => 1_000,
828            Rsk | RskTestnet => 25_000,
829            InjectiveTestnet => 700,
830
831            Morden | Ropsten | Rinkeby | Goerli | Kovan | Sepolia | Holesky | Hoodi
832            | MantleTestnet | Moonbase | MoonbeamDev | OptimismKovan | Poa | Sokol
833            | EmeraldTestnet | Boba | PolygonZkEvm | PolygonZkEvmTestnet | Metis | Linea
834            | LineaGoerli | LineaSepolia | KakarotSepolia | SonicBlaze | SonicTestnet
835            | Treasure | TreasureTopaz | Corn | CornTestnet => return None,
836        }))
837    }
838
839    /// Returns whether the chain implements EIP-1559 (with the type 2 EIP-2718 transaction type).
840    ///
841    /// # Examples
842    ///
843    /// ```
844    /// use alloy_chains::NamedChain;
845    ///
846    /// assert!(!NamedChain::Mainnet.is_legacy());
847    /// assert!(NamedChain::Fantom.is_legacy());
848    /// ```
849    pub const fn is_legacy(self) -> bool {
850        use NamedChain::*;
851
852        match self {
853            // Known legacy chains / non EIP-1559 compliant.
854            CeloAlfajores | CeloBaklava | Elastos | Emerald | EmeraldTestnet | Fantom
855            | FantomTestnet | MantleTestnet | OptimismKovan | PolygonZkEvm
856            | PolygonZkEvmTestnet | Ronin | RoninTestnet | Rsk | RskTestnet | Shimmer
857            | Treasure | TreasureTopaz | Viction | Sophon | SophonTestnet => true,
858
859            // Known EIP-1559 chains.
860            Mainnet
861            | Goerli
862            | Sepolia
863            | Holesky
864            | Hoodi
865            | Odyssey
866            | Acala
867            | AcalaMandalaTestnet
868            | AcalaTestnet
869            | ArbitrumTestnet
870            | Base
871            | BaseGoerli
872            | BaseSepolia
873            | Boba
874            | Metis
875            | Oasis
876            | Blast
877            | BlastSepolia
878            | Celo
879            | Fraxtal
880            | FraxtalTestnet
881            | Optimism
882            | OptimismGoerli
883            | OptimismSepolia
884            | Bob
885            | BobSepolia
886            | Polygon
887            | PolygonMumbai
888            | PolygonAmoy
889            | Avalanche
890            | AvalancheFuji
891            | Arbitrum
892            | ArbitrumGoerli
893            | ArbitrumSepolia
894            | ArbitrumNova
895            | GravityAlphaMainnet
896            | GravityAlphaTestnetSepolia
897            | Xai
898            | XaiSepolia
899            | HappychainTestnet
900            | Syndr
901            | SyndrSepolia
902            | FilecoinMainnet
903            | Linea
904            | LineaGoerli
905            | LineaSepolia
906            | FilecoinCalibrationTestnet
907            | Gnosis
908            | Chiado
909            | Zora
910            | ZoraSepolia
911            | Ink
912            | InkSepolia
913            | Mantle
914            | MantleSepolia
915            | Mode
916            | ModeSepolia
917            | Pgn
918            | PgnSepolia
919            | KakarotSepolia
920            | Etherlink
921            | EtherlinkTestnet
922            | Degen
923            | OpBNBMainnet
924            | OpBNBTestnet
925            | Taiko
926            | TaikoHekla
927            | AutonomysNovaTestnet
928            | Flare
929            | FlareCoston2
930            | Scroll
931            | ScrollSepolia
932            | Darwinia
933            | Cfx
934            | CfxTestnet
935            | Crab
936            | Pulsechain
937            | PulsechainTestnet
938            | Koi
939            | Immutable
940            | ImmutableTestnet
941            | Soneium
942            | SoneiumMinatoTestnet
943            | Sonic
944            | World
945            | WorldSepolia
946            | Unichain
947            | UnichainSepolia
948            | ApeChain
949            | BerachainBepolia
950            | Berachain
951            | Curtis
952            | SuperpositionTestnet
953            | Superposition
954            | MonadTestnet
955            | Hyperliquid
956            | Corn
957            | CornTestnet
958            | ZkSync
959            | ZkSyncTestnet
960            | AbstractTestnet
961            | Abstract
962            | Lens
963            | LensTestnet
964            | BinanceSmartChain
965            | BinanceSmartChainTestnet
966            | Karura
967            | KaruraTestnet
968            | TelosEvm
969            | TelosEvmTestnet => false,
970
971            // Unknown / not applicable, default to false for backwards compatibility.
972            Dev | AnvilHardhat | Morden | Ropsten | Rinkeby | Cronos | CronosTestnet | Kovan
973            | Sokol | Poa | Moonbeam | MoonbeamDev | Moonriver | Moonbase | Evmos
974            | EvmosTestnet | Aurora | AuroraTestnet | Canto | CantoTestnet | Iotex | Core
975            | Merlin | Bitlayer | SonicBlaze | SonicTestnet | Vana | Zeta | Kaia | Story | Sei
976            | InjectiveTestnet => false,
977        }
978    }
979
980    /// Returns whether the chain supports the [Shanghai hardfork][ref].
981    ///
982    /// [ref]: https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/shanghai.md
983    pub const fn supports_shanghai(self) -> bool {
984        use NamedChain::*;
985
986        matches!(
987            self,
988            Mainnet
989                | Goerli
990                | Sepolia
991                | Holesky
992                | Hoodi
993                | AnvilHardhat
994                | Optimism
995                | OptimismGoerli
996                | OptimismSepolia
997                | Bob
998                | BobSepolia
999                | Odyssey
1000                | Base
1001                | BaseGoerli
1002                | BaseSepolia
1003                | Blast
1004                | BlastSepolia
1005                | Fraxtal
1006                | FraxtalTestnet
1007                | Ink
1008                | InkSepolia
1009                | Gnosis
1010                | Chiado
1011                | ZoraSepolia
1012                | Mantle
1013                | MantleSepolia
1014                | Mode
1015                | ModeSepolia
1016                | PolygonMumbai
1017                | Polygon
1018                | Arbitrum
1019                | ArbitrumNova
1020                | ArbitrumSepolia
1021                | GravityAlphaMainnet
1022                | GravityAlphaTestnetSepolia
1023                | Xai
1024                | XaiSepolia
1025                | Syndr
1026                | SyndrSepolia
1027                | Etherlink
1028                | EtherlinkTestnet
1029                | Scroll
1030                | ScrollSepolia
1031                | HappychainTestnet
1032                | Shimmer
1033                | BinanceSmartChain
1034                | BinanceSmartChainTestnet
1035                | OpBNBMainnet
1036                | OpBNBTestnet
1037                | KakarotSepolia
1038                | Taiko
1039                | TaikoHekla
1040                | Avalanche
1041                | AvalancheFuji
1042                | AutonomysNovaTestnet
1043                | Acala
1044                | AcalaMandalaTestnet
1045                | AcalaTestnet
1046                | Karura
1047                | KaruraTestnet
1048                | Darwinia
1049                | Crab
1050                | Cfx
1051                | CfxTestnet
1052                | Pulsechain
1053                | PulsechainTestnet
1054                | Koi
1055                | Immutable
1056                | ImmutableTestnet
1057                | Soneium
1058                | SoneiumMinatoTestnet
1059                | World
1060                | WorldSepolia
1061                | Iotex
1062                | Unichain
1063                | UnichainSepolia
1064                | ApeChain
1065                | Curtis
1066                | SuperpositionTestnet
1067                | Superposition
1068                | MonadTestnet
1069                | Corn
1070                | CornTestnet
1071                | Rsk
1072                | RskTestnet
1073                | Berachain
1074                | BerachainBepolia
1075                | InjectiveTestnet
1076        )
1077    }
1078
1079    #[doc(hidden)]
1080    #[deprecated(since = "0.1.3", note = "use `supports_shanghai` instead")]
1081    pub const fn supports_push0(self) -> bool {
1082        self.supports_shanghai()
1083    }
1084
1085    /// Returns whether the chain is a testnet.
1086    pub const fn is_testnet(self) -> bool {
1087        use NamedChain::*;
1088
1089        match self {
1090            // Ethereum testnets.
1091            Goerli | Holesky | Kovan | Sepolia | Morden | Ropsten | Rinkeby | Hoodi => true,
1092
1093            // Other testnets.
1094            ArbitrumGoerli
1095            | ArbitrumSepolia
1096            | ArbitrumTestnet
1097            | SyndrSepolia
1098            | AuroraTestnet
1099            | AvalancheFuji
1100            | Odyssey
1101            | BaseGoerli
1102            | BaseSepolia
1103            | BlastSepolia
1104            | BinanceSmartChainTestnet
1105            | CantoTestnet
1106            | CronosTestnet
1107            | CeloAlfajores
1108            | CeloBaklava
1109            | EmeraldTestnet
1110            | EvmosTestnet
1111            | FantomTestnet
1112            | FilecoinCalibrationTestnet
1113            | FraxtalTestnet
1114            | HappychainTestnet
1115            | LineaGoerli
1116            | LineaSepolia
1117            | InkSepolia
1118            | MantleTestnet
1119            | MantleSepolia
1120            | MoonbeamDev
1121            | OptimismGoerli
1122            | OptimismKovan
1123            | OptimismSepolia
1124            | BobSepolia
1125            | PolygonMumbai
1126            | PolygonAmoy
1127            | PolygonZkEvmTestnet
1128            | ScrollSepolia
1129            | Shimmer
1130            | ZkSyncTestnet
1131            | ZoraSepolia
1132            | ModeSepolia
1133            | PgnSepolia
1134            | KakarotSepolia
1135            | EtherlinkTestnet
1136            | OpBNBTestnet
1137            | RoninTestnet
1138            | TaikoHekla
1139            | AutonomysNovaTestnet
1140            | FlareCoston2
1141            | AcalaMandalaTestnet
1142            | AcalaTestnet
1143            | KaruraTestnet
1144            | CfxTestnet
1145            | PulsechainTestnet
1146            | GravityAlphaTestnetSepolia
1147            | XaiSepolia
1148            | Koi
1149            | ImmutableTestnet
1150            | SoneiumMinatoTestnet
1151            | WorldSepolia
1152            | UnichainSepolia
1153            | Curtis
1154            | TreasureTopaz
1155            | SonicBlaze
1156            | SonicTestnet
1157            | BerachainBepolia
1158            | SuperpositionTestnet
1159            | MonadTestnet
1160            | RskTestnet
1161            | TelosEvmTestnet
1162            | AbstractTestnet
1163            | LensTestnet
1164            | SophonTestnet
1165            | InjectiveTestnet
1166            | CornTestnet => true,
1167
1168            // Dev chains.
1169            Dev | AnvilHardhat => true,
1170
1171            // Mainnets.
1172            Mainnet | Optimism | Arbitrum | ArbitrumNova | Blast | Syndr | Cronos | Rsk
1173            | BinanceSmartChain | Poa | Sokol | Scroll | Metis | Gnosis | Polygon
1174            | PolygonZkEvm | Fantom | Moonbeam | Moonriver | Moonbase | Evmos | Chiado | Oasis
1175            | Emerald | FilecoinMainnet | Avalanche | Celo | Aurora | Canto | Boba | Base
1176            | Fraxtal | Ink | Linea | ZkSync | Mantle | GravityAlphaMainnet | Xai | Zora | Pgn
1177            | Mode | Viction | Elastos | Degen | OpBNBMainnet | Ronin | Taiko | Flare | Acala
1178            | Karura | Darwinia | Cfx | Crab | Pulsechain | Etherlink | Immutable | World
1179            | Iotex | Core | Merlin | Bitlayer | ApeChain | Vana | Zeta | Kaia | Treasure | Bob
1180            | Soneium | Sonic | Superposition | Berachain | Unichain | TelosEvm | Story | Sei
1181            | Hyperliquid | Abstract | Sophon | Lens | Corn => false,
1182        }
1183    }
1184
1185    /// Returns the symbol of the chain's native currency.
1186    pub const fn native_currency_symbol(self) -> Option<&'static str> {
1187        use NamedChain::*;
1188
1189        Some(match self {
1190            Mainnet | Goerli | Holesky | Kovan | Sepolia | Morden | Ropsten | Rinkeby | Scroll
1191            | ScrollSepolia | Taiko | TaikoHekla | Unichain | UnichainSepolia
1192            | SuperpositionTestnet | Superposition | Abstract | ZkSync | ZkSyncTestnet => "ETH",
1193
1194            Mantle | MantleSepolia => "MNT",
1195
1196            GravityAlphaMainnet | GravityAlphaTestnetSepolia => "G",
1197
1198            Xai | XaiSepolia => "XAI",
1199
1200            HappychainTestnet => "HAPPY",
1201
1202            BinanceSmartChain | BinanceSmartChainTestnet | OpBNBMainnet | OpBNBTestnet => "BNB",
1203
1204            Etherlink | EtherlinkTestnet => "XTZ",
1205
1206            Degen => "DEGEN",
1207
1208            Ronin | RoninTestnet => "RON",
1209
1210            Shimmer => "SMR",
1211
1212            Flare => "FLR",
1213
1214            FlareCoston2 => "C2FLR",
1215
1216            Darwinia => "RING",
1217
1218            Crab => "CRAB",
1219
1220            Koi => "KRING",
1221
1222            Cfx | CfxTestnet => "CFX",
1223            Pulsechain | PulsechainTestnet => "PLS",
1224
1225            Immutable => "IMX",
1226            ImmutableTestnet => "tIMX",
1227
1228            World | WorldSepolia => "WRLD",
1229
1230            Iotex => "IOTX",
1231            Core => "CORE",
1232            Merlin => "BTC",
1233            Bitlayer => "BTC",
1234            Vana => "VANA",
1235            Zeta => "ZETA",
1236            Kaia => "KAIA",
1237            Story => "IP",
1238            Sei => "SEI",
1239            ApeChain | Curtis => "APE",
1240
1241            Treasure | TreasureTopaz => "MAGIC",
1242
1243            BerachainBepolia | Berachain => "BERA",
1244
1245            Sonic | SonicBlaze => "S",
1246
1247            TelosEvm | TelosEvmTestnet => "TLOS",
1248
1249            Hyperliquid => "HYPE",
1250
1251            Polygon | PolygonMumbai | PolygonZkEvm | PolygonZkEvmTestnet | PolygonAmoy => "POL",
1252
1253            Corn | CornTestnet => "BTCN",
1254
1255            Sophon | SophonTestnet => "SOPH",
1256
1257            LensTestnet => "GRASS",
1258            Lens => "GHO",
1259
1260            Rsk => "RBTC",
1261            RskTestnet => "tRBTC",
1262
1263            InjectiveTestnet => "INJ",
1264
1265            _ => return None,
1266        })
1267    }
1268
1269    /// Returns the chain's blockchain explorer and its API (Etherscan and Etherscan-like) URLs.
1270    ///
1271    /// Returns `(API_URL, BASE_URL)`.
1272    ///
1273    /// All URLs have no trailing `/`
1274    ///
1275    /// # Examples
1276    ///
1277    /// ```
1278    /// use alloy_chains::NamedChain;
1279    ///
1280    /// assert_eq!(
1281    ///     NamedChain::Mainnet.etherscan_urls(),
1282    ///     Some(("https://api.etherscan.io/v2/api?chainid=1", "https://etherscan.io"))
1283    /// );
1284    /// assert_eq!(
1285    ///     NamedChain::Avalanche.etherscan_urls(),
1286    ///     Some(("https://api.etherscan.io/v2/api?chainid=43114", "https://snowtrace.io"))
1287    /// );
1288    /// assert_eq!(NamedChain::AnvilHardhat.etherscan_urls(), None);
1289    /// ```
1290    pub const fn etherscan_urls(self) -> Option<(&'static str, &'static str)> {
1291        use NamedChain::*;
1292
1293        Some(match self {
1294            Mainnet => ("https://api.etherscan.io/v2/api?chainid=1", "https://etherscan.io"),
1295            Ropsten => ("https://api-ropsten.etherscan.io/api", "https://ropsten.etherscan.io"),
1296            Kovan => ("https://api-kovan.etherscan.io/api", "https://kovan.etherscan.io"),
1297            Rinkeby => ("https://api-rinkeby.etherscan.io/api", "https://rinkeby.etherscan.io"),
1298            Goerli => ("https://api-goerli.etherscan.io/api", "https://goerli.etherscan.io"),
1299            Sepolia => {
1300                ("https://api.etherscan.io/v2/api?chainid=11155111", "https://sepolia.etherscan.io")
1301            }
1302            Holesky => {
1303                ("https://api.etherscan.io/v2/api?chainid=17000", "https://holesky.etherscan.io")
1304            }
1305            Hoodi => {
1306                ("https://api.etherscan.io/v2/api?chainid=560048", "https://hoodi.etherscan.io")
1307            }
1308
1309            Polygon => ("https://api.etherscan.io/v2/api?chainid=137", "https://polygonscan.com"),
1310            PolygonMumbai => {
1311                ("https://api-testnet.polygonscan.com/api", "https://mumbai.polygonscan.com")
1312            }
1313            PolygonAmoy => {
1314                ("https://api.etherscan.io/v2/api?chainid=80002", "https://amoy.polygonscan.com")
1315            }
1316
1317            PolygonZkEvm => {
1318                ("https://api.etherscan.io/v2/api?chainid=1101", "https://zkevm.polygonscan.com")
1319            }
1320            PolygonZkEvmTestnet => (
1321                "https://api-testnet-zkevm.polygonscan.com/api",
1322                "https://testnet-zkevm.polygonscan.com",
1323            ),
1324
1325            Avalanche => ("https://api.etherscan.io/v2/api?chainid=43114", "https://snowtrace.io"),
1326            AvalancheFuji => {
1327                ("https://api.etherscan.io/v2/api?chainid=43113", "https://testnet.snowtrace.io")
1328            }
1329
1330            Optimism => {
1331                ("https://api.etherscan.io/v2/api?chainid=10", "https://optimistic.etherscan.io")
1332            }
1333            OptimismGoerli => (
1334                "https://api-goerli-optimistic.etherscan.io/api",
1335                "https://goerli-optimism.etherscan.io",
1336            ),
1337            OptimismKovan => (
1338                "https://api-kovan-optimistic.etherscan.io/api",
1339                "https://kovan-optimistic.etherscan.io",
1340            ),
1341            OptimismSepolia => (
1342                "https://api.etherscan.io/v2/api?chainid=11155420",
1343                "https://sepolia-optimism.etherscan.io",
1344            ),
1345
1346            Bob => ("https://explorer.gobob.xyz/api", "https://explorer.gobob.xyz"),
1347            BobSepolia => (
1348                "https://bob-sepolia.explorer.gobob.xyz/api",
1349                "https://bob-sepolia.explorer.gobob.xyz",
1350            ),
1351
1352            Fantom => ("https://api.ftmscan.com/api", "https://ftmscan.com"),
1353            FantomTestnet => ("https://api-testnet.ftmscan.com/api", "https://testnet.ftmscan.com"),
1354
1355            BinanceSmartChain => {
1356                ("https://api.etherscan.io/v2/api?chainid=52", "https://bscscan.com")
1357            }
1358            BinanceSmartChainTestnet => {
1359                ("https://api.etherscan.io/v2/api?chainid=97", "https://testnet.bscscan.com")
1360            }
1361
1362            OpBNBMainnet => {
1363                ("https://api.etherscan.io/v2/api?chainid=204", "https://opbnb.bscscan.com")
1364            }
1365            OpBNBTestnet => (
1366                "https://api.etherscan.io/v2/api?chainid=5611",
1367                "https://opbnb-testnet.bscscan.com",
1368            ),
1369
1370            Arbitrum => ("https://api.etherscan.io/v2/api?chainid=42161", "https://arbiscan.io"),
1371            ArbitrumTestnet => {
1372                ("https://api-testnet.arbiscan.io/api", "https://testnet.arbiscan.io")
1373            }
1374            ArbitrumGoerli => ("https://api-goerli.arbiscan.io/api", "https://goerli.arbiscan.io"),
1375            ArbitrumSepolia => {
1376                ("https://api.etherscan.io/v2/api?chainid=421614", "https://sepolia.arbiscan.io")
1377            }
1378            ArbitrumNova => {
1379                ("https://api.etherscan.io/v2/api?chainid=42170", "https://nova.arbiscan.io")
1380            }
1381
1382            GravityAlphaMainnet => {
1383                ("https://explorer.gravity.xyz/api", "https://explorer.gravity.xyz")
1384            }
1385            GravityAlphaTestnetSepolia => {
1386                ("https://explorer-sepolia.gravity.xyz/api", "https://explorer-sepolia.gravity.xyz")
1387            }
1388            HappychainTestnet => {
1389                ("https://explorer.testnet.happy.tech/api", "https://explorer.testnet.happy.tech")
1390            }
1391
1392            XaiSepolia => (
1393                "https://api.etherscan.io/v2/api?chainid=37714555429",
1394                "https://sepolia.xaiscan.io",
1395            ),
1396            Xai => ("https://api.etherscan.io/v2/api?chainid=660279", "https://xaiscan.io"),
1397
1398            Syndr => ("https://explorer.syndr.com/api", "https://explorer.syndr.com"),
1399            SyndrSepolia => {
1400                ("https://sepolia-explorer.syndr.com/api", "https://sepolia-explorer.syndr.com")
1401            }
1402
1403            Cronos => ("https://api.etherscan.io/v2/api?chainid=25", "https://cronoscan.com"),
1404            CronosTestnet => {
1405                ("https://api-testnet.cronoscan.com/api", "https://testnet.cronoscan.com")
1406            }
1407
1408            Moonbeam => {
1409                ("https://api.etherscan.io/v2/api?chainid=1284", "https://moonbeam.moonscan.io")
1410            }
1411            Moonbase => {
1412                ("https://api.etherscan.io/v2/api?chainid=1287", "https://moonbase.moonscan.io")
1413            }
1414            Moonriver => {
1415                ("https://api.etherscan.io/v2/api?chainid=1285", "https://moonriver.moonscan.io")
1416            }
1417
1418            Gnosis => ("https://api.etherscan.io/v2/api?chainid=100", "https://gnosisscan.io"),
1419
1420            Scroll => ("https://api.etherscan.io/v2/api?chainid=534352", "https://scrollscan.com"),
1421            ScrollSepolia => {
1422                ("https://api.etherscan.io/v2/api?chainid=534351", "https://sepolia.scrollscan.com")
1423            }
1424
1425            Ink => ("https://explorer.inkonchain.com/api/v2", "https://explorer.inkonchain.com"),
1426            InkSepolia => (
1427                "https://explorer-sepolia.inkonchain.com/api/v2",
1428                "https://explorer-sepolia.inkonchain.com",
1429            ),
1430
1431            Shimmer => {
1432                ("https://explorer.evm.shimmer.network/api", "https://explorer.evm.shimmer.network")
1433            }
1434
1435            Metis => (
1436                "https://api.routescan.io/v2/network/mainnet/evm/1088/etherscan",
1437                "https://explorer.metis.io",
1438            ),
1439
1440            Chiado => {
1441                ("https://blockscout.chiadochain.net/api", "https://blockscout.chiadochain.net")
1442            }
1443
1444            FilecoinCalibrationTestnet => (
1445                "https://api.calibration.node.glif.io/rpc/v1",
1446                "https://calibration.filfox.info/en",
1447            ),
1448
1449            Sokol => ("https://blockscout.com/poa/sokol/api", "https://blockscout.com/poa/sokol"),
1450
1451            Poa => ("https://blockscout.com/poa/core/api", "https://blockscout.com/poa/core"),
1452
1453            Rsk => ("https://blockscout.com/rsk/mainnet/api", "https://blockscout.com/rsk/mainnet"),
1454            RskTestnet => (
1455                "https://rootstock-testnet.blockscout.com/api",
1456                "https://rootstock-testnet.blockscout.com",
1457            ),
1458
1459            Oasis => ("https://scan.oasischain.io/api", "https://scan.oasischain.io"),
1460
1461            Emerald => {
1462                ("https://explorer.emerald.oasis.dev/api", "https://explorer.emerald.oasis.dev")
1463            }
1464            EmeraldTestnet => (
1465                "https://testnet.explorer.emerald.oasis.dev/api",
1466                "https://testnet.explorer.emerald.oasis.dev",
1467            ),
1468
1469            Aurora => ("https://api.aurorascan.dev/api", "https://aurorascan.dev"),
1470            AuroraTestnet => {
1471                ("https://testnet.aurorascan.dev/api", "https://testnet.aurorascan.dev")
1472            }
1473
1474            Evmos => ("https://evm.evmos.org/api", "https://evm.evmos.org"),
1475            EvmosTestnet => ("https://evm.evmos.dev/api", "https://evm.evmos.dev"),
1476
1477            Celo => ("https://api.etherscan.io/v2/api?chainid=42220", "https://celoscan.io"),
1478            CeloAlfajores => {
1479                ("https://api.etherscan.io/v2/api?chainid=44787", "https://alfajores.celoscan.io")
1480            }
1481            CeloBaklava => {
1482                ("https://explorer.celo.org/baklava/api", "https://explorer.celo.org/baklava")
1483            }
1484
1485            Canto => ("https://evm.explorer.canto.io/api", "https://evm.explorer.canto.io"),
1486            CantoTestnet => (
1487                "https://testnet-explorer.canto.neobase.one/api",
1488                "https://testnet-explorer.canto.neobase.one",
1489            ),
1490
1491            Boba => ("https://api.bobascan.com/api", "https://bobascan.com"),
1492
1493            Base => ("https://api.etherscan.io/v2/api?chainid=8453", "https://basescan.org"),
1494            BaseGoerli => ("https://api-goerli.basescan.org/api", "https://goerli.basescan.org"),
1495            BaseSepolia => {
1496                ("https://api.etherscan.io/v2/api?chainid=84532", "https://sepolia.basescan.org")
1497            }
1498
1499            Fraxtal => ("https://api.etherscan.io/v2/api?chainid=252", "https://fraxscan.com"),
1500            FraxtalTestnet => {
1501                ("https://api.etherscan.io/v2/api?chainid=2522", "https://holesky.fraxscan.com")
1502            }
1503
1504            Blast => ("https://api.etherscan.io/v2/api?chainid=81457", "https://blastscan.io"),
1505            BlastSepolia => (
1506                "https://api.etherscan.io/v2/api?chainid=168587773",
1507                "https://sepolia.blastscan.io",
1508            ),
1509
1510            ZkSync => ("https://api.etherscan.io/v2/api?chainid=324", "https://era.zksync.network"),
1511            ZkSyncTestnet => (
1512                "https://api.etherscan.io/v2/api?chainid=300",
1513                "https://sepolia-era.zksync.network",
1514            ),
1515
1516            Linea => ("https://api.etherscan.io/v2/api?chainid=59144", "https://lineascan.build"),
1517            LineaGoerli => {
1518                ("https://explorer.goerli.linea.build/api", "https://explorer.goerli.linea.build")
1519            }
1520            LineaSepolia => {
1521                ("https://api.etherscan.io/v2/api?chainid=59141", "https://sepolia.lineascan.build")
1522            }
1523
1524            Mantle => {
1525                ("https://api.etherscan.io/v2/api?chainid=5000", "https://explorer.mantle.xyz")
1526            }
1527            MantleTestnet => {
1528                ("https://explorer.testnet.mantle.xyz/api", "https://explorer.testnet.mantle.xyz")
1529            }
1530            MantleSepolia => (
1531                "https://api.etherscan.io/v2/api?chainid=5003",
1532                "https://explorer.sepolia.mantle.xyz",
1533            ),
1534
1535            Viction => ("https://www.vicscan.xyz/api", "https://www.vicscan.xyz"),
1536
1537            Zora => ("https://explorer.zora.energy/api", "https://explorer.zora.energy"),
1538            ZoraSepolia => {
1539                ("https://sepolia.explorer.zora.energy/api", "https://sepolia.explorer.zora.energy")
1540            }
1541
1542            Pgn => {
1543                ("https://explorer.publicgoods.network/api", "https://explorer.publicgoods.network")
1544            }
1545
1546            PgnSepolia => (
1547                "https://explorer.sepolia.publicgoods.network/api",
1548                "https://explorer.sepolia.publicgoods.network",
1549            ),
1550
1551            Mode => ("https://explorer.mode.network/api", "https://explorer.mode.network"),
1552            ModeSepolia => (
1553                "https://sepolia.explorer.mode.network/api",
1554                "https://sepolia.explorer.mode.network",
1555            ),
1556
1557            Elastos => ("https://esc.elastos.io/api", "https://esc.elastos.io"),
1558            KakarotSepolia => {
1559                ("https://sepolia.kakarotscan.org/api", "https://sepolia.kakarotscan.org")
1560            }
1561            Etherlink => ("https://explorer.etherlink.com/api", "https://explorer.etherlink.com"),
1562            EtherlinkTestnet => (
1563                "https://testnet-explorer.etherlink.com/api",
1564                "https://testnet-explorer.etherlink.com",
1565            ),
1566            Degen => ("https://explorer.degen.tips/api", "https://explorer.degen.tips"),
1567            Ronin => ("https://skynet-api.roninchain.com/ronin", "https://app.roninchain.com"),
1568            RoninTestnet => (
1569                "https://api-gateway.skymavis.com/rpc/testnet",
1570                "https://saigon-app.roninchain.com",
1571            ),
1572            Taiko => ("https://api.etherscan.io/v2/api?chainid=167000", "https://taikoscan.io"),
1573            TaikoHekla => {
1574                ("https://api.etherscan.io/v2/api?chainid=167009", "https://hekla.taikoscan.io")
1575            }
1576            Flare => {
1577                ("https://flare-explorer.flare.network/api", "https://flare-explorer.flare.network")
1578            }
1579            FlareCoston2 => (
1580                "https://coston2-explorer.flare.network/api",
1581                "https://coston2-explorer.flare.network",
1582            ),
1583            Acala => ("https://blockscout.acala.network/api", "https://blockscout.acala.network"),
1584            AcalaMandalaTestnet => (
1585                "https://blockscout.mandala.aca-staging.network/api",
1586                "https://blockscout.mandala.aca-staging.network",
1587            ),
1588            AcalaTestnet => (
1589                "https://blockscout.acala-testnet.aca-staging.network/api",
1590                "https://blockscout.acala-testnet.aca-staging.network",
1591            ),
1592            Karura => {
1593                ("https://blockscout.karura.network/api", "https://blockscout.karura.network")
1594            }
1595            KaruraTestnet => (
1596                "https://blockscout.karura-testnet.aca-staging.network/api",
1597                "https://blockscout.karura-testnet.aca-staging.network",
1598            ),
1599
1600            Darwinia => {
1601                ("https://explorer.darwinia.network/api", "https://explorer.darwinia.network")
1602            }
1603            Crab => {
1604                ("https://crab-scan.darwinia.network/api", "https://crab-scan.darwinia.network")
1605            }
1606            Koi => ("https://koi-scan.darwinia.network/api", "https://koi-scan.darwinia.network"),
1607            Cfx => ("https://evmapi.confluxscan.net/api", "https://evm.confluxscan.io"),
1608            CfxTestnet => {
1609                ("https://evmapi-testnet.confluxscan.net/api", "https://evmtestnet.confluxscan.io")
1610            }
1611            Pulsechain => ("https://api.scan.pulsechain.com", "https://scan.pulsechain.com"),
1612            PulsechainTestnet => (
1613                "https://api.scan.v4.testnet.pulsechain.com",
1614                "https://scan.v4.testnet.pulsechain.com",
1615            ),
1616
1617            Immutable => ("https://explorer.immutable.com/api", "https://explorer.immutable.com"),
1618            ImmutableTestnet => (
1619                "https://explorer.testnet.immutable.com/api",
1620                "https://explorer.testnet.immutable.com",
1621            ),
1622            Soneium => ("https://soneium.blockscout.com/api", "https://soneium.blockscout.com"),
1623            SoneiumMinatoTestnet => (
1624                "https://soneium-minato.blockscout.com/api",
1625                "https://soneium-minato.blockscout.com",
1626            ),
1627            Odyssey => {
1628                ("https://odyssey-explorer.ithaca.xyz/api", "https://odyssey-explorer.ithaca.xyz")
1629            }
1630            World => ("https://api.etherscan.io/v2/api?chainid=480", "https://worldscan.org"),
1631            WorldSepolia => {
1632                ("https://api.etherscan.io/v2/api?chainid=4801", "https://sepolia.worldscan.org")
1633            }
1634            Unichain => ("https://api.etherscan.io/v2/api?chainid=130", "https://uniscan.xyz"),
1635            UnichainSepolia => {
1636                ("https://api.etherscan.io/v2/api?chainid=1301", "https://sepolia.uniscan.xyz")
1637            }
1638            Core => ("https://openapi.coredao.org/api", "https://scan.coredao.org"),
1639            Merlin => ("https://scan.merlinchain.io/api", "https://scan.merlinchain.io"),
1640            Bitlayer => ("https://api.btrscan.com/scan/api", "https://www.btrscan.com"),
1641            Vana => ("https://api.vanascan.io/api", "https://vanascan.io"),
1642            Zeta => ("https://zetachain.blockscout.com/api", "https://zetachain.blockscout.com"),
1643            Kaia => ("https://mainnet-oapi.kaiascan.io/api", "https://kaiascan.io"),
1644            Story => ("https://www.storyscan.xyz/api/v2", "https://www.storyscan.xyz"),
1645
1646            ApeChain => ("https://api.etherscan.io/v2/api?chainid=33139", "https://apescan.io"),
1647            Curtis => {
1648                ("https://api.etherscan.io/v2/api?chainid=33111", "https://curtis.apescan.io")
1649            }
1650            SonicBlaze => {
1651                ("https://api.etherscan.io/v2/api?chainid=57054", "https://testnet.sonicscan.org")
1652            }
1653            SonicTestnet => (
1654                "https://api.routescan.io/v2/network/testnet/evm/64165/etherscan/api",
1655                "https://scan.soniclabs.com",
1656            ),
1657            Sonic => ("https://api.etherscan.io/v2/api?chainid=146", "https://sonicscan.org"),
1658            Treasure => ("https://block-explorer.treasurescan.io/api", "https://treasurescan.io"),
1659            TreasureTopaz => (
1660                "https://block-explorer.topaz.treasurescan.io/api",
1661                "https://topaz.treasurescan.io",
1662            ),
1663            BerachainBepolia => {
1664                ("https://api.etherscan.io/v2/api?chainid=80069", "https://testnet.berascan.com")
1665            }
1666            Berachain => ("https://api.etherscan.io/v2/api?chainid=80094", "https://berascan.com"),
1667            SuperpositionTestnet => (
1668                "https://testnet-explorer.superposition.so/api",
1669                "https://testnet-explorer.superposition.so",
1670            ),
1671            Superposition => {
1672                ("https://explorer.superposition.so/api", "https://explorer.superposition.so")
1673            }
1674            MonadTestnet => {
1675                ("https://api.etherscan.io/v2/api?chainid=10143", "https://testnet.monadscan.com")
1676            }
1677            TelosEvm => ("https://api.teloscan.io/api", "https://teloscan.io"),
1678            TelosEvmTestnet => {
1679                ("https://api.testnet.teloscan.io/api", "https://testnet.teloscan.io")
1680            }
1681            Hyperliquid => (
1682                "https://hyperliquid.cloud.blockscout.com/api/v2",
1683                "https://hyperliquid.cloud.blockscout.com",
1684            ),
1685            Abstract => ("https://api.etherscan.io/v2/api?chainid=2741", "https://abscan.org"),
1686            AbstractTestnet => {
1687                ("https://api.etherscan.io/v2/api?chainid=11124", "https://sepolia.abscan.org")
1688            }
1689            Corn => (
1690                "https://api.routescan.io/v2/network/mainnet/evm/21000000/etherscan/api",
1691                "https://cornscan.io",
1692            ),
1693            CornTestnet => (
1694                "https://api.routescan.io/v2/network/testnet/evm/21000001/etherscan/api",
1695                "https://testnet.cornscan.io",
1696            ),
1697            // TODO: add hoodi etherscan when live
1698            AnvilHardhat | Dev | Morden | MoonbeamDev | FilecoinMainnet | AutonomysNovaTestnet
1699            | Iotex | Sei | InjectiveTestnet => {
1700                return None;
1701            }
1702            Sophon => ("https://api.etherscan.io/v2/api?chainid=50104", "https://sophscan.xyz"),
1703            SophonTestnet => (
1704                "https://api.etherscan.io/v2/api?chainid=531050104",
1705                "https://testnet.sophscan.xyz",
1706            ),
1707            Lens => ("https://explorer-api.lens.xyz", "https://explorer.lens.xyz"),
1708            LensTestnet => (
1709                "https://block-explorer-api.staging.lens.zksync.dev",
1710                "https://explorer.testnet.lens.xyz",
1711            ),
1712        })
1713    }
1714
1715    /// Returns the chain's blockchain explorer's API key environment variable's default name.
1716    ///
1717    /// # Examples
1718    ///
1719    /// ```
1720    /// use alloy_chains::NamedChain;
1721    ///
1722    /// assert_eq!(NamedChain::Mainnet.etherscan_api_key_name(), Some("ETHERSCAN_API_KEY"));
1723    /// assert_eq!(NamedChain::AnvilHardhat.etherscan_api_key_name(), None);
1724    /// ```
1725    pub const fn etherscan_api_key_name(self) -> Option<&'static str> {
1726        use NamedChain::*;
1727
1728        let api_key_name = match self {
1729            Mainnet
1730            | Morden
1731            | Ropsten
1732            | Kovan
1733            | Rinkeby
1734            | Goerli
1735            | Holesky
1736            | Hoodi
1737            | Optimism
1738            | OptimismGoerli
1739            | OptimismKovan
1740            | OptimismSepolia
1741            | BinanceSmartChain
1742            | BinanceSmartChainTestnet
1743            | OpBNBMainnet
1744            | OpBNBTestnet
1745            | Arbitrum
1746            | ArbitrumTestnet
1747            | ArbitrumGoerli
1748            | ArbitrumSepolia
1749            | ArbitrumNova
1750            | Syndr
1751            | SyndrSepolia
1752            | Cronos
1753            | CronosTestnet
1754            | Aurora
1755            | AuroraTestnet
1756            | Celo
1757            | CeloAlfajores
1758            | Base
1759            | Linea
1760            | LineaSepolia
1761            | Mantle
1762            | MantleTestnet
1763            | MantleSepolia
1764            | Xai
1765            | XaiSepolia
1766            | BaseGoerli
1767            | BaseSepolia
1768            | Fraxtal
1769            | FraxtalTestnet
1770            | Blast
1771            | BlastSepolia
1772            | Gnosis
1773            | Scroll
1774            | ScrollSepolia
1775            | Taiko
1776            | TaikoHekla
1777            | Unichain
1778            | UnichainSepolia
1779            | MonadTestnet
1780            | ApeChain
1781            | Abstract
1782            | AbstractTestnet
1783            | ZkSyncTestnet
1784            | ZkSync
1785            | Sophon
1786            | SophonTestnet => "ETHERSCAN_API_KEY",
1787
1788            Avalanche | AvalancheFuji => "SNOWTRACE_API_KEY",
1789
1790            Polygon | PolygonMumbai | PolygonAmoy | PolygonZkEvm | PolygonZkEvmTestnet => {
1791                "POLYGONSCAN_API_KEY"
1792            }
1793
1794            Fantom | FantomTestnet => "FTMSCAN_API_KEY",
1795
1796            Moonbeam | Moonbase | MoonbeamDev | Moonriver => "MOONSCAN_API_KEY",
1797
1798            Acala | AcalaMandalaTestnet | AcalaTestnet | Canto | CantoTestnet | CeloBaklava
1799            | Etherlink | EtherlinkTestnet | Flare | FlareCoston2 | KakarotSepolia | Karura
1800            | KaruraTestnet | Mode | ModeSepolia | Pgn | PgnSepolia | Shimmer | Zora
1801            | ZoraSepolia | Darwinia | Crab | Koi | Immutable | ImmutableTestnet | Soneium
1802            | SoneiumMinatoTestnet | World | WorldSepolia | Curtis | Ink | InkSepolia
1803            | SuperpositionTestnet | Superposition | Vana | Story | Hyperliquid => {
1804                "BLOCKSCOUT_API_KEY"
1805            }
1806
1807            Boba => "BOBASCAN_API_KEY",
1808
1809            Core => "CORESCAN_API_KEY",
1810            Merlin => "MERLINSCAN_API_KEY",
1811            Bitlayer => "BITLAYERSCAN_API_KEY",
1812            Zeta => "ZETASCAN_API_KEY",
1813            Kaia => "KAIASCAN_API_KEY",
1814            Sonic => "SONICSCAN_API_KEY",
1815            Berachain | BerachainBepolia => "BERASCAN_API_KEY",
1816            Corn | CornTestnet => "ROUTESCAN_API_KEY",
1817            // Explicitly exhaustive. See NB above.
1818            Metis
1819            | Chiado
1820            | Odyssey
1821            | Sepolia
1822            | Rsk
1823            | RskTestnet
1824            | Sokol
1825            | Poa
1826            | Oasis
1827            | Emerald
1828            | EmeraldTestnet
1829            | Evmos
1830            | EvmosTestnet
1831            | AnvilHardhat
1832            | Dev
1833            | GravityAlphaMainnet
1834            | GravityAlphaTestnetSepolia
1835            | Bob
1836            | BobSepolia
1837            | FilecoinMainnet
1838            | LineaGoerli
1839            | FilecoinCalibrationTestnet
1840            | Viction
1841            | Elastos
1842            | Degen
1843            | Ronin
1844            | RoninTestnet
1845            | Cfx
1846            | CfxTestnet
1847            | Pulsechain
1848            | PulsechainTestnet
1849            | AutonomysNovaTestnet
1850            | Iotex
1851            | HappychainTestnet
1852            | SonicBlaze
1853            | SonicTestnet
1854            | Treasure
1855            | TreasureTopaz
1856            | TelosEvm
1857            | TelosEvmTestnet
1858            | Lens
1859            | LensTestnet
1860            | Sei
1861            | InjectiveTestnet => return None,
1862        };
1863
1864        Some(api_key_name)
1865    }
1866
1867    /// Returns the chain's blockchain explorer's API key, from the environment variable with the
1868    /// name specified in [`etherscan_api_key_name`](NamedChain::etherscan_api_key_name).
1869    ///
1870    /// # Examples
1871    ///
1872    /// ```
1873    /// use alloy_chains::NamedChain;
1874    ///
1875    /// let chain = NamedChain::Mainnet;
1876    /// unsafe {
1877    ///     std::env::set_var(chain.etherscan_api_key_name().unwrap(), "KEY");
1878    /// }
1879    /// assert_eq!(chain.etherscan_api_key().as_deref(), Some("KEY"));
1880    /// ```
1881    #[cfg(feature = "std")]
1882    pub fn etherscan_api_key(self) -> Option<String> {
1883        self.etherscan_api_key_name().and_then(|name| std::env::var(name).ok())
1884    }
1885
1886    /// Returns the address of the public DNS node list for the given chain.
1887    ///
1888    /// See also <https://github.com/ethereum/discv4-dns-lists>.
1889    pub fn public_dns_network_protocol(self) -> Option<String> {
1890        use NamedChain::*;
1891
1892        const DNS_PREFIX: &str = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@";
1893        if let Mainnet | Goerli | Sepolia | Ropsten | Rinkeby | Holesky | Hoodi = self {
1894            // `{DNS_PREFIX}all.{self.lower()}.ethdisco.net`
1895            let mut s = String::with_capacity(DNS_PREFIX.len() + 32);
1896            s.push_str(DNS_PREFIX);
1897            s.push_str("all.");
1898            let chain_str = self.as_ref();
1899            s.push_str(chain_str);
1900            let l = s.len();
1901            s[l - chain_str.len()..].make_ascii_lowercase();
1902            s.push_str(".ethdisco.net");
1903
1904            Some(s)
1905        } else {
1906            None
1907        }
1908    }
1909
1910    /// Returns the address of the most popular wrapped native token address for this chain, if it
1911    /// exists.
1912    ///
1913    /// Example:
1914    ///
1915    /// ```
1916    /// use alloy_chains::NamedChain;
1917    /// use alloy_primitives::address;
1918    ///
1919    /// let chain = NamedChain::Mainnet;
1920    /// assert_eq!(
1921    ///     chain.wrapped_native_token(),
1922    ///     Some(address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"))
1923    /// );
1924    /// ```
1925    pub const fn wrapped_native_token(self) -> Option<Address> {
1926        use NamedChain::*;
1927
1928        let addr = match self {
1929            Mainnet => address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
1930            Optimism => address!("4200000000000000000000000000000000000006"),
1931            BinanceSmartChain => address!("bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"),
1932            OpBNBMainnet => address!("4200000000000000000000000000000000000006"),
1933            Arbitrum => address!("82af49447d8a07e3bd95bd0d56f35241523fbab1"),
1934            Base => address!("4200000000000000000000000000000000000006"),
1935            Linea => address!("e5d7c2a44ffddf6b295a15c148167daaaf5cf34f"),
1936            Mantle => address!("deaddeaddeaddeaddeaddeaddeaddeaddead1111"),
1937            Blast => address!("4300000000000000000000000000000000000004"),
1938            Gnosis => address!("e91d153e0b41518a2ce8dd3d7944fa863463a97d"),
1939            Scroll => address!("5300000000000000000000000000000000000004"),
1940            Taiko => address!("a51894664a773981c6c112c43ce576f315d5b1b6"),
1941            Avalanche => address!("b31f66aa3c1e785363f0875a1b74e27b85fd66c7"),
1942            Polygon => address!("0d500b1d8e8ef31e21c99d1db9a6444d3adf1270"),
1943            Fantom => address!("21be370d5312f44cb42ce377bc9b8a0cef1a4c83"),
1944            Iotex => address!("a00744882684c3e4747faefd68d283ea44099d03"),
1945            Core => address!("40375C92d9FAf44d2f9db9Bd9ba41a3317a2404f"),
1946            Merlin => address!("F6D226f9Dc15d9bB51182815b320D3fBE324e1bA"),
1947            Bitlayer => address!("ff204e2681a6fa0e2c3fade68a1b28fb90e4fc5f"),
1948            ApeChain => address!("48b62137EdfA95a428D35C09E44256a739F6B557"),
1949            Vana => address!("00EDdD9621Fb08436d0331c149D1690909a5906d"),
1950            Zeta => address!("5F0b1a82749cb4E2278EC87F8BF6B618dC71a8bf"),
1951            Kaia => address!("19aac5f612f524b754ca7e7c41cbfa2e981a4432"),
1952            Story => address!("1514000000000000000000000000000000000000"),
1953            Treasure => address!("263d8f36bb8d0d9526255e205868c26690b04b88"),
1954            Superposition => address!("1fB719f10b56d7a85DCD32f27f897375fB21cfdd"),
1955            Sonic => address!("039e2fB66102314Ce7b64Ce5Ce3E5183bc94aD38"),
1956            Berachain => address!("6969696969696969696969696969696969696969"),
1957            Hyperliquid => address!("5555555555555555555555555555555555555555"),
1958            Abstract => address!("3439153EB7AF838Ad19d56E1571FBD09333C2809"),
1959            Sei => address!("E30feDd158A2e3b13e9badaeABaFc5516e95e8C7"),
1960            ZkSync => address!("5aea5775959fbc2557cc8789bc1bf90a239d9a91"),
1961            Sophon => address!("f1f9e08a0818594fde4713ae0db1e46672ca960e"),
1962            Rsk => address!("967f8799af07df1534d48a95a5c9febe92c53ae0"),
1963            _ => return None,
1964        };
1965
1966        Some(addr)
1967    }
1968}
1969
1970#[cfg(test)]
1971mod tests {
1972    use super::*;
1973    use strum::{EnumCount, IntoEnumIterator};
1974
1975    #[allow(unused_imports)]
1976    use alloc::string::ToString;
1977
1978    #[test]
1979    #[cfg(feature = "serde")]
1980    fn default() {
1981        assert_eq!(serde_json::to_string(&NamedChain::default()).unwrap(), "\"mainnet\"");
1982    }
1983
1984    #[test]
1985    fn enum_iter() {
1986        assert_eq!(NamedChain::COUNT, NamedChain::iter().size_hint().0);
1987    }
1988
1989    #[test]
1990    fn roundtrip_string() {
1991        for chain in NamedChain::iter() {
1992            let chain_string = chain.to_string();
1993            assert_eq!(chain_string, format!("{chain}"));
1994            assert_eq!(chain_string.as_str(), chain.as_ref());
1995            #[cfg(feature = "serde")]
1996            assert_eq!(serde_json::to_string(&chain).unwrap(), format!("\"{chain_string}\""));
1997
1998            assert_eq!(chain_string.parse::<NamedChain>().unwrap(), chain);
1999        }
2000    }
2001
2002    #[test]
2003    #[cfg(feature = "serde")]
2004    fn roundtrip_serde() {
2005        for chain in NamedChain::iter() {
2006            let chain_string = serde_json::to_string(&chain).unwrap();
2007            let chain_string = chain_string.replace('-', "_");
2008            assert_eq!(serde_json::from_str::<'_, NamedChain>(&chain_string).unwrap(), chain);
2009        }
2010    }
2011
2012    #[test]
2013    #[cfg(feature = "arbitrary")]
2014    fn test_arbitrary_named_chain() {
2015        use arbitrary::{Arbitrary, Unstructured};
2016        let data = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 255];
2017        let mut unstructured = Unstructured::new(&data);
2018
2019        for _ in 0..10 {
2020            let _chain = NamedChain::arbitrary(&mut unstructured).unwrap();
2021        }
2022    }
2023
2024    #[test]
2025    fn aliases() {
2026        use NamedChain::*;
2027
2028        // kebab-case
2029        const ALIASES: &[(NamedChain, &[&str])] = &[
2030            (Mainnet, &["ethlive"]),
2031            (BinanceSmartChain, &["bsc", "bnb-smart-chain", "binance-smart-chain"]),
2032            (
2033                BinanceSmartChainTestnet,
2034                &["bsc-testnet", "bnb-smart-chain-testnet", "binance-smart-chain-testnet"],
2035            ),
2036            (Gnosis, &["gnosis", "gnosis-chain"]),
2037            (PolygonMumbai, &["mumbai"]),
2038            (PolygonZkEvm, &["zkevm", "polygon-zkevm"]),
2039            (PolygonZkEvmTestnet, &["zkevm-testnet", "polygon-zkevm-testnet"]),
2040            (AnvilHardhat, &["anvil", "hardhat"]),
2041            (AvalancheFuji, &["fuji"]),
2042            (ZkSync, &["zksync"]),
2043            (ZkSyncTestnet, &["zksync-testnet"]),
2044            (Mantle, &["mantle"]),
2045            (MantleTestnet, &["mantle-testnet"]),
2046            (MantleSepolia, &["mantle-sepolia"]),
2047            (GravityAlphaMainnet, &["gravity-alpha-mainnet"]),
2048            (GravityAlphaTestnetSepolia, &["gravity-alpha-testnet-sepolia"]),
2049            (Bob, &["bob"]),
2050            (BobSepolia, &["bob-sepolia"]),
2051            (HappychainTestnet, &["happychain-testnet"]),
2052            (Xai, &["xai"]),
2053            (XaiSepolia, &["xai-sepolia"]),
2054            (Base, &["base"]),
2055            (BaseGoerli, &["base-goerli"]),
2056            (BaseSepolia, &["base-sepolia"]),
2057            (Fraxtal, &["fraxtal"]),
2058            (FraxtalTestnet, &["fraxtal-testnet"]),
2059            (Ink, &["ink"]),
2060            (InkSepolia, &["ink-sepolia"]),
2061            (BlastSepolia, &["blast-sepolia"]),
2062            (Syndr, &["syndr"]),
2063            (SyndrSepolia, &["syndr-sepolia"]),
2064            (LineaGoerli, &["linea-goerli"]),
2065            (LineaSepolia, &["linea-sepolia"]),
2066            (AutonomysNovaTestnet, &["autonomys-nova-testnet"]),
2067            (Immutable, &["immutable"]),
2068            (ImmutableTestnet, &["immutable-testnet"]),
2069            (Soneium, &["soneium"]),
2070            (SoneiumMinatoTestnet, &["soneium-minato-testnet"]),
2071            (ApeChain, &["apechain"]),
2072            (Curtis, &["apechain-testnet", "curtis"]),
2073            (Treasure, &["treasure"]),
2074            (TreasureTopaz, &["treasure-topaz-testnet", "treasure-topaz"]),
2075            (BerachainBepolia, &["berachain-bepolia-testnet", "berachain-bepolia"]),
2076            (SuperpositionTestnet, &["superposition-testnet"]),
2077            (Superposition, &["superposition"]),
2078            (Hyperliquid, &["hyperliquid"]),
2079            (Abstract, &["abstract"]),
2080            (AbstractTestnet, &["abstract-testnet"]),
2081            (Sophon, &["sophon"]),
2082            (SophonTestnet, &["sophon-testnet"]),
2083            (Lens, &["lens"]),
2084            (LensTestnet, &["lens-testnet"]),
2085        ];
2086
2087        for &(chain, aliases) in ALIASES {
2088            for &alias in aliases {
2089                let named = alias.parse::<NamedChain>().expect(alias);
2090                assert_eq!(named, chain);
2091
2092                #[cfg(feature = "serde")]
2093                {
2094                    assert_eq!(
2095                        serde_json::from_str::<NamedChain>(&format!("\"{alias}\"")).unwrap(),
2096                        chain
2097                    );
2098
2099                    assert_eq!(
2100                        serde_json::from_str::<NamedChain>(&format!("\"{named}\"")).unwrap(),
2101                        chain
2102                    );
2103                }
2104            }
2105        }
2106    }
2107
2108    #[test]
2109    #[cfg(feature = "serde")]
2110    fn serde_to_string_match() {
2111        for chain in NamedChain::iter() {
2112            let chain_serde = serde_json::to_string(&chain).unwrap();
2113            let chain_string = format!("\"{chain}\"");
2114            assert_eq!(chain_serde, chain_string);
2115        }
2116    }
2117
2118    #[test]
2119    fn test_dns_network() {
2120        let s = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.mainnet.ethdisco.net";
2121        assert_eq!(NamedChain::Mainnet.public_dns_network_protocol().unwrap(), s);
2122    }
2123
2124    #[test]
2125    fn ensure_no_trailing_etherscan_url_separator() {
2126        for chain in NamedChain::iter() {
2127            if let Some((api, base)) = chain.etherscan_urls() {
2128                assert!(!api.ends_with('/'), "{chain:?} api url has trailing /");
2129                assert!(!base.ends_with('/'), "{chain:?} base url has trailing /");
2130            }
2131        }
2132    }
2133}