From cc766ffcaf3acb38638f1b553f9fbfe36bb01289 Mon Sep 17 00:00:00 2001 From: Shiyas Mohammed Date: Mon, 4 Aug 2025 09:42:09 +0530 Subject: [PATCH 1/4] fix(forge): improve error messages for etherscan verification failures --- crates/config/src/etherscan.rs | 8 ++++++-- crates/config/src/lib.rs | 13 ++++++++++--- crates/verify/src/verify.rs | 13 +++++++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/config/src/etherscan.rs b/crates/config/src/etherscan.rs index d8aee66eb2bc2..d3e3fe092fafb 100644 --- a/crates/config/src/etherscan.rs +++ b/crates/config/src/etherscan.rs @@ -53,7 +53,11 @@ pub enum EtherscanConfigError { #[error(transparent)] Unresolved(#[from] UnresolvedEnvVarError), - #[error("No known Etherscan API URL for config{0} with chain `{1}`. Please specify a `url`")] + #[error( + "No known Etherscan API URL for chain `{1}`. To fix this, please:\n + 1. Specify a `url` {0}\n + 2. Verify the chain `{1}` is correct" + )] UnknownChain(String, Chain), #[error("At least one of `url` or `chain` must be present{0}")] @@ -233,7 +237,7 @@ impl EtherscanConfig { }), (Some(chain), None) => ResolvedEtherscanConfig::create(key, chain, api_version) .ok_or_else(|| { - let msg = alias.map(|a| format!(" `{a}`")).unwrap_or_default(); + let msg = alias.map(|a| format!("for `{a}`")).unwrap_or_default(); EtherscanConfigError::UnknownChain(msg, chain) }), (None, Some(api_url)) => Ok(ResolvedEtherscanConfig { diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 9d265867a11d3..767e18813d121 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -1429,13 +1429,20 @@ impl Config { // etherscan fallback via API key if let Some(key) = self.etherscan_api_key.as_ref() { - return Ok(ResolvedEtherscanConfig::create( + match ResolvedEtherscanConfig::create( key, chain.or(self.chain).unwrap_or_default(), default_api_version, - )); + ) { + Some(config) => return Ok(Some(config)), + None => { + return Err(EtherscanConfigError::UnknownChain( + "".to_string(), + chain.unwrap_or_default(), + )); + } + } } - Ok(None) } diff --git a/crates/verify/src/verify.rs b/crates/verify/src/verify.rs index 4abb70ac4d9e0..e3d3853923edd 100644 --- a/crates/verify/src/verify.rs +++ b/crates/verify/src/verify.rs @@ -320,7 +320,11 @@ impl VerifyArgs { .unwrap_or_default(); if unique_versions.is_empty() { - eyre::bail!("No matching artifact found for {}", contract.name); + eyre::bail!( + "No matching artifact found for {}. This could be due to:\n\ + - Compiler version mismatch - the contract was compiled with a different Solidity version than what's being used for verification", + contract.name + ); } else if unique_versions.len() > 1 { warn!( "Ambiguous compiler versions found in cache: {}", @@ -372,7 +376,12 @@ impl VerifyArgs { .unwrap_or_default(); if profiles.is_empty() { - eyre::bail!("No matching artifact found for {}", contract.name); + eyre::bail!( + "No matching artifact found for {} with compiler version {}. This could be due to:\n\ + - Compiler version mismatch - the contract was compiled with a different Solidity version", + contract.name, + version + ); } else if profiles.len() > 1 { eyre::bail!( "Ambiguous compilation profiles found in cache: {}, please specify the profile through `--compilation-profile` flag", From 64fded12f09ee48044a041b93ee774430364c41f Mon Sep 17 00:00:00 2001 From: Shiyas Mohammed Date: Mon, 4 Aug 2025 10:11:25 +0530 Subject: [PATCH 2/4] fix: cargo clippy --- crates/config/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 767e18813d121..b478b080651c0 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -1437,7 +1437,7 @@ impl Config { Some(config) => return Ok(Some(config)), None => { return Err(EtherscanConfigError::UnknownChain( - "".to_string(), + String::new(), chain.unwrap_or_default(), )); } From 8cb4749c4e0cf14384ea4aafbe083ac5e3097626 Mon Sep 17 00:00:00 2001 From: Shiyas Mohammed Date: Mon, 4 Aug 2025 19:02:54 +0530 Subject: [PATCH 3/4] tests(forge): add tests for unknown chain id --- crates/config/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index b478b080651c0..2aa4cc7b61a41 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -5088,4 +5088,45 @@ mod tests { .unwrap(); assert_eq!(endpoint.url, "https://rpc.sophon.xyz"); } + + #[test] + fn test_get_etherscan_config_with_unknown_chain() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "foundry.toml", + r#" + [etherscan] + mainnet = { chain = 3658348, key = "api-key"} + "#, + )?; + let config = Config::load().unwrap(); + let unknown_chain = Chain::from_id(3658348); + let result = config.get_etherscan_config_with_chain(Some(unknown_chain)); + assert!(result.is_err()); + let error_msg = result.unwrap_err().to_string(); + assert!(error_msg.contains("No known Etherscan API URL for chain `3658348`")); + assert!(error_msg.contains("Specify a `url`")); + assert!(error_msg.contains("Verify the chain `3658348` is correct")); + + Ok(()) + }); + } + + #[test] + fn test_get_etherscan_config_with_existing_chain_and_url() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "foundry.toml", + r#" + [etherscan] + mainnet = { chain = 1, key = "api-key" } + "#, + )?; + let config = Config::load().unwrap(); + let unknown_chain = Chain::from_id(1); + let result = config.get_etherscan_config_with_chain(Some(unknown_chain)); + assert!(result.is_ok()); + Ok(()) + }); + } } From 43dd2a0732bc7dbe8dd0094f1a9669976fb4bb88 Mon Sep 17 00:00:00 2001 From: Shiyas Mohammed Date: Tue, 5 Aug 2025 08:38:38 +0530 Subject: [PATCH 4/4] fix: err formating for EtherscanConfigError --- crates/config/src/etherscan.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/config/src/etherscan.rs b/crates/config/src/etherscan.rs index d3e3fe092fafb..4c75ffc12cfad 100644 --- a/crates/config/src/etherscan.rs +++ b/crates/config/src/etherscan.rs @@ -54,8 +54,8 @@ pub enum EtherscanConfigError { Unresolved(#[from] UnresolvedEnvVarError), #[error( - "No known Etherscan API URL for chain `{1}`. To fix this, please:\n - 1. Specify a `url` {0}\n + "No known Etherscan API URL for chain `{1}`. To fix this, please:\n\ + 1. Specify a `url` {0}\n\ 2. Verify the chain `{1}` is correct" )] UnknownChain(String, Chain),