2021-10-04 19:05:11 +00:00
|
|
|
//! Bindings for [etherscan.io web api](https://docs.etherscan.io/)
|
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
use std::borrow::Cow;
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-10-04 19:05:11 +00:00
|
|
|
use reqwest::{header, Url};
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
2021-11-27 07:54:20 +00:00
|
|
|
|
|
|
|
use errors::EtherscanError;
|
|
|
|
use ethers_core::{abi::Address, types::Chain};
|
|
|
|
|
|
|
|
pub mod contract;
|
|
|
|
pub mod errors;
|
|
|
|
pub mod gas;
|
|
|
|
pub mod transaction;
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-12-02 00:54:16 +00:00
|
|
|
pub(crate) type Result<T> = std::result::Result<T, EtherscanError>;
|
2021-10-04 19:05:11 +00:00
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
/// The Etherscan.io API client.
|
2021-10-24 18:41:50 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-10-04 19:05:11 +00:00
|
|
|
pub struct Client {
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Client that executes HTTP requests
|
2021-10-04 19:05:11 +00:00
|
|
|
client: reqwest::Client,
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Etherscan API key
|
2021-10-04 19:05:11 +00:00
|
|
|
api_key: String,
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Etherscan API endpoint like https://api(-chain).etherscan.io/api
|
2021-10-04 19:05:11 +00:00
|
|
|
etherscan_api_url: Url,
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Etherscan base endpoint like https://etherscan.io
|
2021-10-04 19:05:11 +00:00
|
|
|
etherscan_url: Url,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Create a new client with the correct endpoints based on the chain and provided API key
|
2021-10-24 18:41:50 +00:00
|
|
|
pub fn new(chain: Chain, api_key: impl Into<String>) -> Result<Self> {
|
2021-10-04 19:05:11 +00:00
|
|
|
let (etherscan_api_url, etherscan_url) = match chain {
|
2021-10-29 12:29:35 +00:00
|
|
|
Chain::Mainnet => {
|
|
|
|
(Url::parse("https://api.etherscan.io/api"), Url::parse("https://etherscan.io"))
|
|
|
|
}
|
2021-10-24 18:41:50 +00:00
|
|
|
Chain::Ropsten | Chain::Kovan | Chain::Rinkeby | Chain::Goerli => {
|
|
|
|
let chain_name = chain.to_string().to_lowercase();
|
|
|
|
|
|
|
|
(
|
|
|
|
Url::parse(&format!("https://api-{}.etherscan.io/api", chain_name)),
|
|
|
|
Url::parse(&format!("https://{}.etherscan.io", chain_name)),
|
|
|
|
)
|
|
|
|
}
|
2021-11-22 09:02:28 +00:00
|
|
|
Chain::Polygon => (
|
|
|
|
Url::parse("https://api.polygonscan.com/api"),
|
|
|
|
Url::parse("https://polygonscan.com"),
|
|
|
|
),
|
|
|
|
Chain::PolygonMumbai => (
|
|
|
|
Url::parse("https://api-testnet.polygonscan.com/api"),
|
|
|
|
Url::parse("https://mumbai.polygonscan.com"),
|
|
|
|
),
|
|
|
|
Chain::Avalanche => {
|
|
|
|
(Url::parse("https://api.snowtrace.io/api"), Url::parse("https://snowtrace.io"))
|
|
|
|
}
|
|
|
|
Chain::AvalancheFuji => (
|
|
|
|
Url::parse("https://api-testnet.snowtrace.io/api"),
|
|
|
|
Url::parse("https://testnet.snowtrace.io"),
|
|
|
|
),
|
2021-12-25 16:19:50 +00:00
|
|
|
Chain::Optimism => (
|
|
|
|
Url::parse("https://api-optimistic.etherscan.io/api"),
|
|
|
|
Url::parse("https://optimistic.etherscan.io"),
|
|
|
|
),
|
|
|
|
Chain::OptimismKovan => (
|
|
|
|
Url::parse("https://api-kovan-optimistic.etherscan.io/api"),
|
|
|
|
Url::parse("https://kovan-optimistic.etherscan.io"),
|
|
|
|
),
|
2022-01-18 11:00:27 +00:00
|
|
|
Chain::Fantom => {
|
|
|
|
(Url::parse("https://api.ftmscan.com"), Url::parse("https://ftmscan.com"))
|
|
|
|
}
|
|
|
|
Chain::FantomTestnet => (
|
|
|
|
Url::parse("https://api-testnet.ftmscan.com"),
|
|
|
|
Url::parse("https://testnet.ftmscan.com"),
|
|
|
|
),
|
2022-01-27 14:50:11 +00:00
|
|
|
Chain::BinanceSmartChain => {
|
|
|
|
(Url::parse("https://api.bscscan.com/api"), Url::parse("https://bscscan.com"))
|
|
|
|
}
|
|
|
|
Chain::BinanceSmartChainTestnet => (
|
|
|
|
Url::parse("https://api-testnet.bscscan.com/api"),
|
|
|
|
Url::parse("https://testnet.bscscan.com"),
|
|
|
|
),
|
2022-02-05 14:36:21 +00:00
|
|
|
Chain::Arbitrum => {
|
|
|
|
(Url::parse("https://api.arbiscan.io/api"), Url::parse("https://arbiscan.io"))
|
|
|
|
}
|
|
|
|
Chain::ArbitrumTestnet => (
|
|
|
|
Url::parse("https://api-testnet.arbiscan.io/api"),
|
|
|
|
Url::parse("https://testnet.arbiscan.io"),
|
|
|
|
),
|
2021-10-24 18:41:50 +00:00
|
|
|
chain => return Err(EtherscanError::ChainNotSupported(chain)),
|
2021-10-04 19:05:11 +00:00
|
|
|
};
|
|
|
|
|
2021-10-24 18:41:50 +00:00
|
|
|
Ok(Self {
|
2021-10-04 19:05:11 +00:00
|
|
|
client: Default::default(),
|
|
|
|
api_key: api_key.into(),
|
|
|
|
etherscan_api_url: etherscan_api_url.expect("is valid http"),
|
|
|
|
etherscan_url: etherscan_url.expect("is valid http"),
|
2021-10-24 18:41:50 +00:00
|
|
|
})
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new client with the correct endpoints based on the chain and API key
|
|
|
|
/// from ETHERSCAN_API_KEY environment variable
|
|
|
|
pub fn new_from_env(chain: Chain) -> Result<Self> {
|
2021-11-22 09:02:28 +00:00
|
|
|
let api_key = match chain {
|
|
|
|
Chain::Avalanche | Chain::AvalancheFuji => std::env::var("SNOWTRACE_API_KEY")?,
|
|
|
|
Chain::Polygon | Chain::PolygonMumbai => std::env::var("POLYGONSCAN_API_KEY")?,
|
2022-01-28 07:06:35 +00:00
|
|
|
Chain::Mainnet |
|
|
|
|
Chain::Ropsten |
|
|
|
|
Chain::Kovan |
|
|
|
|
Chain::Rinkeby |
|
|
|
|
Chain::Goerli |
|
|
|
|
Chain::Optimism |
|
|
|
|
Chain::OptimismKovan |
|
|
|
|
Chain::Fantom |
|
|
|
|
Chain::FantomTestnet |
|
|
|
|
Chain::BinanceSmartChain |
|
2022-02-05 14:36:21 +00:00
|
|
|
Chain::BinanceSmartChainTestnet |
|
|
|
|
Chain::Arbitrum |
|
|
|
|
Chain::ArbitrumTestnet => std::env::var("ETHERSCAN_API_KEY")?,
|
2021-12-03 18:05:38 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
Chain::XDai | Chain::Sepolia => String::default(),
|
2021-12-03 18:05:38 +00:00
|
|
|
Chain::Moonbeam | Chain::MoonbeamDev | Chain::Moonriver => {
|
|
|
|
std::env::var("MOONSCAN_API_KEY")?
|
|
|
|
}
|
2021-11-22 09:02:28 +00:00
|
|
|
};
|
|
|
|
Self::new(chain, api_key)
|
2021-10-04 19:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn etherscan_api_url(&self) -> &Url {
|
|
|
|
&self.etherscan_api_url
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn etherscan_url(&self) -> &Url {
|
|
|
|
&self.etherscan_url
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Return the URL for the given block number
|
|
|
|
pub fn block_url(&self, block: u64) -> String {
|
|
|
|
format!("{}/block/{}", self.etherscan_url, block)
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:05:11 +00:00
|
|
|
/// Return the URL for the given address
|
|
|
|
pub fn address_url(&self, address: Address) -> String {
|
2021-10-17 10:01:20 +00:00
|
|
|
format!("{}/address/{}", self.etherscan_url, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the URL for the given transaction hash
|
|
|
|
pub fn transaction_url(&self, tx_hash: impl AsRef<str>) -> String {
|
|
|
|
format!("{}/tx/{}", self.etherscan_url, tx_hash.as_ref())
|
2021-10-04 19:05:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Return the URL for the given token hash
|
|
|
|
pub fn token_url(&self, token_hash: impl AsRef<str>) -> String {
|
|
|
|
format!("{}/token/{}", self.etherscan_url, token_hash.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute an API POST request with a form
|
2021-10-04 19:05:11 +00:00
|
|
|
async fn post_form<T: DeserializeOwned, Form: Serialize>(
|
|
|
|
&self,
|
|
|
|
form: &Form,
|
2021-10-17 10:01:20 +00:00
|
|
|
) -> Result<Response<T>> {
|
2021-10-04 19:05:11 +00:00
|
|
|
Ok(self
|
|
|
|
.client
|
|
|
|
.post(self.etherscan_api_url.clone())
|
|
|
|
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.form(form)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Execute an API GET request with parameters
|
|
|
|
async fn get_json<T: DeserializeOwned, Q: Serialize>(&self, query: &Q) -> Result<Response<T>> {
|
2021-10-04 19:05:11 +00:00
|
|
|
Ok(self
|
|
|
|
.client
|
|
|
|
.get(self.etherscan_api_url.clone())
|
|
|
|
.header(header::ACCEPT, "application/json")
|
|
|
|
.query(query)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_query<T: Serialize>(
|
|
|
|
&self,
|
|
|
|
module: &'static str,
|
|
|
|
action: &'static str,
|
|
|
|
other: T,
|
|
|
|
) -> Query<T> {
|
|
|
|
Query {
|
|
|
|
apikey: Cow::Borrowed(&self.api_key),
|
|
|
|
module: Cow::Borrowed(module),
|
|
|
|
action: Cow::Borrowed(action),
|
|
|
|
other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The API response type
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct Response<T> {
|
|
|
|
pub status: String,
|
|
|
|
pub message: String,
|
|
|
|
pub result: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The type that gets serialized as query
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct Query<'a, T: Serialize> {
|
|
|
|
apikey: Cow<'a, str>,
|
|
|
|
module: Cow<'a, str>,
|
|
|
|
action: Cow<'a, str>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
other: T,
|
|
|
|
}
|
2021-10-24 18:41:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-11-27 07:54:20 +00:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
time::{Duration, SystemTime},
|
|
|
|
};
|
|
|
|
|
2021-10-24 18:41:50 +00:00
|
|
|
use ethers_core::types::Chain;
|
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
use crate::{Client, EtherscanError};
|
|
|
|
|
2021-10-24 18:41:50 +00:00
|
|
|
#[test]
|
|
|
|
fn chain_not_supported() {
|
|
|
|
let err = Client::new_from_env(Chain::XDai).unwrap_err();
|
|
|
|
|
|
|
|
assert!(matches!(err, EtherscanError::ChainNotSupported(_)));
|
2022-02-12 15:41:18 +00:00
|
|
|
assert_eq!(err.to_string(), "chain xdai not supported");
|
2021-10-24 18:41:50 +00:00
|
|
|
}
|
2021-11-27 07:54:20 +00:00
|
|
|
|
|
|
|
pub async fn run_at_least_duration(duration: Duration, block: impl Future) {
|
|
|
|
let start = SystemTime::now();
|
|
|
|
block.await;
|
|
|
|
if let Some(sleep) = duration.checked_sub(start.elapsed().unwrap()) {
|
|
|
|
tokio::time::sleep(sleep).await;
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 18:41:50 +00:00
|
|
|
}
|