kona_registry/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(
3    html_logo_url = "https://raw.githubusercontent.com/op-rs/kona/main/assets/square.png",
4    html_favicon_url = "https://raw.githubusercontent.com/op-rs/kona/main/assets/favicon.ico",
5    issue_tracker_base_url = "https://github.com/op-rs/kona/issues/"
6)]
7#![cfg_attr(not(test), warn(unused_crate_dependencies))]
8#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
9#![cfg_attr(not(feature = "std"), no_std)]
10
11extern crate alloc;
12
13pub use alloy_primitives::map::{DefaultHashBuilder, HashMap};
14pub use kona_genesis::{ChainConfig, RollupConfig};
15
16pub mod chain_list;
17pub use chain_list::{Chain, ChainList};
18
19pub mod superchain;
20pub use superchain::Registry;
21
22#[cfg(test)]
23pub mod test_utils;
24
25lazy_static::lazy_static! {
26    /// Private initializer that loads the superchain configurations.
27    static ref _INIT: Registry = Registry::from_chain_list();
28
29    /// Chain configurations exported from the registry
30    pub static ref CHAINS: ChainList = _INIT.chain_list.clone();
31
32    /// OP Chain configurations exported from the registry
33    pub static ref OPCHAINS: HashMap<u64, ChainConfig, DefaultHashBuilder> = _INIT.op_chains.clone();
34
35    /// Rollup configurations exported from the registry
36    pub static ref ROLLUP_CONFIGS: HashMap<u64, RollupConfig, DefaultHashBuilder> = _INIT.rollup_configs.clone();
37}
38
39/// Returns a [RollupConfig] by its identifier.
40pub fn rollup_config_by_ident(ident: &str) -> Option<&RollupConfig> {
41    let chain_id = CHAINS.get_chain_by_ident(ident)?.chain_id;
42    ROLLUP_CONFIGS.get(&chain_id)
43}
44
45/// Returns a [RollupConfig] by its identifier.
46pub fn rollup_config_by_alloy_ident(chain: &alloy_chains::Chain) -> Option<&RollupConfig> {
47    ROLLUP_CONFIGS.get(&chain.id())
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use alloy_chains::Chain as AlloyChain;
54
55    #[test]
56    fn test_hardcoded_rollup_configs() {
57        let test_cases = [
58            (10, test_utils::OP_MAINNET_CONFIG),
59            (8453, test_utils::BASE_MAINNET_CONFIG),
60            (11155420, test_utils::OP_SEPOLIA_CONFIG),
61            (84532, test_utils::BASE_SEPOLIA_CONFIG),
62        ]
63        .to_vec();
64
65        for (chain_id, expected) in test_cases {
66            let derived = super::ROLLUP_CONFIGS.get(&chain_id).unwrap();
67            assert_eq!(expected, *derived);
68        }
69    }
70
71    #[test]
72    fn test_chain_by_ident() {
73        const ALLOY_BASE: AlloyChain = AlloyChain::base_mainnet();
74
75        let chain_by_ident = CHAINS.get_chain_by_ident("mainnet/base").unwrap();
76        let chain_by_alloy_ident = CHAINS.get_chain_by_alloy_ident(&ALLOY_BASE).unwrap();
77        let chain_by_id = CHAINS.get_chain_by_id(8453).unwrap();
78
79        assert_eq!(chain_by_ident, chain_by_id);
80        assert_eq!(chain_by_alloy_ident, chain_by_id);
81    }
82
83    #[test]
84    fn test_rollup_config_by_ident() {
85        const ALLOY_BASE: AlloyChain = AlloyChain::base_mainnet();
86
87        let rollup_config_by_ident = rollup_config_by_ident("mainnet/base").unwrap();
88        let rollup_config_by_alloy_ident = rollup_config_by_alloy_ident(&ALLOY_BASE).unwrap();
89        let rollup_config_by_id = ROLLUP_CONFIGS.get(&8453).unwrap();
90
91        assert_eq!(rollup_config_by_ident, rollup_config_by_id);
92        assert_eq!(rollup_config_by_alloy_ident, rollup_config_by_id);
93    }
94}