From 43142e86a8aeb4fcd1f86137b0b30f3db11bc62c Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Wed, 1 May 2024 17:42:27 -0700 Subject: [PATCH 1/2] simulate tx --- src/contracts/mod.rs | 12 ++++++++++-- src/ethereum/mod.rs | 10 ++++++++++ src/ethereum/write/mod.rs | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/contracts/mod.rs b/src/contracts/mod.rs index f4e64832..cf41e027 100644 --- a/src/contracts/mod.rs +++ b/src/contracts/mod.rs @@ -260,6 +260,10 @@ impl IdentityManager { ) .tx; + self.ethereum + .simulate_transaction(®ister_identities_transaction) + .await?; + self.ethereum .send_transaction(register_identities_transaction, true) .await @@ -277,7 +281,7 @@ impl IdentityManager { ) -> anyhow::Result { let proof_points_array: [U256; 8] = deletion_proof.into(); - let register_identities_transaction = self + let delete_identities_transaction = self .abi .delete_identities( proof_points_array, @@ -288,7 +292,11 @@ impl IdentityManager { .tx; self.ethereum - .send_transaction(register_identities_transaction, true) + .simulate_transaction(&delete_identities_transaction) + .await?; + + self.ethereum + .send_transaction(delete_identities_transaction, true) .await .map_err(|tx_err| anyhow!("{}", tx_err.to_string())) } diff --git a/src/ethereum/mod.rs b/src/ethereum/mod.rs index 65aaee3b..0c408096 100644 --- a/src/ethereum/mod.rs +++ b/src/ethereum/mod.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; +use ethers::providers::Middleware; use ethers::types::transaction::eip2718::TypedTransaction; use ethers::types::Address; pub use read::ReadProvider; @@ -74,6 +75,15 @@ impl Ethereum { self.write_provider.send_transaction(tx, only_once).await } + pub async fn simulate_transaction(&self, tx: &TypedTransaction) -> Result<(), TxError> { + if let Err(err) = self.read_provider.call(&tx, None).await { + tracing::error!("Error simulating transaction: {:?}", err); + return Err(TxError::Simulate(anyhow::Error::new(err))); + } + + Ok(()) + } + pub async fn fetch_pending_transactions(&self) -> Result, TxError> { self.write_provider.fetch_pending_transactions().await } diff --git a/src/ethereum/write/mod.rs b/src/ethereum/write/mod.rs index 7aeec2a7..42d2c476 100644 --- a/src/ethereum/write/mod.rs +++ b/src/ethereum/write/mod.rs @@ -35,6 +35,9 @@ pub enum TxError { #[error("Error sending transaction: {0}")] Send(anyhow::Error), + #[error("Error simulating transaction: {0}")] + Simulate(anyhow::Error), + #[error("Timeout while waiting for confirmations")] ConfirmationTimeout, From c1e660ef9e0d5675f98ed599c9af446782ffe3e1 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Wed, 1 May 2024 17:44:41 -0700 Subject: [PATCH 2/2] simulate before broadcasting --- src/contracts/mod.rs | 8 -------- src/ethereum/mod.rs | 6 +----- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/contracts/mod.rs b/src/contracts/mod.rs index cf41e027..5a7ad913 100644 --- a/src/contracts/mod.rs +++ b/src/contracts/mod.rs @@ -260,10 +260,6 @@ impl IdentityManager { ) .tx; - self.ethereum - .simulate_transaction(®ister_identities_transaction) - .await?; - self.ethereum .send_transaction(register_identities_transaction, true) .await @@ -291,10 +287,6 @@ impl IdentityManager { ) .tx; - self.ethereum - .simulate_transaction(&delete_identities_transaction) - .await?; - self.ethereum .send_transaction(delete_identities_transaction, true) .await diff --git a/src/ethereum/mod.rs b/src/ethereum/mod.rs index 0c408096..142a948b 100644 --- a/src/ethereum/mod.rs +++ b/src/ethereum/mod.rs @@ -72,16 +72,12 @@ impl Ethereum { tx: TypedTransaction, only_once: bool, ) -> Result { - self.write_provider.send_transaction(tx, only_once).await - } - - pub async fn simulate_transaction(&self, tx: &TypedTransaction) -> Result<(), TxError> { if let Err(err) = self.read_provider.call(&tx, None).await { tracing::error!("Error simulating transaction: {:?}", err); return Err(TxError::Simulate(anyhow::Error::new(err))); } - Ok(()) + self.write_provider.send_transaction(tx, only_once).await } pub async fn fetch_pending_transactions(&self) -> Result, TxError> {