@@ -13,7 +13,7 @@ pub struct ForkConfigs(pub HashMap<String, ForkChainConfig>);
1313
1414impl ForkConfigs {
1515 /// Resolve environment variables in all fork config fields
16- pub fn resolve_env_vars ( & mut self ) -> Result < ( ) , ExtractConfigError > {
16+ fn resolve_env_vars ( & mut self ) -> Result < ( ) , ExtractConfigError > {
1717 for ( name, fork_config) in & mut self . 0 {
1818 // Take temporary ownership of the config, so that it can be consumed.
1919 let config = std:: mem:: take ( fork_config) ;
@@ -33,6 +33,47 @@ impl ForkConfigs {
3333
3434 Ok ( ( ) )
3535 }
36+
37+ /// Normalize fork config chains, so that all have `alloy_chain::NamedChain` compatible names.
38+ fn normalize_keys ( & mut self ) -> Result < ( ) , ExtractConfigError > {
39+ let mut normalized = HashMap :: new ( ) ;
40+
41+ for ( key, config) in std:: mem:: take ( & mut self . 0 ) {
42+ // Determine the canonical key for this entry
43+ let canonical_key = if let Ok ( chain_id) = key. parse :: < u64 > ( ) {
44+ if let Some ( named) = alloy_chains:: Chain :: from_id ( chain_id) . named ( ) {
45+ named. as_str ( ) . to_string ( )
46+ } else {
47+ return Err ( ExtractConfigError :: new ( figment:: Error :: from ( format ! (
48+ "chain id '{key}' is not supported by 'alloy_chains'." ,
49+ ) ) ) ) ;
50+ }
51+ } else if let Ok ( named) = key. parse :: < alloy_chains:: NamedChain > ( ) {
52+ named. as_str ( ) . to_string ( )
53+ } else {
54+ return Err ( ExtractConfigError :: new ( figment:: Error :: from ( format ! (
55+ "chain name '{key}' is not supported by 'alloy_chains'." ,
56+ ) ) ) ) ;
57+ } ;
58+
59+ // Insert and check for conflicts
60+ if normalized. insert ( canonical_key, config) . is_some ( ) {
61+ return Err ( ExtractConfigError :: new ( figment:: Error :: from (
62+ "duplicate fork configuration." ,
63+ ) ) ) ;
64+ }
65+ }
66+
67+ self . 0 = normalized;
68+ Ok ( ( ) )
69+ }
70+
71+ /// Normalize fork config chains and resolve environment variables in all configured fields.
72+ pub fn normalize_and_resolve ( & mut self ) -> Result < ( ) , ExtractConfigError > {
73+ self . resolve_env_vars ( ) ?;
74+ self . normalize_keys ( ) ?;
75+ Ok ( ( ) )
76+ }
3677}
3778
3879impl Deref for ForkConfigs {
0 commit comments