chore(etherscan): Return error for unverified contracts (#1065)

Given contract source code is not useful in the case of unverified contracts
have the contract_source_code method return an error in the case if the item.abi
has the "Contract source code not verified" string.

Uncomment unit test ignore and run test:
> cargo test --lib ethers-etherscan -- get_error
This commit is contained in:
Curtis Spencer 2022-03-19 11:32:19 -07:00 committed by GitHub
parent 916e9a7334
commit a0d7995f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 1 deletions

View File

@ -310,6 +310,9 @@ impl Client {
let query = let query =
self.create_query("contract", "getsourcecode", HashMap::from([("address", address)])); self.create_query("contract", "getsourcecode", HashMap::from([("address", address)]));
let response: Response<Vec<Metadata>> = self.get_json(&query).await?; let response: Response<Vec<Metadata>> = self.get_json(&query).await?;
if response.result.iter().any(|item| item.abi == "Contract source code not verified") {
return Err(EtherscanError::ContractCodeNotVerified(address))
}
Ok(ContractMetadata { items: response.result }) Ok(ContractMetadata { items: response.result })
} }
} }
@ -323,7 +326,7 @@ mod tests {
use ethers_core::types::Chain; use ethers_core::types::Chain;
use ethers_solc::{Project, ProjectPathsConfig}; use ethers_solc::{Project, ProjectPathsConfig};
use crate::{contract::VerifyContract, tests::run_at_least_duration, Client}; use crate::{contract::VerifyContract, tests::run_at_least_duration, Client, EtherscanError};
#[tokio::test] #[tokio::test]
#[serial] #[serial]
@ -355,6 +358,27 @@ mod tests {
.await .await
} }
#[tokio::test]
#[serial]
#[ignore]
async fn can_get_error_on_unverified_contract() {
run_at_least_duration(Duration::from_millis(250), async {
let client = Client::new_from_env(Chain::Mainnet).unwrap();
let unverified_addr = "0xb5c31a0e22cae98ac08233e512bd627885aa24e5".parse().unwrap();
let result = client.contract_source_code(unverified_addr).await;
match result.err() {
Some(error) => match error {
EtherscanError::ContractCodeNotVerified(addr) => {
assert_eq!(addr, unverified_addr);
}
_ => panic!("Invalid EtherscanError type"),
},
None => panic!("Result should contain ContractCodeNotVerified error"),
}
})
.await
}
/// Query a contract that has a single string source entry instead of underlying JSON metadata. /// Query a contract that has a single string source entry instead of underlying JSON metadata.
#[tokio::test] #[tokio::test]
#[serial] #[serial]