@@ -739,7 +739,7 @@ impl Config {
739739 /// # }
740740 /// ```
741741 pub fn get_rpc_url ( & self ) -> Option < Result < Cow < str > , UnresolvedEnvVarError > > {
742- let eth_rpc_url = self . eth_rpc_url . as_ref ( ) ?;
742+ let eth_rpc_url = self . eth_rpc_url . as_ref ( ) . or ( self . etherscan_api_key . as_ref ( ) ) ?;
743743 let mut endpoints = self . rpc_endpoints . clone ( ) . resolved ( ) ;
744744 if let Some ( alias) = endpoints. remove ( eth_rpc_url) {
745745 Some ( alias. map ( Cow :: Owned ) )
@@ -808,7 +808,7 @@ impl Config {
808808 pub fn get_etherscan_config (
809809 & self ,
810810 ) -> Option < Result < ResolvedEtherscanConfig , EtherscanConfigError > > {
811- let maybe_alias = self . etherscan_api_key . as_ref ( ) ?;
811+ let maybe_alias = self . etherscan_api_key . as_ref ( ) . or ( self . eth_rpc_url . as_ref ( ) ) ?;
812812 if self . etherscan . contains_key ( maybe_alias) {
813813 // etherscan points to an alias in the `etherscan` table, so we try to resolve
814814 // that
@@ -831,7 +831,7 @@ impl Config {
831831 chain : Option < impl Into < Chain > > ,
832832 ) -> Result < Option < ResolvedEtherscanConfig > , EtherscanConfigError > {
833833 let chain = chain. map ( Into :: into) ;
834- if let Some ( maybe_alias) = self . etherscan_api_key . as_ref ( ) {
834+ if let Some ( maybe_alias) = self . etherscan_api_key . as_ref ( ) . or ( self . eth_rpc_url . as_ref ( ) ) {
835835 if self . etherscan . contains_key ( maybe_alias) {
836836 let mut resolved = self . etherscan . clone ( ) . resolved ( ) ;
837837 return resolved. remove ( maybe_alias) . transpose ( )
@@ -2998,6 +2998,35 @@ mod tests {
29982998 } ) ;
29992999 }
30003000
3001+ #[ test]
3002+ fn test_extract_etherscan_config_by_chain_and_alias ( ) {
3003+ figment:: Jail :: expect_with ( |jail| {
3004+ jail. create_file (
3005+ "foundry.toml" ,
3006+ r#"
3007+ [profile.default]
3008+ eth_rpc_url = "mumbai"
3009+
3010+ [etherscan]
3011+ mumbai = { key = "https://etherscan-mumbai.com/" }
3012+
3013+ [rpc_endpoints]
3014+ mumbai = "https://polygon-mumbai.g.alchemy.com/v2/mumbai"
3015+ "# ,
3016+ ) ?;
3017+
3018+ let config = Config :: load ( ) ;
3019+
3020+ let mumbai =
3021+ config. get_etherscan_config_with_chain ( Option :: < u64 > :: None ) . unwrap ( ) . unwrap ( ) ;
3022+ assert_eq ! ( mumbai. key, "https://etherscan-mumbai.com/" . to_string( ) ) ;
3023+
3024+ let mumbai_rpc = config. get_rpc_url ( ) . unwrap ( ) . unwrap ( ) ;
3025+ assert_eq ! ( mumbai_rpc, "https://polygon-mumbai.g.alchemy.com/v2/mumbai" ) ;
3026+ Ok ( ( ) )
3027+ } ) ;
3028+ }
3029+
30013030 #[ test]
30023031 fn test_toml_file ( ) {
30033032 figment:: Jail :: expect_with ( |jail| {
0 commit comments