Skip to content

Commit

Permalink
chore: unify etherscan resolve functions (#7340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Mar 7, 2024
1 parent c3a1902 commit b253d84
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,8 @@ impl Config {
/// Returns
/// - the matching `ResolvedEtherscanConfig` of the `etherscan` table if `etherscan_api_key` is
/// an alias
/// - the matching `ResolvedEtherscanConfig` of the `etherscan` table if a `chain` is
/// configured. an alias
/// - the Mainnet `ResolvedEtherscanConfig` if `etherscan_api_key` is set, `None` otherwise
///
/// # Example
Expand All @@ -933,18 +935,7 @@ impl Config {
pub fn get_etherscan_config(
&self,
) -> Option<Result<ResolvedEtherscanConfig, EtherscanConfigError>> {
let maybe_alias = self.etherscan_api_key.as_ref().or(self.eth_rpc_url.as_ref())?;
if self.etherscan.contains_key(maybe_alias) {
// etherscan points to an alias in the `etherscan` table, so we try to resolve that
let mut resolved = self.etherscan.clone().resolved();
return resolved.remove(maybe_alias)
}

// we treat the `etherscan_api_key` as actual API key
// if no chain provided, we assume mainnet
let chain = self.chain.unwrap_or(Chain::mainnet());
let api_key = self.etherscan_api_key.as_ref()?;
ResolvedEtherscanConfig::create(api_key, chain).map(Ok)
self.get_etherscan_config_with_chain(None).transpose()
}

/// Same as [`Self::get_etherscan_config()`] but optionally updates the config with the given
Expand All @@ -964,8 +955,9 @@ impl Config {
}

// try to find by comparing chain IDs after resolving
if let Some(res) =
chain.and_then(|chain| self.etherscan.clone().resolved().find_chain(chain))
if let Some(res) = chain
.or(self.chain)
.and_then(|chain| self.etherscan.clone().resolved().find_chain(chain))
{
match (res, self.etherscan_api_key.as_ref()) {
(Ok(mut config), Some(key)) => {
Expand All @@ -992,6 +984,10 @@ impl Config {
}

/// Helper function to just get the API key
///
/// Optionally updates the config with the given `chain`.
///
/// See also [Self::get_etherscan_config_with_chain]
pub fn get_etherscan_api_key(&self, chain: Option<Chain>) -> Option<String> {
self.get_etherscan_config_with_chain(chain).ok().flatten().map(|c| c.key)
}
Expand Down Expand Up @@ -3120,6 +3116,29 @@ mod tests {
});
}

#[test]
fn test_resolve_etherscan_chain_id() {
figment::Jail::expect_with(|jail| {
jail.create_file(
"foundry.toml",
r#"
[profile.default]
chain_id = "sepolia"
[etherscan]
sepolia = { key = "FX42Z3BBJJEWXWGYV2X1CIPRSCN" }
"#,
)?;

let config = Config::load();
let etherscan = config.get_etherscan_config().unwrap().unwrap();
assert_eq!(etherscan.chain, Some(NamedChain::Sepolia.into()));
assert_eq!(etherscan.key, "FX42Z3BBJJEWXWGYV2X1CIPRSCN");

Ok(())
});
}

#[test]
fn test_resolve_rpc_url() {
figment::Jail::expect_with(|jail| {
Expand Down

0 comments on commit b253d84

Please sign in to comment.