Skip to content

Commit e7a0693

Browse files
authored
fix(script): use fork_block_number for init sender nonce (#9669)
* fix(`script`): use fork_block_number for init sender nonce * test
1 parent aa0161e commit e7a0693

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

crates/forge/tests/cli/script.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anvil::{spawn, NodeConfig};
66
use forge_script_sequence::ScriptSequence;
77
use foundry_config::Config;
88
use foundry_test_utils::{
9-
rpc,
9+
rpc::{self, next_http_rpc_endpoint},
1010
snapbox::IntoData,
1111
util::{OTHER_SOLC_VERSION, SOLC_VERSION},
1212
ScriptOutcome, ScriptTester,
@@ -2445,3 +2445,43 @@ contract ContractScript is Script {
24452445
assert_eq!(sequence.transactions.len(), 2);
24462446
assert_eq!(sequence.transactions[1].additional_contracts.len(), 1);
24472447
});
2448+
2449+
// <https://github.com/foundry-rs/foundry/issues/9661>
2450+
forgetest_async!(should_set_correct_sender_nonce_via_cli, |prj, cmd| {
2451+
foundry_test_utils::util::initialize(prj.root());
2452+
prj.add_script(
2453+
"MyScript.s.sol",
2454+
r#"
2455+
import {Script, console} from "forge-std/Script.sol";
2456+
2457+
contract MyScript is Script {
2458+
function run() public view {
2459+
console.log("sender nonce", vm.getNonce(msg.sender));
2460+
}
2461+
}
2462+
"#,
2463+
)
2464+
.unwrap();
2465+
2466+
let rpc_url = next_http_rpc_endpoint();
2467+
2468+
let fork_bn = 21614115;
2469+
2470+
cmd.forge_fuse()
2471+
.args([
2472+
"script",
2473+
"MyScript",
2474+
"--sender",
2475+
"0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97",
2476+
"--fork-block-number",
2477+
&fork_bn.to_string(),
2478+
"--rpc-url",
2479+
&rpc_url,
2480+
])
2481+
.assert_success()
2482+
.stdout_eq(str![[r#"[COMPILING_FILES] with [SOLC_VERSION]
2483+
[SOLC_VERSION] [ELAPSED]
2484+
...
2485+
== Logs ==
2486+
sender nonce 1124703[..]"#]]);
2487+
});

crates/script/src/broadcast.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
};
55
use alloy_chains::Chain;
66
use alloy_consensus::TxEnvelope;
7-
use alloy_eips::eip2718::Encodable2718;
7+
use alloy_eips::{eip2718::Encodable2718, BlockId};
88
use alloy_network::{AnyNetwork, EthereumWallet, TransactionBuilder};
99
use alloy_primitives::{
1010
map::{AddressHashMap, AddressHashSet},
@@ -49,10 +49,16 @@ where
4949
Ok(())
5050
}
5151

52-
pub async fn next_nonce(caller: Address, provider_url: &str) -> eyre::Result<u64> {
52+
pub async fn next_nonce(
53+
caller: Address,
54+
provider_url: &str,
55+
block_number: Option<u64>,
56+
) -> eyre::Result<u64> {
5357
let provider = try_get_http_provider(provider_url)
5458
.wrap_err_with(|| format!("bad fork_url provider: {provider_url}"))?;
55-
Ok(provider.get_transaction_count(caller).await?)
59+
60+
let block_id = block_number.map_or(BlockId::latest(), BlockId::number);
61+
Ok(provider.get_transaction_count(caller).block_id(block_id).await?)
5662
}
5763

5864
pub async fn send_transaction(

crates/script/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ pub struct ScriptConfig {
550550
impl ScriptConfig {
551551
pub async fn new(config: Config, evm_opts: EvmOpts) -> Result<Self> {
552552
let sender_nonce = if let Some(fork_url) = evm_opts.fork_url.as_ref() {
553-
next_nonce(evm_opts.sender, fork_url).await?
553+
next_nonce(evm_opts.sender, fork_url, evm_opts.fork_block_number).await?
554554
} else {
555555
// dapptools compatibility
556556
1
@@ -561,7 +561,7 @@ impl ScriptConfig {
561561

562562
pub async fn update_sender(&mut self, sender: Address) -> Result<()> {
563563
self.sender_nonce = if let Some(fork_url) = self.evm_opts.fork_url.as_ref() {
564-
next_nonce(sender, fork_url).await?
564+
next_nonce(sender, fork_url, None).await?
565565
} else {
566566
// dapptools compatibility
567567
1

0 commit comments

Comments
 (0)