Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f737563

Browse files
authoredSep 4, 2024··
Merge pull request #13 from anton-rs/rf/feat/superchain-type-cleanup
feat(primitives): Superchain Type Config Cleanup
2 parents d87e4e0 + 2fefe9e commit f737563

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed
 

‎crates/primitives/src/superchain.rs

+78-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@ pub struct Superchain {
2323
#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
2424
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2525
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
26+
#[cfg_attr(feature = "serde", serde(rename_all = "PascalCase"))]
2627
pub struct SuperchainConfig {
2728
/// Superchain name (e.g. "Mainnet")
28-
#[cfg_attr(feature = "serde", serde(rename = "Name"))]
2929
pub name: String,
3030
/// Superchain L1 anchor information
31-
#[cfg_attr(feature = "serde", serde(rename = "L1"))]
3231
pub l1: SuperchainL1Info,
3332
/// Optional addresses for the superchain-wide default protocol versions contract.
34-
#[cfg_attr(feature = "serde", serde(rename = "ProtocolVersionsAddr"))]
3533
pub protocol_versions_addr: Option<Address>,
3634
/// Optional address for the superchain-wide default superchain config contract.
37-
#[cfg_attr(feature = "serde", serde(rename = "SuperchainConfigAddr"))]
3835
pub superchain_config_addr: Option<Address>,
3936
/// Hardfork Configuration. These values may be overridden by individual chains.
4037
#[cfg_attr(feature = "serde", serde(flatten))]
@@ -45,6 +42,7 @@ pub struct SuperchainConfig {
4542
#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
4643
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4744
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
45+
#[cfg_attr(feature = "serde", serde(rename_all = "PascalCase"))]
4846
pub struct SuperchainL1Info {
4947
/// L1 chain ID
5048
#[cfg_attr(feature = "serde", serde(rename = "ChainID"))]
@@ -53,7 +51,6 @@ pub struct SuperchainL1Info {
5351
#[cfg_attr(feature = "serde", serde(rename = "PublicRPC"))]
5452
pub public_rpc: String,
5553
/// L1 chain explorer RPC endpoint
56-
#[cfg_attr(feature = "serde", serde(rename = "Explorer"))]
5754
pub explorer: String,
5855
}
5956

@@ -74,3 +71,79 @@ pub enum SuperchainLevel {
7471
#[default]
7572
Standard = 1,
7673
}
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
use alloy_primitives::address;
79+
80+
fn ref_config() -> SuperchainConfig {
81+
SuperchainConfig {
82+
name: "Mainnet".to_string(),
83+
l1: SuperchainL1Info {
84+
chain_id: 1,
85+
public_rpc: "https://ethereum-rpc.publicnode.com".to_string(),
86+
explorer: "https://etherscan.io".to_string(),
87+
},
88+
protocol_versions_addr: Some(address!("8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935")),
89+
superchain_config_addr: Some(address!("95703e0982140D16f8ebA6d158FccEde42f04a4C")),
90+
hardfork_defaults: HardForkConfiguration::default(),
91+
}
92+
}
93+
94+
#[test]
95+
fn test_superchain_l1_info_serde() {
96+
let l1_str = r#"{
97+
"ChainID": 1,
98+
"PublicRPC": "https://ethereum-rpc.publicnode.com",
99+
"Explorer": "https://etherscan.io"
100+
}"#;
101+
let l1: SuperchainL1Info = serde_json::from_str(l1_str).unwrap();
102+
assert_eq!(
103+
l1,
104+
SuperchainL1Info {
105+
chain_id: 1,
106+
public_rpc: "https://ethereum-rpc.publicnode.com".to_string(),
107+
explorer: "https://etherscan.io".to_string(),
108+
}
109+
);
110+
}
111+
112+
#[test]
113+
fn test_superchain_config_serde() {
114+
let cfg_str = r#"{
115+
"Name": "Mainnet",
116+
"L1": {
117+
"ChainID": 1,
118+
"PublicRPC": "https://ethereum-rpc.publicnode.com",
119+
"Explorer": "https://etherscan.io"
120+
},
121+
"ProtocolVersionsAddr": "0x8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935",
122+
"SuperchainConfigAddr": "0x95703e0982140D16f8ebA6d158FccEde42f04a4C"
123+
}"#;
124+
let cfg: SuperchainConfig = serde_json::from_str(cfg_str).unwrap();
125+
assert_eq!(cfg, ref_config());
126+
}
127+
128+
#[test]
129+
fn test_superchain_serde() {
130+
let superchain_str = r#"{
131+
"name": "Mainnet",
132+
"config": {
133+
"Name": "Mainnet",
134+
"L1": {
135+
"ChainID": 1,
136+
"PublicRPC": "https://ethereum-rpc.publicnode.com",
137+
"Explorer": "https://etherscan.io"
138+
},
139+
"ProtocolVersionsAddr": "0x8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935",
140+
"SuperchainConfigAddr": "0x95703e0982140D16f8ebA6d158FccEde42f04a4C"
141+
},
142+
"chains": []
143+
}"#;
144+
let superchain: Superchain = serde_json::from_str(superchain_str).unwrap();
145+
assert_eq!(superchain.name, "Mainnet");
146+
assert_eq!(superchain.config, ref_config());
147+
assert!(superchain.chains.is_empty());
148+
}
149+
}

0 commit comments

Comments
 (0)
This repository has been archived.