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 url::Url;
|
|
|
|
|
|
|
|
use crate::gas_oracle::{GasCategory, GasOracle, GasOracleError, GWEI_TO_WEI};
|
|
|
|
|
|
|
|
const ETHERCHAIN_URL: &str = "https://www.etherchain.org/api/gasPriceOracle";
|
|
|
|
|
|
|
|
/// A client over HTTP for the [Etherchain](https://www.etherchain.org/api/gasPriceOracle) 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 Etherchain {
|
|
|
|
client: Client,
|
|
|
|
url: Url,
|
|
|
|
gas_category: GasCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Etherchain {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:38:12 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct EtherchainResponse {
|
|
|
|
pub safe_low: f32,
|
|
|
|
pub standard: f32,
|
|
|
|
pub fast: f32,
|
|
|
|
pub fastest: f32,
|
|
|
|
pub current_base_fee: f32,
|
|
|
|
pub recommended_base_fee: f32,
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Etherchain {
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Creates a new [Etherchain](https://etherchain.org/tools/gasPriceOracle) gas price oracle.
|
2020-08-18 18:47:56 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let url = Url::parse(ETHERCHAIN_URL).expect("invalid url");
|
|
|
|
|
|
|
|
Etherchain {
|
|
|
|
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<EtherchainResponse, GasOracleError> {
|
|
|
|
Ok(self
|
2020-08-18 18:47:56 +00:00
|
|
|
.client
|
|
|
|
.get(self.url.as_ref())
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json::<EtherchainResponse>()
|
2021-08-19 08:38:12 +00:00
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 18:47:56 +00:00
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
2021-08-19 08:38:12 +00:00
|
|
|
impl GasOracle for Etherchain {
|
|
|
|
async fn fetch(&self) -> Result<U256, GasOracleError> {
|
|
|
|
let res = self.query().await?;
|
2020-08-18 18:47:56 +00:00
|
|
|
let gas_price = match self.gas_category {
|
|
|
|
GasCategory::SafeLow => U256::from((res.safe_low as u64) * GWEI_TO_WEI),
|
|
|
|
GasCategory::Standard => U256::from((res.standard as u64) * GWEI_TO_WEI),
|
|
|
|
GasCategory::Fast => U256::from((res.fast as u64) * GWEI_TO_WEI),
|
|
|
|
GasCategory::Fastest => U256::from((res.fastest as u64) * GWEI_TO_WEI),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(gas_price)
|
|
|
|
}
|
2021-08-19 14:01:40 +00:00
|
|
|
|
|
|
|
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256), GasOracleError> {
|
|
|
|
Err(GasOracleError::Eip1559EstimationNotSupported)
|
|
|
|
}
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|