1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::gas_oracle::{GasCategory, GasOracle, GasOracleError, GWEI_TO_WEI};
use async_trait::async_trait;
use ethers_core::types::U256;
use reqwest::{header::AUTHORIZATION, Client};
use serde::Deserialize;
use std::{collections::HashMap, convert::TryInto};
use url::Url;

const BLOCKNATIVE_GAS_PRICE_ENDPOINT: &str = "https://api.blocknative.com/gasprices/blockprices";

fn gas_category_to_confidence(gas_category: &GasCategory) -> u64 {
    match gas_category {
        GasCategory::SafeLow => 80,
        GasCategory::Standard => 90,
        GasCategory::Fast => 95,
        GasCategory::Fastest => 99,
    }
}

/// A client over HTTP for the [BlockNative](https://www.blocknative.com/gas-estimator) gas tracker API
/// that implements the `GasOracle` trait
#[derive(Clone, Debug)]
pub struct BlockNative {
    client: Client,
    url: Url,
    api_key: String,
    gas_category: GasCategory,
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct BlockNativeGasResponse {
    system: Option<String>,
    network: Option<String>,
    unit: Option<String>,
    max_price: Option<u64>,
    block_prices: Vec<BlockPrice>,
    estimated_base_fees: Vec<HashMap<String, Vec<BaseFeeEstimate>>>,
}

impl BlockNativeGasResponse {
    pub fn get_estimation_for(
        &self,
        gas_category: &GasCategory,
    ) -> Result<EstimatedPrice, GasOracleError> {
        let confidence = gas_category_to_confidence(gas_category);
        Ok(self
            .block_prices
            .first()
            .ok_or(GasOracleError::InvalidResponse)?
            .estimated_prices
            .iter()
            .find(|p| p.confidence == confidence)
            .ok_or(GasOracleError::GasCategoryNotSupported)?
            .clone())
    }
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct BlockPrice {
    block_number: u64,
    estimated_transaction_count: u64,
    base_fee_per_gas: f64,
    estimated_prices: Vec<EstimatedPrice>,
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EstimatedPrice {
    confidence: u64,
    price: u64,
    max_priority_fee_per_gas: f64,
    max_fee_per_gas: f64,
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct BaseFeeEstimate {
    confidence: u64,
    base_fee: f64,
}

impl BlockNative {
    /// Creates a new [BlockNative](https://www.blocknative.com/gas-estimator) gas oracle.
    pub fn new(api_key: String) -> Self {
        Self::with_client(Client::new(), api_key)
    }

    /// Same as [`Self::new`] but with a custom [`Client`].
    pub fn with_client(client: Client, api_key: String) -> Self {
        Self {
            client,
            api_key,
            url: BLOCKNATIVE_GAS_PRICE_ENDPOINT.try_into().unwrap(),
            gas_category: GasCategory::Standard,
        }
    }

    /// Sets the gas price category to be used when fetching the gas price.
    #[must_use]
    pub fn category(mut self, gas_category: GasCategory) -> Self {
        self.gas_category = gas_category;
        self
    }

    /// Perform request to Blocknative, decode response
    pub async fn request(&self) -> Result<BlockNativeGasResponse, GasOracleError> {
        self.client
            .get(self.url.as_ref())
            .header(AUTHORIZATION, &self.api_key)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await
            .map_err(GasOracleError::HttpClientError)
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl GasOracle for BlockNative {
    async fn fetch(&self) -> Result<U256, GasOracleError> {
        let prices = self.request().await?.get_estimation_for(&self.gas_category)?;
        Ok(U256::from(prices.price * 100_u64) * U256::from(GWEI_TO_WEI) / U256::from(100))
    }

    async fn estimate_eip1559_fees(&self) -> Result<(U256, U256), GasOracleError> {
        let prices = self.request().await?.get_estimation_for(&self.gas_category)?;
        let base_fee = U256::from((prices.max_fee_per_gas * 100.0) as u64) *
            U256::from(GWEI_TO_WEI) /
            U256::from(100);
        let prio_fee = U256::from((prices.max_priority_fee_per_gas * 100.0) as u64) *
            U256::from(GWEI_TO_WEI) /
            U256::from(100);
        Ok((base_fee, prio_fee))
    }
}