Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): prints error when accessing invalid etherscan config #9951

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/cast/bin/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ impl StorageArgs {
}
}

if !self.etherscan.has_key() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check does not match the later api_key assignment.
In my case, this check fails, but config.get_etherscan_api_key works.

eyre::bail!("You must provide an Etherscan API key if you're fetching a remote contract's storage.");
}

let chain = utils::get_chain(config.chain, &provider).await?;
let api_key = config.get_etherscan_api_key(Some(chain)).unwrap_or_default();
let api_key = config.get_etherscan_api_key(Some(chain)).or_else(|| self.etherscan.key()).ok_or_else(|| {
eyre::eyre!("You must provide an Etherscan API key if you're fetching a remote contract's storage.")
})?;
let client = Client::new(chain, api_key)?;
let source = find_source(client, address).await?;
let metadata = source.items.first().unwrap();
Expand Down
34 changes: 33 additions & 1 deletion crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,19 @@ impl Config {
/// Optionally updates the config with the given `chain`.
///
/// See also [Self::get_etherscan_config_with_chain]
#[expect(clippy::disallowed_macros)]
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)
self.get_etherscan_config_with_chain(chain)
.map_err(|e| {
// `sh_warn!` is a circular dependency, preventing us from using it here.
eprintln!(
"{}",
yansi::Paint::yellow(&format!("Error getting etherscan config: {}", e))
);
})
.ok()
.flatten()
.map(|c| c.key)
}

/// Returns the remapping for the project's _src_ directory
Expand Down Expand Up @@ -3137,6 +3148,27 @@ mod tests {
});
}

// any invalid entry invalidates whole [etherscan] sections
#[test]
fn test_resolve_etherscan_with_invalid_name() {
figment::Jail::expect_with(|jail| {
jail.create_file(
"foundry.toml",
r#"
[etherscan]
mainnet = { key = "FX42Z3BBJJEWXWGYV2X1CIPRSCN" }
an_invalid_name = { key = "FX42Z3BBJJEWXWGYV2X1CIPRSCN" }
"#,
)?;

let config = Config::load().unwrap();
let etherscan_config = config.get_etherscan_config();
assert!(etherscan_config.is_none());

Ok(())
});
}

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