2021-01-22 09:25:22 +00:00
|
|
|
use super::{Transformer, TransformerError};
|
|
|
|
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, PendingTransaction};
|
2021-01-22 09:25:22 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// Middleware used for intercepting transaction requests and transforming them to be executed by
|
|
|
|
/// the underneath `Transformer` instance.
|
|
|
|
pub struct TransformerMiddleware<M, T> {
|
|
|
|
inner: M,
|
|
|
|
transformer: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M, T> TransformerMiddleware<M, T>
|
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
T: Transformer,
|
|
|
|
{
|
|
|
|
/// Creates a new TransformerMiddleware that intercepts transactions, modifying them to be sent
|
|
|
|
/// through the Transformer.
|
|
|
|
pub fn new(inner: M, transformer: T) -> Self {
|
|
|
|
Self { inner, transformer }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum TransformerMiddlewareError<M: Middleware> {
|
|
|
|
#[error(transparent)]
|
|
|
|
TransformerError(#[from] TransformerError),
|
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
MiddlewareError(M::Error),
|
|
|
|
}
|
|
|
|
|
2023-02-20 23:55:36 +00:00
|
|
|
impl<M: Middleware> MiddlewareError for TransformerMiddlewareError<M> {
|
|
|
|
type Inner = M::Error;
|
|
|
|
|
|
|
|
fn from_err(src: M::Error) -> Self {
|
2021-01-22 09:25:22 +00:00
|
|
|
TransformerMiddlewareError::MiddlewareError(src)
|
|
|
|
}
|
2023-02-20 23:55:36 +00:00
|
|
|
|
|
|
|
fn as_inner(&self) -> Option<&Self::Inner> {
|
|
|
|
match self {
|
|
|
|
TransformerMiddlewareError::MiddlewareError(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 09:25:22 +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)]
|
2021-01-22 09:25:22 +00:00
|
|
|
impl<M, T> Middleware for TransformerMiddleware<M, T>
|
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
T: Transformer,
|
|
|
|
{
|
|
|
|
type Error = TransformerMiddlewareError<M>;
|
|
|
|
type Provider = M::Provider;
|
|
|
|
type Inner = M;
|
|
|
|
|
|
|
|
fn inner(&self) -> &M {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
async fn send_transaction<Tx: Into<TypedTransaction> + Send + Sync>(
|
2021-01-22 09:25:22 +00:00
|
|
|
&self,
|
2021-08-09 00:31:11 +00:00
|
|
|
tx: Tx,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2021-01-22 09:25:22 +00:00
|
|
|
) -> Result<PendingTransaction<'_, Self::Provider>, Self::Error> {
|
2021-08-09 00:31:11 +00:00
|
|
|
let mut tx = tx.into();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// construct the appropriate proxy tx.
|
2021-08-09 00:31:11 +00:00
|
|
|
self.transformer.transform(&mut tx)?;
|
2021-01-22 09:25:22 +00:00
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
self.fill_transaction(&mut tx, block).await?;
|
2021-01-22 09:25:22 +00:00
|
|
|
// send the proxy tx.
|
|
|
|
self.inner
|
2021-08-09 00:31:11 +00:00
|
|
|
.send_transaction(tx, block)
|
2021-01-22 09:25:22 +00:00
|
|
|
.await
|
|
|
|
.map_err(TransformerMiddlewareError::MiddlewareError)
|
|
|
|
}
|
|
|
|
}
|