Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: running script with --broadcast for a transaction sequence can error out due to nonce desync from rpc latency #9096

Merged
merged 11 commits into from
Oct 14, 2024
15 changes: 12 additions & 3 deletions crates/script/src/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use foundry_common::{
use foundry_config::Config;
use futures::{future::join_all, StreamExt};
use itertools::Itertools;
use std::sync::Arc;
use std::{cmp::Ordering, sync::Arc};

pub async fn estimate_gas<P, T>(
tx: &mut WithOtherFields<TransactionRequest>,
Expand Down Expand Up @@ -70,8 +70,17 @@ pub async fn send_transaction(
let nonce = provider.get_transaction_count(from).await?;

let tx_nonce = tx.nonce.expect("no nonce");
if nonce != tx_nonce {
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")

match nonce.cmp(&tx_nonce) {
Ordering::Greater => {
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")
}
Ordering::Less => {
warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Proceeding with expected.");
}
Ordering::Equal => {
// Nonces are equal, no action needed
}
}
}

Expand Down
Loading