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, *};
|
2020-12-17 11:26:01 +00:00
|
|
|
use ethers_providers::{FromErr, Middleware, PendingTransaction};
|
2020-09-24 21:33:09 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-10-08 15:56:36 +00:00
|
|
|
/// Middleware used for fetching gas prices over an API instead of `eth_gasPrice`
|
2020-09-24 21:33:09 +00:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl<M: Middleware> FromErr<M::Error> for MiddlewareError<M> {
|
|
|
|
fn from(src: M::Error) -> MiddlewareError<M> {
|
|
|
|
MiddlewareError::MiddlewareError(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:56:36 +00:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_gas_price(&self) -> Result<U256, Self::Error> {
|
|
|
|
Ok(self.gas_oracle.fetch().await?)
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>(
|
2020-09-24 21:33:09 +00:00
|
|
|
&self,
|
2021-08-09 00:31:11 +00:00
|
|
|
tx: T,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2020-12-17 11:26:01 +00:00
|
|
|
) -> Result<PendingTransaction<'_, Self::Provider>, Self::Error> {
|
2021-08-09 00:31:11 +00:00
|
|
|
let mut tx = tx.into();
|
|
|
|
|
|
|
|
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?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TypedTransaction::Eip1559(_) => {
|
|
|
|
return Err(MiddlewareError::UnsupportedTxType);
|
|
|
|
}
|
|
|
|
};
|
2020-09-24 21:33:09 +00:00
|
|
|
self.inner
|
|
|
|
.send_transaction(tx, block)
|
|
|
|
.await
|
|
|
|
.map_err(MiddlewareError::MiddlewareError)
|
|
|
|
}
|
|
|
|
}
|