2020-09-24 21:33:09 +00:00
|
|
|
use super::{GasOracle, GasOracleError};
|
|
|
|
use async_trait::async_trait;
|
2021-08-09 00:31:11 +00:00
|
|
|
use ethers_core::types::{transaction::eip2718::TypedTransaction, *};
|
2023-02-20 23:55:36 +00:00
|
|
|
use ethers_providers::{Middleware, MiddlewareError as METrait, PendingTransaction};
|
2020-09-24 21:33:09 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-12-18 11:45:47 +00:00
|
|
|
/// Middleware used for fetching gas prices over an API instead of `eth_gasPrice`.
|
2020-09-24 21:33:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GasOracleMiddleware<M, G> {
|
|
|
|
inner: M,
|
|
|
|
gas_oracle: G,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M, G> GasOracleMiddleware<M, G>
|
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
G: GasOracle,
|
|
|
|
{
|
|
|
|
pub fn new(inner: M, gas_oracle: G) -> Self {
|
|
|
|
Self { inner, gas_oracle }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:45:47 +00:00
|
|
|
#[derive(Debug, Error)]
|
2020-09-24 21:33:09 +00:00
|
|
|
pub enum MiddlewareError<M: Middleware> {
|
|
|
|
#[error(transparent)]
|
|
|
|
GasOracleError(#[from] GasOracleError),
|
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
MiddlewareError(M::Error),
|
2021-08-09 00:31:11 +00:00
|
|
|
|
|
|
|
#[error("This gas price oracle only works with Legacy and EIP2930 transactions.")]
|
|
|
|
UnsupportedTxType,
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 23:55:36 +00:00
|
|
|
impl<M: Middleware> METrait for MiddlewareError<M> {
|
|
|
|
type Inner = M::Error;
|
|
|
|
|
|
|
|
fn from_err(src: M::Error) -> MiddlewareError<M> {
|
2020-09-24 21:33:09 +00:00
|
|
|
MiddlewareError::MiddlewareError(src)
|
|
|
|
}
|
2023-02-20 23:55:36 +00:00
|
|
|
|
|
|
|
fn as_inner(&self) -> Option<&Self::Inner> {
|
|
|
|
match self {
|
|
|
|
MiddlewareError::MiddlewareError(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 21:33:09 +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)]
|
2020-09-24 21:33:09 +00:00
|
|
|
impl<M, G> Middleware for GasOracleMiddleware<M, G>
|
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
G: GasOracle,
|
|
|
|
{
|
|
|
|
type Error = MiddlewareError<M>;
|
|
|
|
type Provider = M::Provider;
|
|
|
|
type Inner = M;
|
|
|
|
|
|
|
|
// OVERRIDEN METHODS
|
|
|
|
|
|
|
|
fn inner(&self) -> &M {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:04:08 +00:00
|
|
|
async fn fill_transaction(
|
2020-09-24 21:33:09 +00:00
|
|
|
&self,
|
2022-08-20 23:04:08 +00:00
|
|
|
tx: &mut TypedTransaction,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2022-08-20 23:04:08 +00:00
|
|
|
) -> Result<(), Self::Error> {
|
2021-08-09 00:31:11 +00:00
|
|
|
match tx {
|
|
|
|
TypedTransaction::Legacy(ref mut tx) => {
|
|
|
|
if tx.gas_price.is_none() {
|
|
|
|
tx.gas_price = Some(self.get_gas_price().await?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TypedTransaction::Eip2930(ref mut inner) => {
|
|
|
|
if inner.tx.gas_price.is_none() {
|
|
|
|
inner.tx.gas_price = Some(self.get_gas_price().await?);
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 14:01:40 +00:00
|
|
|
TypedTransaction::Eip1559(ref mut inner) => {
|
|
|
|
if inner.max_priority_fee_per_gas.is_none() || inner.max_fee_per_gas.is_none() {
|
|
|
|
let (max_fee_per_gas, max_priority_fee_per_gas) =
|
|
|
|
self.estimate_eip1559_fees(None).await?;
|
|
|
|
if inner.max_priority_fee_per_gas.is_none() {
|
|
|
|
inner.max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
|
|
|
|
}
|
|
|
|
if inner.max_fee_per_gas.is_none() {
|
|
|
|
inner.max_fee_per_gas = Some(max_fee_per_gas);
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 00:31:11 +00:00
|
|
|
}
|
|
|
|
};
|
2022-08-20 23:04:08 +00:00
|
|
|
|
2023-02-20 23:55:36 +00:00
|
|
|
self.inner().fill_transaction(tx, block).await.map_err(METrait::from_err)
|
2022-08-20 23:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_gas_price(&self) -> Result<U256, Self::Error> {
|
|
|
|
Ok(self.gas_oracle.fetch().await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn estimate_eip1559_fees(
|
|
|
|
&self,
|
|
|
|
_: Option<fn(U256, Vec<Vec<U256>>) -> (U256, U256)>,
|
|
|
|
) -> Result<(U256, U256), Self::Error> {
|
|
|
|
Ok(self.gas_oracle.estimate_eip1559_fees().await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
tx: T,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<PendingTransaction<'_, Self::Provider>, Self::Error> {
|
|
|
|
let mut tx = tx.into();
|
|
|
|
self.fill_transaction(&mut tx, block).await?;
|
2021-10-29 12:29:35 +00:00
|
|
|
self.inner.send_transaction(tx, block).await.map_err(MiddlewareError::MiddlewareError)
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
}
|