2020-08-18 18:47:56 +00:00
|
|
|
use ethers_core::types::U256;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use reqwest::Client;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_aux::prelude::*;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use crate::gas_oracle::{GasCategory, GasOracle, GasOracleError, GWEI_TO_WEI};
|
|
|
|
|
|
|
|
const ETHERSCAN_URL_PREFIX: &str =
|
|
|
|
"https://api.etherscan.io/api?module=gastracker&action=gasoracle";
|
|
|
|
|
|
|
|
/// A client over HTTP for the [Etherscan](https://api.etherscan.io/api?module=gastracker&action=gasoracle) gas tracker API
|
|
|
|
/// that implements the `GasOracle` trait
|
2021-08-19 08:38:12 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-08-18 18:47:56 +00:00
|
|
|
pub struct Etherscan {
|
|
|
|
client: Client,
|
|
|
|
url: Url,
|
|
|
|
gas_category: GasCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-08-19 08:38:12 +00:00
|
|
|
struct EtherscanResponseWrapper {
|
|
|
|
result: EtherscanResponse,
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 08:38:12 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd)]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
pub struct EtherscanResponse {
|
|
|
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
|
|
|
pub safe_gas_price: u64,
|
|
|
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
|
|
|
pub propose_gas_price: u64,
|
2020-08-18 18:47:56 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
2021-08-19 08:38:12 +00:00
|
|
|
pub fast_gas_price: u64,
|
2020-08-18 18:47:56 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
2021-08-19 08:38:12 +00:00
|
|
|
pub last_block: u64,
|
2020-09-17 11:06:56 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
2021-08-19 08:38:12 +00:00
|
|
|
#[serde(rename = "suggestBaseFee")]
|
|
|
|
pub suggested_base_fee: f64,
|
|
|
|
#[serde(deserialize_with = "deserialize_f64_vec")]
|
|
|
|
#[serde(rename = "gasUsedRatio")]
|
|
|
|
pub gas_used_ratio: Vec<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
use serde::de;
|
|
|
|
use std::str::FromStr;
|
|
|
|
fn deserialize_f64_vec<'de, D>(deserializer: D) -> Result<Vec<f64>, D::Error>
|
|
|
|
where
|
|
|
|
D: de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let str_sequence = String::deserialize(deserializer)?;
|
|
|
|
str_sequence
|
|
|
|
.split(',')
|
|
|
|
.map(|item| f64::from_str(item).map_err(|err| de::Error::custom(err.to_string())))
|
|
|
|
.collect()
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Etherscan {
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Creates a new [Etherscan](https://etherscan.io/gastracker) gas price oracle.
|
2020-09-17 11:06:56 +00:00
|
|
|
pub fn new(api_key: Option<&str>) -> Self {
|
2020-08-18 18:47:56 +00:00
|
|
|
let url = match api_key {
|
|
|
|
Some(key) => format!("{}&apikey={}", ETHERSCAN_URL_PREFIX, key),
|
|
|
|
None => ETHERSCAN_URL_PREFIX.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let url = Url::parse(&url).expect("invalid url");
|
|
|
|
|
|
|
|
Etherscan {
|
|
|
|
client: Client::new(),
|
|
|
|
url,
|
|
|
|
gas_category: GasCategory::Standard,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Sets the gas price category to be used when fetching the gas price.
|
2020-08-18 18:47:56 +00:00
|
|
|
pub fn category(mut self, gas_category: GasCategory) -> Self {
|
|
|
|
self.gas_category = gas_category;
|
|
|
|
self
|
|
|
|
}
|
2021-08-19 08:38:12 +00:00
|
|
|
|
|
|
|
pub async fn query(&self) -> Result<EtherscanResponse, GasOracleError> {
|
|
|
|
let res = self
|
|
|
|
.client
|
|
|
|
.get(self.url.as_ref())
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json::<EtherscanResponseWrapper>()
|
|
|
|
.await?;
|
|
|
|
Ok(res.result)
|
|
|
|
}
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl GasOracle for Etherscan {
|
|
|
|
async fn fetch(&self) -> Result<U256, GasOracleError> {
|
2020-09-17 11:06:56 +00:00
|
|
|
if matches!(self.gas_category, GasCategory::Fastest) {
|
2020-08-18 18:47:56 +00:00
|
|
|
return Err(GasOracleError::GasCategoryNotSupported);
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:38:12 +00:00
|
|
|
let res = self.query().await?;
|
2020-08-18 18:47:56 +00:00
|
|
|
|
|
|
|
match self.gas_category {
|
2021-08-19 08:38:12 +00:00
|
|
|
GasCategory::SafeLow => Ok(U256::from(res.safe_gas_price * GWEI_TO_WEI)),
|
|
|
|
GasCategory::Standard => Ok(U256::from(res.propose_gas_price * GWEI_TO_WEI)),
|
|
|
|
GasCategory::Fast => Ok(U256::from(res.fast_gas_price * GWEI_TO_WEI)),
|
2020-08-18 18:47:56 +00:00
|
|
|
_ => Err(GasOracleError::GasCategoryNotSupported),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|