ethers_middleware/gas_oracle/
gas_now.rs

1use super::{GasCategory, GasOracle, GasOracleError, Result};
2use async_trait::async_trait;
3use ethers_core::types::U256;
4use reqwest::Client;
5use serde::Deserialize;
6use url::Url;
7
8const URL: &str = "https://beaconcha.in/api/v1/execution/gasnow";
9
10/// A client over HTTP for the [beaconcha.in GasNow](https://beaconcha.in/gasnow) gas tracker API
11/// that implements the `GasOracle` trait.
12#[derive(Clone, Debug)]
13#[must_use]
14pub struct GasNow {
15    client: Client,
16    url: Url,
17    gas_category: GasCategory,
18}
19
20#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
21pub struct Response {
22    pub code: u64,
23    pub data: ResponseData,
24}
25
26#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
27pub struct ResponseData {
28    pub rapid: u64,
29    pub fast: u64,
30    pub standard: u64,
31    pub slow: u64,
32    pub timestamp: u64,
33    #[serde(rename = "priceUSD")]
34    pub price_usd: f64,
35}
36
37impl Response {
38    #[inline]
39    pub fn gas_from_category(&self, gas_category: GasCategory) -> u64 {
40        self.data.gas_from_category(gas_category)
41    }
42}
43
44impl ResponseData {
45    fn gas_from_category(&self, gas_category: GasCategory) -> u64 {
46        match gas_category {
47            GasCategory::SafeLow => self.slow,
48            GasCategory::Standard => self.standard,
49            GasCategory::Fast => self.fast,
50            GasCategory::Fastest => self.rapid,
51        }
52    }
53}
54
55impl Default for GasNow {
56    fn default() -> Self {
57        Self::new()
58    }
59}
60
61#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
62#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
63impl GasOracle for GasNow {
64    async fn fetch(&self) -> Result<U256> {
65        let res = self.query().await?;
66        let gas_price = res.gas_from_category(self.gas_category);
67        Ok(U256::from(gas_price))
68    }
69
70    async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> {
71        Err(GasOracleError::Eip1559EstimationNotSupported)
72    }
73}
74
75impl GasNow {
76    /// Creates a new [beaconcha.in GasNow](https://beaconcha.in/gasnow) gas price oracle.
77    pub fn new() -> Self {
78        Self::with_client(Client::new())
79    }
80
81    /// Same as [`Self::new`] but with a custom [`Client`].
82    pub fn with_client(client: Client) -> Self {
83        let url = Url::parse(URL).unwrap();
84        Self { client, url, gas_category: GasCategory::Standard }
85    }
86
87    /// Sets the gas price category to be used when fetching the gas price.
88    pub fn category(mut self, gas_category: GasCategory) -> Self {
89        self.gas_category = gas_category;
90        self
91    }
92
93    /// Perform a request to the gas price API and deserialize the response.
94    pub async fn query(&self) -> Result<Response> {
95        let response =
96            self.client.get(self.url.as_ref()).send().await?.error_for_status()?.json().await?;
97        Ok(response)
98    }
99}