Skip to content

Commit d1ee666

Browse files
authored
fix: only consider eth_rpc_url if no alias (#3363)
1 parent 5342d1c commit d1ee666

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

config/src/lib.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,12 @@ 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().or(self.etherscan_api_key.as_ref())?;
742+
let maybe_alias = self.eth_rpc_url.as_ref().or(self.etherscan_api_key.as_ref())?;
743743
let mut endpoints = self.rpc_endpoints.clone().resolved();
744-
if let Some(alias) = endpoints.remove(eth_rpc_url) {
744+
if let Some(alias) = endpoints.remove(maybe_alias) {
745745
Some(alias.map(Cow::Owned))
746746
} else {
747-
Some(Ok(Cow::Borrowed(eth_rpc_url.as_str())))
747+
Some(Ok(Cow::Borrowed(self.eth_rpc_url.as_deref()?)))
748748
}
749749
}
750750

@@ -818,7 +818,9 @@ impl Config {
818818
// we treat the `etherscan_api_key` as actual API key
819819
// if no chain provided, we assume mainnet
820820
let chain = self.chain_id.unwrap_or_else(|| Mainnet.into());
821-
ResolvedEtherscanConfig::create(maybe_alias, chain).map(Ok)
821+
822+
let api_key = self.etherscan_api_key.as_ref()?;
823+
ResolvedEtherscanConfig::create(api_key, chain).map(Ok)
822824
}
823825

824826
/// Same as [`Self::get_etherscan_config()`] but optionally updates the config with the given
@@ -2859,6 +2861,26 @@ mod tests {
28592861
})
28602862
}
28612863

2864+
#[test]
2865+
fn test_resolve_rpc_url_if_etherscan_set() {
2866+
figment::Jail::expect_with(|jail| {
2867+
jail.create_file(
2868+
"foundry.toml",
2869+
r#"
2870+
[profile.default]
2871+
etherscan_api_key = "dummy"
2872+
[rpc_endpoints]
2873+
optimism = "https://example.com/"
2874+
"#,
2875+
)?;
2876+
2877+
let config = Config::load();
2878+
assert_eq!("http://localhost:8545", config.get_rpc_url_or_localhost_http().unwrap());
2879+
2880+
Ok(())
2881+
})
2882+
}
2883+
28622884
#[test]
28632885
fn test_resolve_rpc_url_alias() {
28642886
figment::Jail::expect_with(|jail| {

0 commit comments

Comments
 (0)