2022-04-25 15:50:55 +00:00
|
|
|
mod blocknative;
|
|
|
|
pub use blocknative::BlockNative;
|
|
|
|
|
2020-08-18 18:47:56 +00:00
|
|
|
mod eth_gas_station;
|
|
|
|
pub use eth_gas_station::EthGasStation;
|
|
|
|
|
|
|
|
mod etherchain;
|
|
|
|
pub use etherchain::Etherchain;
|
|
|
|
|
|
|
|
mod etherscan;
|
|
|
|
pub use etherscan::Etherscan;
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
mod middleware;
|
|
|
|
pub use middleware::{GasOracleMiddleware, MiddlewareError};
|
|
|
|
|
2022-05-06 15:16:43 +00:00
|
|
|
mod median;
|
|
|
|
pub use median::Median;
|
|
|
|
|
|
|
|
mod cache;
|
|
|
|
pub use cache::Cache;
|
|
|
|
|
|
|
|
mod polygon;
|
|
|
|
pub use polygon::Polygon;
|
|
|
|
|
2022-08-20 23:04:08 +00:00
|
|
|
mod gas_now;
|
|
|
|
pub use gas_now::GasNow;
|
|
|
|
|
|
|
|
mod provider_oracle;
|
|
|
|
pub use provider_oracle::ProviderOracle;
|
|
|
|
|
2020-08-18 18:47:56 +00:00
|
|
|
use ethers_core::types::U256;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2022-08-20 23:04:08 +00:00
|
|
|
use auto_impl::auto_impl;
|
2020-08-18 18:47:56 +00:00
|
|
|
use reqwest::Error as ReqwestError;
|
2022-08-20 23:04:08 +00:00
|
|
|
use std::error::Error;
|
2020-08-18 18:47:56 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
const GWEI_TO_WEI: u64 = 1000000000;
|
|
|
|
|
|
|
|
/// Various gas price categories. Choose one of the available
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum GasCategory {
|
|
|
|
SafeLow,
|
|
|
|
Standard,
|
|
|
|
Fast,
|
|
|
|
Fastest,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
/// Error thrown when fetching data from the `GasOracle`
|
|
|
|
pub enum GasOracleError {
|
|
|
|
/// An internal error in the HTTP request made from the underlying
|
|
|
|
/// gas oracle
|
|
|
|
#[error(transparent)]
|
|
|
|
HttpClientError(#[from] ReqwestError),
|
|
|
|
|
2022-04-25 15:50:55 +00:00
|
|
|
/// An error decoding JSON response from gas oracle
|
|
|
|
#[error(transparent)]
|
|
|
|
SerdeJsonError(#[from] serde_json::Error),
|
|
|
|
|
|
|
|
/// An error with oracle response type
|
|
|
|
#[error("invalid oracle response")]
|
|
|
|
InvalidResponse,
|
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
/// An internal error in the Etherscan client request made from the underlying
|
|
|
|
/// gas oracle
|
|
|
|
#[error(transparent)]
|
|
|
|
EtherscanError(#[from] ethers_etherscan::errors::EtherscanError),
|
|
|
|
|
2020-08-18 18:47:56 +00:00
|
|
|
/// An internal error thrown when the required gas category is not
|
|
|
|
/// supported by the gas oracle API
|
|
|
|
#[error("gas category not supported")]
|
|
|
|
GasCategoryNotSupported,
|
2021-08-19 14:01:40 +00:00
|
|
|
|
|
|
|
#[error("EIP-1559 gas estimation not supported")]
|
|
|
|
Eip1559EstimationNotSupported,
|
2022-05-06 15:16:43 +00:00
|
|
|
|
|
|
|
#[error("None of the oracles returned a value")]
|
|
|
|
NoValues,
|
|
|
|
|
|
|
|
#[error("Chain is not supported by the oracle")]
|
|
|
|
UnsupportedChain,
|
2022-08-20 23:04:08 +00:00
|
|
|
|
|
|
|
/// Error thrown when the provider failed.
|
|
|
|
#[error("Chain is not supported by the oracle")]
|
|
|
|
ProviderError(#[from] Box<dyn Error + Send + Sync>),
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `GasOracle` is a trait that an underlying gas oracle needs to implement.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2021-08-28 21:06:29 +00:00
|
|
|
/// use ethers_middleware::{
|
2020-08-18 18:47:56 +00:00
|
|
|
/// gas_oracle::{EthGasStation, Etherscan, GasCategory, GasOracle},
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// let eth_gas_station_oracle = EthGasStation::new(Some("my-api-key"));
|
|
|
|
/// let etherscan_oracle = EthGasStation::new(None).category(GasCategory::SafeLow);
|
|
|
|
///
|
|
|
|
/// let data_1 = eth_gas_station_oracle.fetch().await?;
|
|
|
|
/// let data_2 = etherscan_oracle.fetch().await?;
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
2022-08-20 23:04:08 +00:00
|
|
|
#[auto_impl(&, Box, Arc)]
|
2020-08-18 18:47:56 +00:00
|
|
|
pub trait GasOracle: Send + Sync + std::fmt::Debug {
|
|
|
|
/// Makes an asynchronous HTTP query to the underlying `GasOracle`
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2021-08-28 21:06:29 +00:00
|
|
|
/// use ethers_middleware::{
|
2020-08-18 18:47:56 +00:00
|
|
|
/// gas_oracle::{Etherchain, GasCategory, GasOracle},
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// let etherchain_oracle = Etherchain::new().category(GasCategory::Fastest);
|
|
|
|
/// let data = etherchain_oracle.fetch().await?;
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
async fn fetch(&self) -> Result<U256, GasOracleError>;
|
2021-08-19 14:01:40 +00:00
|
|
|
|
|
|
|
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256), GasOracleError>;
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|