Skip to content

Commit

Permalink
fix(verify): improve etherscan section parsing (#4574)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Mar 17, 2023
1 parent 4872912 commit 97f070f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/src/cmd/forge/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl VerifyArgs {
let config = self.load_config_emit_warnings();
let chain = config.chain_id.unwrap_or_default();
self.etherscan.chain = Some(chain);
self.etherscan.key = config.get_etherscan_api_key(Some(chain));
self.etherscan.key = config.get_etherscan_config_with_chain(Some(chain))?.map(|c| c.key);

if self.show_standard_json_input {
let args =
Expand Down
74 changes: 70 additions & 4 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,11 @@ impl Config {
}

/// Same as [`Self::get_etherscan_config()`] but optionally updates the config with the given
/// `chain`
/// `chain`, and `etherscan_api_key`
///
/// If not matching alias was found, then this will try to find the first entry in the table
/// with a matching chain id
/// with a matching chain id. If an etherscan_api_key is already set it will take precedence
/// over the chain's entry in the table.
pub fn get_etherscan_config_with_chain(
&self,
chain: Option<impl Into<Chain>>,
Expand All @@ -890,10 +891,22 @@ impl Config {
if let Some(res) =
chain.and_then(|chain| self.etherscan.clone().resolved().find_chain(chain))
{
return res.map(Some)
match (res, self.etherscan_api_key.as_ref()) {
(Ok(mut config), Some(key)) => {
// we update the key, because if an etherscan_api_key is set, it should take
// precedence over the entry, since this is usually set via env var or CLI args.
config.key = key.clone();
return Ok(Some(config))
}
(Ok(config), None) => return Ok(Some(config)),
(Err(err), None) => return Err(err),
(Err(_), Some(_)) => {
// use the etherscan key as fallback
}
}
}

// fallback `etherscan_api_key` as actual key
// etherscan fallback via API key
if let Some(key) = self.etherscan_api_key.as_ref() {
let chain = chain.or(self.chain_id).unwrap_or_default();
return Ok(ResolvedEtherscanConfig::create(key, chain))
Expand Down Expand Up @@ -2844,6 +2857,59 @@ mod tests {
});
}

#[test]
fn test_resolve_etherscan_with_chain() {
figment::Jail::expect_with(|jail| {
let env_key = "__BSC_ETHERSCAN_API_KEY";
let env_value = "env value";
jail.create_file(
"foundry.toml",
r#"
[profile.default]
[etherscan]
bsc = { key = "${__BSC_ETHERSCAN_API_KEY}", url = "https://api.bscscan.com/api" }
"#,
)?;

let config = Config::load();
assert!(config.get_etherscan_config_with_chain(None::<u64>).unwrap().is_none());
assert!(config
.get_etherscan_config_with_chain(Some(ethers_core::types::Chain::BinanceSmartChain))
.is_err());

std::env::set_var(env_key, env_value);

assert_eq!(
config
.get_etherscan_config_with_chain(Some(
ethers_core::types::Chain::BinanceSmartChain
))
.unwrap()
.unwrap()
.key,
env_value
);

let mut with_key = config.clone();
with_key.etherscan_api_key = Some("via etherscan_api_key".to_string());

assert_eq!(
with_key
.get_etherscan_config_with_chain(Some(
ethers_core::types::Chain::BinanceSmartChain
))
.unwrap()
.unwrap()
.key,
"via etherscan_api_key"
);

std::env::remove_var(env_key);
Ok(())
});
}

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

0 comments on commit 97f070f

Please sign in to comment.