ethers-rs/ethers-etherscan/src/errors.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
16 KiB
Rust
Raw Normal View History

use ethers_core::types::{Address, Chain};
use std::env::VarError;
#[derive(Debug, thiserror::Error)]
pub enum EtherscanError {
#[error("Chain {0} not supported")]
ChainNotSupported(Chain),
#[error("Contract execution call failed: {0}")]
ExecutionFailed(String),
#[error("Balance failed")]
BalanceFailed,
#[error("Transaction receipt failed")]
TransactionReceiptFailed,
#[error("Gas estimation failed")]
GasEstimationFailed,
#[error("Bad status code: {0}")]
BadStatusCode(String),
#[error(transparent)]
EnvVarNotFound(#[from] VarError),
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error("Contract source code not verified: {0}")]
ContractCodeNotVerified(Address),
#[error("Rate limit exceeded")]
RateLimitExceeded,
#[error(transparent)]
IO(#[from] std::io::Error),
#[error("Local networks (e.g. anvil, ganache, geth --dev) cannot be indexed by etherscan")]
LocalNetworksNotSupported,
#[error("Received error response: status={status},message={message}, result={result:?}")]
ErrorResponse { status: String, message: String, result: Option<String> },
#[error("Unknown error: {0}")]
Unknown(String),
#[error("Missing field: {0}")]
Builder(String),
#[error("Missing solc version: {0}")]
MissingSolcVersion(String),
#[error("Invalid API Key")]
InvalidApiKey,
#[error("Sorry, you have been blocked by Cloudflare, See also https://community.cloudflare.com/t/sorry-you-have-been-blocked/110790")]
BlockedByCloudflare,
#[error("The Requested prompted a cloudflare captcha security challenge to review the security of your connection before proceeding.")]
CloudFlareSecurityChallenge,
#[error("Received `Page not found` response. API server is likely down")]
PageNotFound,
}
/// etherscan/polyscan is protected by cloudflare, which can lead to html responses like `Sorry, you have been blocked` See also <https://community.cloudflare.com/t/sorry-you-have-been-blocked/110790>
///
/// This returns true if the `txt` is a cloudflare error response
pub(crate) fn is_blocked_by_cloudflare_response(txt: &str) -> bool {
txt.to_lowercase().contains("sorry, you have been blocked")
}
/// etherscan/polyscan is protected by cloudflare, which can require captchas to "review the
/// security of your connection before proceeding"
pub(crate) fn is_cloudflare_security_challenge(txt: &str) -> bool {
txt.contains("https://www.cloudflare.com?utm_source=challenge") ||
txt.to_lowercase().contains("checking if the site connection is secure")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_cloudflare_security_challenge() {
let res = "<!DOCTYPE html><html lang=\"en-US\"><head> <title>Just a moment...</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"> <meta name=\"robots\" content=\"noindex,nofollow\"> <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"> <link href=\"/cdn-cgi/styles/challenges.css\" rel=\"stylesheet\"> </head><body class=\"no-js\"> <div class=\"main-wrapper\" role=\"main\"> <div class=\"main-content\"> <h1 class=\"zone-name-title h1\"> <img class=\"heading-favicon\" src=\"/favicon.ico\" alt=\"Icon for api-goerli.etherscan.io\" onerror=\"this.onerror=null;this.parentNode.removeChild(this)\"> api-goerli.etherscan.io </h1> <h2 class=\"h2\" id=\"challenge-running\"> Checking if the site connection is secure </h2> <noscript> <div id=\"challenge-error-title\"> <div class=\"h2\"> <span class=\"icon-wrapper\"> <div class=\"heading-icon warning-icon\"></div> </span> <span id=\"challenge-error-text\"> Enable JavaScript and cookies to continue </span> </div> </div> </noscript> <div id=\"trk_jschal_js\" style=\"display:none;background-image:url('/cdn-cgi/images/trace/captcha/nojs/transparent.gif?ray=794294b0ff122cc8')\"></div> <div id=\"challenge-body-text\" class=\"core-msg spacer\"> api-goerli.etherscan.io needs to review the security of your connection before proceeding. </div> <form id=\"challenge-form\" action=\"/api/?__cf_chl_f_tk=3e8YnNWEoJpt7yhj9ZB_z7nPP6BpgWkrEO9fxS_FHfs-1675505052-0-gaNycGzNCv0\" method=\"POST\" enctype=\"application/x-www-form-urlencoded\"> <input type=\"hidden\" name=\"md\" value=\"kphchgOl8CtVKUgr8zjngomaIt8zm7QzUkLvD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xM3fMk7vk/i0LUDxsgZdPn6hndGPpEk06ilQ8ZGyNoqOOqbFa3/u1jTDmQ60B37Qp58bItB46XScO9DuVbtyEJ1VsWHyN1Q+hJE2TLaFuYmXkJMxDpP6dmqQ+GlXuIueRC/hompkJpDnzfhMg22G3X4EFw+I8vvMeHGcQvvSL1DcAr1V0g4egwpaZgtUQEnHT6V3/Os7j6OGdlioDLnIeowBM4M2mPEQ+ikcz9unZ47025a3VFJy4SuPgJpaAqCntlqedrDwKGXEFJFnsJmj/lTwb38rgGX2irzSxcOXNC4MWlDkwae3KJz/rUhdgDQknTkw6xvNmPhkCIwgIcagEAgNzQfvih72/cXQPiAAauB7+p/vS5VH6052cbNgvYZ74C2BEkPvw6qj3ASWorsiT8/BJfn/CcArpL0AYP/jL6QbubPqFLfO0Zs9e55lT0cMkko2+jT66HWgIUashrjW1dMxoCAZ97TSVRm4tZ/pCWEQym6tSr6EnkUA7GdbfAwCnxIvD5DH/0J81WJjMkW6C6+NC5GFC3iWoN48MAHXTtvpm7d3g6aQpGydNiP9QVckwCkoF0eFbSV8OE6oZsbJfKo0TzYi2Eg2gEZJYmZOqDHI2E3KdODxdd3vUeL73bZkyNqsDK/u9AC8kiqHna4oaGZ9ABeym6sGqHl9cwTsf5h2EUexMklhGu0QChVyOXhPTv0I3O1+/YVkrWX2wX40XC+KqCqc00lNyzf3zEdgM65WMC3OTrljansYl81eUXfoNpcPL/K+37kFG+f8UQxIiZu+q1/qMPgGA4bpQTe0bki3IFSkV/nKwCjfzy0j4b8YWhGyCY7yuaSTsLViVeYrRK6pOl9WNhEmsYqdeoKmp/SA7G2WsPCkth/OUYxmzjYKDikWzXv3sp/0w+kjh4vmPqTgoY5rK/cfueHuy9F2ojOxETD44yGn9ulogLIL
assert!(is_cloudflare_security_challenge(res));
}
#[test]
fn test_cloudflare_response() {
let resp = "<!DOCTYPE html>\n<!--[if lt IE 7]> <html class=\"no-js ie6 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if IE 7]> <html class=\"no-js ie7 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if IE 8]> <html class=\"no-js ie8 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class=\"no-js\" lang=\"en-US\"> <!--<![endif]-->\n<head>\n<title>Attention Required! | Cloudflare</title>\n<meta charset=\"UTF-8\" />\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\" />\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />\n<link rel=\"stylesheet\" id=\"cf_styles-css\" href=\"/cdn-cgi/styles/cf.errors.css\" />\n<!--[if lt IE 9]><link rel=\"stylesheet\" id='cf_styles-ie-css' href=\"/cdn-cgi/styles/cf.errors.ie.css\" /><![endif]-->\n<style>body{margin:0;padding:0}</style>\n\n\n<!--[if gte IE 10]><!-->\n<script>\n if (!navigator.cookieEnabled) {\n window.addEventListener('DOMContentLoaded', function () {\n var cookieEl = document.getElementById('cookie-alert');\n cookieEl.style.display = 'block';\n })\n }\n</script>\n<!--<![endif]-->\n\n\n</head>\n<body>\n <div id=\"cf-wrapper\">\n <div class=\"cf-alert cf-alert-error cf-cookie-error\" id=\"cookie-alert\" data-translate=\"enable_cookies\">Please enable cookies.</div>\n <div id=\"cf-error-details\" class=\"cf-error-details-wrapper\">\n <div class=\"cf-wrapper cf-header cf-error-overview\">\n <h1 data-translate=\"block_headline\">Sorry, you have been blocked</h1>\n <h2 class=\"cf-subheadline\"><span data-translate=\"unable_to_access\">You are unable to access</span> polygonscan.com</h2>\n </div><!-- /.header -->\n\n <div class=\"cf-section cf-highlight\">\n <div class=\"cf-wrapper\">\n <div class=\"cf-screenshot-container cf-screenshot-full\">\n \n <span class=\"cf-no-screenshot error\"></span>\n \n </div>\n </div>\n </div><!-- /.captcha-container -->\n\n <div class=\"cf-section cf-wrapper\">\n <div class=\"cf-columns two\">\n <div class=\"cf-column\">\n <h2 data-translate=\"blocked_why_headline\">Why have I been blocked?</h2>\n\n <p data-translate=\"blocked_why_detail\">This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.</p>\n </div>\n\n <div class=\"cf-column\">\n <h2 data-translate=\"blocked_resolve_headline\">What can I do to resolve this?</h2>\n\n <p data-translate=\"blocked_resolve_detail\">You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.</p>\n </div>\n </div>\n </div><!-- /.section -->\n\n <div class=\"cf-error-footer cf-wrapper w-240 lg:w-full py-10 sm:py-4 sm:px-8 mx-auto text-center sm:text-left border-solid border-0 border-t border-gray-300\">\n <p class=\"text-13\">\n <span class=\"cf-footer-item sm:block sm:mb-1\">Cloudflare Ray ID: <strong class=\"font-semibold\">74d2aa5ed9e27367</strong></span>\n <span class=\"cf-footer-separator sm:hidden\">&bull;</span>\n <span id=\"cf-footer-item-ip\" class=\"cf-footer-item hidden sm:block sm:mb-1\">\n Your IP:\n <button type=\"button\" id=\"cf-footer-ip-reveal\" class=\"cf-footer-ip-reveal-btn\">Click to reveal</button>\n <span class=\"hidden\" id=\"cf-footer-ip\">62.96.232.178</span>\n <span class=\"cf-footer-separator sm:hidden\">&bull;</span>\n </span>\n <span class=\"cf-footer-item sm:block sm:mb-1\"><span>Performance &amp; security by</span> <a rel=\"noopener noreferrer\" href=\"https://www.cloudflare.com/5xx-error-landing\"
assert!(is_blocked_by_cloudflare_response(resp));
}
}