From ca44d345c2bac9e7eb5b39b68565ac89754410a9 Mon Sep 17 00:00:00 2001 From: Daniel Savu <23065004+daniel-savu@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:03:06 +0000 Subject: [PATCH] fix(NonceManager): off-by-one error resyncing onchain nonce --- ethers-middleware/src/nonce_manager.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ethers-middleware/src/nonce_manager.rs b/ethers-middleware/src/nonce_manager.rs index aed41490f..2e1d7b6a7 100644 --- a/ethers-middleware/src/nonce_manager.rs +++ b/ethers-middleware/src/nonce_manager.rs @@ -160,7 +160,12 @@ where tracing::debug!(?nonce, "Sent transaction"); let tx_count_for_resync = self.get_tx_count_for_resync(); if new_txs_since_resync >= tx_count_for_resync { - let onchain_nonce = self.get_transaction_count(self.address, block).await?; + // subtract 1 because the nonce returned by `get_transaction_count` + // is the next one to be used, whereas we want the last one that landed + let onchain_nonce = self + .get_transaction_count(self.address, block) + .await? + .saturating_sub(1.into()); self.nonce.store(onchain_nonce.as_u64(), Ordering::SeqCst); self.txs_since_resync.store(0, Ordering::SeqCst); tracing::debug!(?nonce, "Resynced internal nonce with onchain nonce");