middleware

This commit is contained in:
0xfirefist 2024-05-06 20:01:51 +05:30
parent ff6b11023c
commit e992aea9bc
5 changed files with 61 additions and 3 deletions

View File

@ -1514,6 +1514,7 @@ dependencies = [
"serde_with",
"serde_yaml",
"sha3",
"thiserror",
"tokio",
"tower-http",
"tracing",

View File

@ -34,6 +34,7 @@ utoipa-swagger-ui = { version = "3.1.4", features = ["axum"] }
once_cell = "1.18.0"
lazy_static = "1.4.0"
url = "2.5.0"
thiserror = "1.0.49"
[dev-dependencies]

View File

@ -1,2 +1,3 @@
pub(crate) mod ethereum;
pub(crate) mod reader;
mod client;

View File

@ -0,0 +1,54 @@
use std::future::Future;
use std::pin::Pin;
use axum::async_trait;
use ethabi::ethereum_types::H256;
use ethers::middleware::MiddlewareError;
use ethers::prelude::{Block, BlockId, Middleware, TxHash};
use thiserror::Error;
use ethers::core::types::U64;
#[derive(Debug)]
pub struct MyMiddleware<M>(pub M);
#[derive(Error, Debug)]
pub enum MyError<M: Middleware> {
#[error("{0}")]
MiddlewareError(M::Error),
}
impl<M: Middleware> MiddlewareError for MyError<M> {
type Inner = M::Error;
fn from_err(src: M::Error) -> Self {
MyError::MiddlewareError(src)
}
fn as_inner(&self) -> Option<&Self::Inner> {
match self {
MyError::MiddlewareError(e) => Some(e),
}
}
}
#[async_trait]
impl<M> Middleware for MyMiddleware<M>
where
M: Middleware,
{
type Error = MyError<M>;
type Provider = M::Provider;
type Inner = M;
fn inner(&self) -> &M {
&self.0
}
/// Gets the block at `block_hash_or_number` (transaction hashes only)
async fn get_block<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<Option<Block<TxHash>>, Self::Error> {
tracing::debug!("called");
self.inner().get_block(block_hash_or_number).await.map_err(MiddlewareError::from_err)
}
}

View File

@ -54,6 +54,7 @@ use {
},
std::sync::Arc,
};
use crate::chain::client::MyMiddleware;
// TODO: Programmatically generate this so we don't have to keep committed ABI in sync with the
// contract in the same repo.
@ -68,7 +69,7 @@ pub type SignablePythContract = PythRandom<
LegacyTxTransformer,
>,
>;
pub type PythContract = PythRandom<Provider<Http>>;
pub type PythContract = PythRandom<MyMiddleware<Provider<Http>>>;
/// Transformer that converts a transaction into a legacy transaction if use_legacy_tx is true.
#[derive(Clone, Debug)]
@ -185,7 +186,7 @@ impl SignablePythContract {
impl PythContract {
pub fn from_config(chain_config: &EthereumConfig) -> Result<PythContract> {
let provider = Provider::<Http>::try_from(&chain_config.geth_rpc_addr)?;
let provider = MyMiddleware(Provider::<Http>::try_from(&chain_config.geth_rpc_addr)?);
Ok(PythRandom::new(
chain_config.contract_addr,
@ -262,7 +263,7 @@ impl EntropyReader for PythContract {
user_random_number: [u8; 32],
provider_revelation: [u8; 32],
) -> Result<Option<U256>> {
let result: Result<U256, ContractError<Provider<Http>>> = self
let result: Result<U256, ContractError<MyMiddleware<Provider<Http>>>> = self
.reveal_with_callback(
provider,
sequence_number,