Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(verify): make verification result an option to handle blockscou…
Browse files Browse the repository at this point in the history
…t's format (#2426)

* fix(): make result an option to handle nulls gracefully

* chore: fmt

* chore: clippy

* chore: error with EmptyResult instead of always letting serde error

* chore: include status/message on error
  • Loading branch information
Evalir authored May 23, 2023
1 parent d70cd87 commit 00d10f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 18 additions & 4 deletions ethers-etherscan/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,31 @@ impl Client {
}

let query = self.create_query("contract", "getabi", HashMap::from([("address", address)]));
let resp: Response<String> = self.get_json(&query).await?;
if resp.result.starts_with("Max rate limit reached") {
let resp: Response<Option<String>> = self.get_json(&query).await?;

let result = match resp.result {
Some(result) => result,
None => {
return Err(EtherscanError::EmptyResult {
message: resp.message,
status: resp.status,
})
}
};

if result.starts_with("Max rate limit reached") {
return Err(EtherscanError::RateLimitExceeded)
}
if resp.result.starts_with("Contract source code not verified") {

if result.starts_with("Contract source code not verified") ||
resp.message.starts_with("Contract source code not verified")
{
if let Some(ref cache) = self.cache {
cache.set_abi(address, None);
}
return Err(EtherscanError::ContractCodeNotVerified(address))
}
let abi = serde_json::from_str(&resp.result)?;
let abi = serde_json::from_str(&result)?;

if let Some(ref cache) = self.cache {
cache.set_abi(address, Some(&abi));
Expand Down
2 changes: 2 additions & 0 deletions ethers-etherscan/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum EtherscanError {
Serde(#[from] serde_json::Error),
#[error("Contract source code not verified: {0}")]
ContractCodeNotVerified(Address),
#[error("Response result is unexpectedly empty: status={status}, message={message}")]
EmptyResult { status: String, message: String },
#[error("Rate limit exceeded")]
RateLimitExceeded,
#[error(transparent)]
Expand Down

0 comments on commit 00d10f4

Please sign in to comment.