From 1cba9967dfa893050f315530ec91711bb1ef1730 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 23 Dec 2024 15:41:06 +0530 Subject: [PATCH] fix(evmlib): add timeout to pending tx builder --- evmlib/src/contract/network_token.rs | 11 +++++++---- evmlib/src/contract/payment_vault/handler.rs | 16 ++++++++++++---- evmlib/src/lib.rs | 3 +++ evmlib/src/wallet.rs | 17 +++++++++++++++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/evmlib/src/contract/network_token.rs b/evmlib/src/contract/network_token.rs index 10903c9fd2..df18b37a1d 100644 --- a/evmlib/src/contract/network_token.rs +++ b/evmlib/src/contract/network_token.rs @@ -8,6 +8,7 @@ use crate::common::{Address, Calldata, TxHash, U256}; use crate::contract::network_token::NetworkTokenContract::NetworkTokenContractInstance; +use crate::TX_TIMEOUT; use alloy::network::TransactionBuilder; use alloy::providers::{Network, Provider}; use alloy::sol; @@ -108,9 +109,10 @@ where .await .inspect_err(|err| { error!( - "Error approving spender {spender:?} to spend raw amt of tokens {value}: {err:?}" + "Error to send_transaction while approving spender {spender:?} to spend raw amt of tokens {value}: {err:?}" ) - })?; + })? + .with_timeout(Some(TX_TIMEOUT)); let pending_tx_hash = *pending_tx_builder.tx_hash(); @@ -150,8 +152,9 @@ where .send_transaction(transaction_request) .await .inspect_err(|err| { - error!("Error transferring raw amt of tokens to {receiver:?}: {err:?}") - })?; + error!("Error to send_transaction during transfer raw amt of tokens to {receiver:?}: {err:?}") + })? + .with_timeout(Some(TX_TIMEOUT)); let pending_tx_hash = *pending_tx_builder.tx_hash(); debug!( diff --git a/evmlib/src/contract/payment_vault/handler.rs b/evmlib/src/contract/payment_vault/handler.rs index 1000d7d108..4330f31fbe 100644 --- a/evmlib/src/contract/payment_vault/handler.rs +++ b/evmlib/src/contract/payment_vault/handler.rs @@ -2,6 +2,7 @@ use crate::common::{Address, Amount, Calldata, TxHash}; use crate::contract::payment_vault::error::Error; use crate::contract::payment_vault::interface::IPaymentVault; use crate::contract::payment_vault::interface::IPaymentVault::IPaymentVaultInstance; +use crate::TX_TIMEOUT; use alloy::network::{Network, TransactionBuilder}; use alloy::providers::Provider; use alloy::transports::Transport; @@ -59,13 +60,20 @@ where .with_to(to) .with_input(calldata); - let tx_hash = self + let pending_tx_builder = self .contract .provider() .send_transaction(transaction_request) - .await? - .watch() - .await?; + .await + .inspect_err(|err| error!("Error to send_transaction during pay_for_quotes: {err:?}"))? + .with_timeout(Some(TX_TIMEOUT)); + + let pending_tx_hash = pending_tx_builder.tx_hash(); + debug!("pay_for_quotes is pending with tx hash: {pending_tx_hash}"); + + let tx_hash = pending_tx_builder.watch().await.inspect_err(|err| { + error!("Error to watch transaction during pay_for_quotes: {err:?}") + })?; Ok(tx_hash) } diff --git a/evmlib/src/lib.rs b/evmlib/src/lib.rs index abd5d3309a..e2715c0ed6 100644 --- a/evmlib/src/lib.rs +++ b/evmlib/src/lib.rs @@ -52,6 +52,9 @@ const ARBITRUM_ONE_DATA_PAYMENTS_ADDRESS: Address = const ARBITRUM_SEPOLIA_DATA_PAYMENTS_ADDRESS: Address = address!("993C7739f50899A997fEF20860554b8a28113634"); +/// Timeout for transactions +const TX_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60); + #[serde_as] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct CustomNetwork { diff --git a/evmlib/src/wallet.rs b/evmlib/src/wallet.rs index 0f6ba3acea..40b302ccad 100644 --- a/evmlib/src/wallet.rs +++ b/evmlib/src/wallet.rs @@ -12,7 +12,7 @@ use crate::contract::payment_vault::handler::PaymentVaultHandler; use crate::contract::payment_vault::MAX_TRANSFERS_PER_TRANSACTION; use crate::contract::{network_token, payment_vault}; use crate::utils::http_provider; -use crate::Network; +use crate::{Network, TX_TIMEOUT}; use alloy::hex::ToHexExt; use alloy::network::{Ethereum, EthereumWallet, NetworkWallet, TransactionBuilder}; use alloy::providers::fillers::{ @@ -262,7 +262,20 @@ pub async fn transfer_gas_tokens( .with_to(receiver) .with_value(amount); - let tx_hash = provider.send_transaction(tx).await?.watch().await?; + let pending_tx_builder = provider + .send_transaction(tx) + .await + .inspect_err(|err| { + error!("Error to send_transaction during transfer_gas_tokens: {err}"); + })? + .with_timeout(Some(TX_TIMEOUT)); + let pending_tx_hash = *pending_tx_builder.tx_hash(); + debug!("The transfer of gas tokens is pending with tx_hash: {pending_tx_hash}"); + + let tx_hash = pending_tx_builder.watch().await.inspect_err(|err| { + error!("Error watching transfer_gas_tokens tx with hash {pending_tx_hash}: {err}") + })?; + debug!("Transfer of gas tokens with tx_hash: {tx_hash} is successful"); Ok(tx_hash) }