From b642230c7df212fedb3e29f0712b4ddadf484bad Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Mon, 14 Mar 2022 16:57:06 +0530 Subject: [PATCH] feat(signer): set from on tx before calling eth_call, eth_createAccessList, eth_estimateGas (#1021) --- ethers-middleware/src/signer.rs | 37 ++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ethers-middleware/src/signer.rs b/ethers-middleware/src/signer.rs index 542df8ca..c4b72a2e 100644 --- a/ethers-middleware/src/signer.rs +++ b/ethers-middleware/src/signer.rs @@ -1,5 +1,6 @@ use ethers_core::types::{ - transaction::eip2718::TypedTransaction, Address, BlockId, Bytes, Signature, + transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed}, + Address, BlockId, Bytes, Signature, U256, }; use ethers_providers::{maybe, FromErr, Middleware, PendingTransaction}; use ethers_signers::Signer; @@ -158,6 +159,14 @@ where this.signer = signer; this } + + fn set_tx_from_if_none(&self, tx: &TypedTransaction) -> TypedTransaction { + let mut tx = tx.clone(); + if tx.from().is_none() { + tx.set_from(self.address); + } + tx + } } #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] @@ -264,6 +273,32 @@ where ) -> Result { self.signer.sign_message(data.into()).await.map_err(SignerMiddlewareError::SignerError) } + + async fn estimate_gas(&self, tx: &TypedTransaction) -> Result { + let tx = self.set_tx_from_if_none(tx); + self.inner.estimate_gas(&tx).await.map_err(SignerMiddlewareError::MiddlewareError) + } + + async fn create_access_list( + &self, + tx: &TypedTransaction, + block: Option, + ) -> Result { + let tx = self.set_tx_from_if_none(tx); + self.inner + .create_access_list(&tx, block) + .await + .map_err(SignerMiddlewareError::MiddlewareError) + } + + async fn call( + &self, + tx: &TypedTransaction, + block: Option, + ) -> Result { + let tx = self.set_tx_from_if_none(tx); + self.inner().call(&tx, block).await.map_err(SignerMiddlewareError::MiddlewareError) + } } #[cfg(all(test, not(feature = "celo"), not(target_arch = "wasm32")))]