Skip to content

Commit 0e7eca2

Browse files
fix(forge): improve error messages for etherscan verification failures (#11194)
* fix(forge): improve error messages for etherscan verification failures * fix: cargo clippy * tests(forge): add tests for unknown chain id * fix: err formating for EtherscanConfigError --------- Co-authored-by: zerosnacks <[email protected]>
1 parent 4097459 commit 0e7eca2

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

crates/config/src/etherscan.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ pub enum EtherscanConfigError {
5353
#[error(transparent)]
5454
Unresolved(#[from] UnresolvedEnvVarError),
5555

56-
#[error("No known Etherscan API URL for config{0} with chain `{1}`. Please specify a `url`")]
56+
#[error(
57+
"No known Etherscan API URL for chain `{1}`. To fix this, please:\n\
58+
1. Specify a `url` {0}\n\
59+
2. Verify the chain `{1}` is correct"
60+
)]
5761
UnknownChain(String, Chain),
5862

5963
#[error("At least one of `url` or `chain` must be present{0}")]
@@ -233,7 +237,7 @@ impl EtherscanConfig {
233237
}),
234238
(Some(chain), None) => ResolvedEtherscanConfig::create(key, chain, api_version)
235239
.ok_or_else(|| {
236-
let msg = alias.map(|a| format!(" `{a}`")).unwrap_or_default();
240+
let msg = alias.map(|a| format!("for `{a}`")).unwrap_or_default();
237241
EtherscanConfigError::UnknownChain(msg, chain)
238242
}),
239243
(None, Some(api_url)) => Ok(ResolvedEtherscanConfig {

crates/config/src/lib.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,13 +1429,20 @@ impl Config {
14291429

14301430
// etherscan fallback via API key
14311431
if let Some(key) = self.etherscan_api_key.as_ref() {
1432-
return Ok(ResolvedEtherscanConfig::create(
1432+
match ResolvedEtherscanConfig::create(
14331433
key,
14341434
chain.or(self.chain).unwrap_or_default(),
14351435
default_api_version,
1436-
));
1436+
) {
1437+
Some(config) => return Ok(Some(config)),
1438+
None => {
1439+
return Err(EtherscanConfigError::UnknownChain(
1440+
String::new(),
1441+
chain.unwrap_or_default(),
1442+
));
1443+
}
1444+
}
14371445
}
1438-
14391446
Ok(None)
14401447
}
14411448

@@ -5081,4 +5088,45 @@ mod tests {
50815088
.unwrap();
50825089
assert_eq!(endpoint.url, "https://rpc.sophon.xyz");
50835090
}
5091+
5092+
#[test]
5093+
fn test_get_etherscan_config_with_unknown_chain() {
5094+
figment::Jail::expect_with(|jail| {
5095+
jail.create_file(
5096+
"foundry.toml",
5097+
r#"
5098+
[etherscan]
5099+
mainnet = { chain = 3658348, key = "api-key"}
5100+
"#,
5101+
)?;
5102+
let config = Config::load().unwrap();
5103+
let unknown_chain = Chain::from_id(3658348);
5104+
let result = config.get_etherscan_config_with_chain(Some(unknown_chain));
5105+
assert!(result.is_err());
5106+
let error_msg = result.unwrap_err().to_string();
5107+
assert!(error_msg.contains("No known Etherscan API URL for chain `3658348`"));
5108+
assert!(error_msg.contains("Specify a `url`"));
5109+
assert!(error_msg.contains("Verify the chain `3658348` is correct"));
5110+
5111+
Ok(())
5112+
});
5113+
}
5114+
5115+
#[test]
5116+
fn test_get_etherscan_config_with_existing_chain_and_url() {
5117+
figment::Jail::expect_with(|jail| {
5118+
jail.create_file(
5119+
"foundry.toml",
5120+
r#"
5121+
[etherscan]
5122+
mainnet = { chain = 1, key = "api-key" }
5123+
"#,
5124+
)?;
5125+
let config = Config::load().unwrap();
5126+
let unknown_chain = Chain::from_id(1);
5127+
let result = config.get_etherscan_config_with_chain(Some(unknown_chain));
5128+
assert!(result.is_ok());
5129+
Ok(())
5130+
});
5131+
}
50845132
}

crates/verify/src/verify.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ impl VerifyArgs {
320320
.unwrap_or_default();
321321

322322
if unique_versions.is_empty() {
323-
eyre::bail!("No matching artifact found for {}", contract.name);
323+
eyre::bail!(
324+
"No matching artifact found for {}. This could be due to:\n\
325+
- Compiler version mismatch - the contract was compiled with a different Solidity version than what's being used for verification",
326+
contract.name
327+
);
324328
} else if unique_versions.len() > 1 {
325329
warn!(
326330
"Ambiguous compiler versions found in cache: {}",
@@ -372,7 +376,12 @@ impl VerifyArgs {
372376
.unwrap_or_default();
373377

374378
if profiles.is_empty() {
375-
eyre::bail!("No matching artifact found for {}", contract.name);
379+
eyre::bail!(
380+
"No matching artifact found for {} with compiler version {}. This could be due to:\n\
381+
- Compiler version mismatch - the contract was compiled with a different Solidity version",
382+
contract.name,
383+
version
384+
);
376385
} else if profiles.len() > 1 {
377386
eyre::bail!(
378387
"Ambiguous compilation profiles found in cache: {}, please specify the profile through `--compilation-profile` flag",

0 commit comments

Comments
 (0)