From 1af17deeb55435001184150ad0254acda59fd090 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 15 Aug 2022 19:30:30 +0200 Subject: [PATCH] feat(etherscan): add invalid api key error type (#1600) --- ethers-etherscan/src/errors.rs | 2 ++ ethers-etherscan/src/lib.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ethers-etherscan/src/errors.rs b/ethers-etherscan/src/errors.rs index 8a23bc7b..060b518f 100644 --- a/ethers-etherscan/src/errors.rs +++ b/ethers-etherscan/src/errors.rs @@ -35,4 +35,6 @@ pub enum EtherscanError { Builder(String), #[error("Missing solc version: {0}")] MissingSolcVersion(String), + #[error("Invalid API Key")] + InvalidApiKey, } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 8b89f05e..0d38f49f 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -185,6 +185,8 @@ impl Client { ResponseData::Error { result, .. } => { if result.starts_with("Max rate limit reached") { Err(EtherscanError::RateLimitExceeded) + } else if result.to_lowercase() == "invalid api key" { + Err(EtherscanError::InvalidApiKey) } else { Err(EtherscanError::Unknown(result)) } @@ -461,6 +463,17 @@ mod tests { assert!(matches!(err, EtherscanError::LocalNetworksNotSupported)); } + #[tokio::test] + async fn check_wrong_etherscan_api_key() { + let client = Client::new(Chain::Mainnet, "ABCDEFG").unwrap(); + let resp = client + .contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap()) + .await + .unwrap_err(); + + assert!(matches!(resp, EtherscanError::InvalidApiKey)); + } + pub async fn run_at_least_duration(duration: Duration, block: impl Future) { let start = SystemTime::now(); block.await;