2022-12-18 11:45:47 +00:00
|
|
|
use super::{from_gwei_f64, GasCategory, GasOracle, GasOracleError, Result};
|
2022-05-06 15:16:43 +00:00
|
|
|
use async_trait::async_trait;
|
2022-12-18 11:45:47 +00:00
|
|
|
use ethers_core::types::{Chain, U256};
|
2022-05-06 15:16:43 +00:00
|
|
|
use reqwest::Client;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
|
|
|
|
2022-12-18 11:45:47 +00:00
|
|
|
const MAINNET_URL: &str = "https://gasstation-mainnet.matic.network/v2";
|
|
|
|
const MUMBAI_URL: &str = "https://gasstation-mumbai.matic.today/v2";
|
2022-05-06 15:16:43 +00:00
|
|
|
|
|
|
|
/// The [Polygon](https://docs.polygon.technology/docs/develop/tools/polygon-gas-station/) gas station API
|
2022-12-18 11:45:47 +00:00
|
|
|
/// Queries over HTTP and implements the `GasOracle` trait.
|
2022-05-06 15:16:43 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-12-18 11:45:47 +00:00
|
|
|
#[must_use]
|
2022-05-06 15:16:43 +00:00
|
|
|
pub struct Polygon {
|
|
|
|
client: Client,
|
|
|
|
url: Url,
|
|
|
|
gas_category: GasCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The response from the Polygon gas station API.
|
2022-12-18 11:45:47 +00:00
|
|
|
///
|
2022-05-06 15:16:43 +00:00
|
|
|
/// Gas prices are in Gwei.
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Response {
|
2022-12-18 11:45:47 +00:00
|
|
|
pub estimated_base_fee: f64,
|
|
|
|
pub safe_low: GasEstimate,
|
|
|
|
pub standard: GasEstimate,
|
|
|
|
pub fast: GasEstimate,
|
2022-05-06 15:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct GasEstimate {
|
2022-12-18 11:45:47 +00:00
|
|
|
pub max_priority_fee: f64,
|
|
|
|
pub max_fee: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Response {
|
|
|
|
#[inline]
|
|
|
|
pub fn estimate_from_category(&self, gas_category: GasCategory) -> GasEstimate {
|
|
|
|
match gas_category {
|
|
|
|
GasCategory::SafeLow => self.safe_low,
|
|
|
|
GasCategory::Standard => self.standard,
|
|
|
|
GasCategory::Fast => self.fast,
|
|
|
|
GasCategory::Fastest => self.fast,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Polygon {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(Chain::Polygon).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
|
|
|
impl GasOracle for Polygon {
|
|
|
|
async fn fetch(&self) -> Result<U256> {
|
|
|
|
let response = self.query().await?;
|
|
|
|
let base = response.estimated_base_fee;
|
|
|
|
let prio = response.estimate_from_category(self.gas_category).max_priority_fee;
|
|
|
|
let fee = base + prio;
|
|
|
|
Ok(from_gwei_f64(fee))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> {
|
|
|
|
let response = self.query().await?;
|
|
|
|
let estimate = response.estimate_from_category(self.gas_category);
|
|
|
|
let max = from_gwei_f64(estimate.max_fee);
|
|
|
|
let prio = from_gwei_f64(estimate.max_priority_fee);
|
|
|
|
Ok((max, prio))
|
|
|
|
}
|
2022-05-06 15:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Polygon {
|
2022-12-18 11:45:47 +00:00
|
|
|
pub fn new(chain: Chain) -> Result<Self> {
|
2022-05-06 15:16:43 +00:00
|
|
|
Self::with_client(Client::new(), chain)
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:45:47 +00:00
|
|
|
pub fn with_client(client: Client, chain: Chain) -> Result<Self> {
|
2022-05-06 15:16:43 +00:00
|
|
|
// TODO: Sniff chain from chain id.
|
|
|
|
let url = match chain {
|
2022-12-18 11:45:47 +00:00
|
|
|
Chain::Polygon => MAINNET_URL,
|
|
|
|
Chain::PolygonMumbai => MUMBAI_URL,
|
2022-05-06 15:16:43 +00:00
|
|
|
_ => return Err(GasOracleError::UnsupportedChain),
|
|
|
|
};
|
2022-12-18 11:45:47 +00:00
|
|
|
Ok(Self { client, url: Url::parse(url).unwrap(), gas_category: GasCategory::Standard })
|
2022-05-06 15:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the gas price category to be used when fetching the gas price.
|
|
|
|
pub fn category(mut self, gas_category: GasCategory) -> Self {
|
|
|
|
self.gas_category = gas_category;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:45:47 +00:00
|
|
|
/// Perform a request to the gas price API and deserialize the response.
|
|
|
|
pub async fn query(&self) -> Result<Response> {
|
|
|
|
let response =
|
|
|
|
self.client.get(self.url.clone()).send().await?.error_for_status()?.json().await?;
|
|
|
|
Ok(response)
|
2022-05-06 15:16:43 +00:00
|
|
|
}
|
|
|
|
}
|