reth_optimism_chainspec/superchain/
chain_spec_macro.rs

1/// Create a chain spec for a given superchain and environment.
2#[macro_export]
3macro_rules! create_chain_spec {
4    ($name:expr, $environment:expr) => {
5        paste::paste! {
6            /// The Optimism $name $environment spec
7            pub static [<$name:upper _ $environment:upper>]: $crate::LazyLock<alloc::sync::Arc<$crate::OpChainSpec>> = $crate::LazyLock::new(|| {
8                $crate::OpChainSpec::from_genesis($crate::superchain::configs::read_superchain_genesis($name, $environment)
9                    .expect(&alloc::format!("Can't read {}-{} genesis", $name, $environment)))
10                    .into()
11            });
12        }
13    };
14}
15
16/// Generates the key string for a given name and environment pair.
17#[macro_export]
18macro_rules! key_for {
19    ($name:expr, "mainnet") => {
20        $name
21    };
22    ($name:expr, $env:expr) => {
23        concat!($name, "-", $env)
24    };
25}
26
27/// Create chain specs and an enum of every superchain (name, environment) pair.
28#[macro_export]
29macro_rules! create_superchain_specs {
30    ( $( ($name:expr, $env:expr) ),+ $(,)? ) => {
31        $(
32            $crate::create_chain_spec!($name, $env);
33        )+
34
35        paste::paste! {
36            /// All available superchains as an enum
37            #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38            #[allow(non_camel_case_types)]
39            pub enum Superchain {
40                $(
41                    #[doc = concat!("Superchain variant for `", $name, "-", $env, "`.")]
42                    [<$name:camel _ $env:camel>],
43                )+
44            }
45
46            impl Superchain {
47                /// A slice of every superchain enum variant
48                pub const ALL: &'static [Self] = &[
49                    $(
50                        Self::[<$name:camel _ $env:camel>],
51                    )+
52                ];
53
54                /// Returns the original name
55                pub const fn name(self) -> &'static str {
56                    match self {
57                        $(
58                            Self::[<$name:camel _ $env:camel>] => $name,
59                        )+
60                    }
61                }
62
63                /// Returns the original environment
64                pub const fn environment(self) -> &'static str {
65                    match self {
66                        $(
67                            Self::[<$name:camel _ $env:camel>] => $env,
68                        )+
69                    }
70                }
71            }
72
73            /// All supported superchains, including both older and newer naming,
74            /// for backwards compatibility
75            pub const SUPPORTED_CHAINS: &'static [&'static str] = &[
76                "optimism",
77                "optimism_sepolia",
78                "optimism-sepolia",
79                "base",
80                "base_sepolia",
81                "base-sepolia",
82                $(
83                    $crate::key_for!($name, $env),
84                )+
85                "dev",
86            ];
87
88            /// Parses the chain into an [`$crate::OpChainSpec`], if recognized.
89            pub fn generated_chain_value_parser(s: &str) -> Option<alloc::sync::Arc<$crate::OpChainSpec>> {
90                match s {
91                    "dev"                                   => Some($crate::OP_DEV.clone()),
92                    "optimism"                              => Some($crate::OP_MAINNET.clone()),
93                    "optimism_sepolia" | "optimism-sepolia" => Some($crate::OP_SEPOLIA.clone()),
94                    "base"                                  => Some($crate::BASE_MAINNET.clone()),
95                    "base_sepolia" | "base-sepolia"         => Some($crate::BASE_SEPOLIA.clone()),
96                    $(
97                        $crate::key_for!($name, $env)        => Some($crate::[<$name:upper _ $env:upper>].clone()),
98                    )+
99                    _                                       => None,
100                }
101            }
102        }
103    };
104}