Skip to content

bug(cheatcodes): vm.getNonce(player) the nonce is incorrect #8811

@EthanOK

Description

@EthanOK

Discussed in #8584

Originally posted by EthanOK August 2, 2024
In the latest version, it will only be increased in the new Contract, but not in the calling contract.
Test demo

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract VmNonceTest is Test {
    address public player = vm.addr(666666);

    function setUp() external {}

    function testNonce() public {
        vm.startPrank(player, player);
        ERC20 token = new ERC20("", "");
        token.transfer(address(2), 0);
        assertEq(2, vm.getNonce(player));
        vm.stopPrank();
    }

    function testNonce_2() public {
        vm.startPrank(player, player);
        new ERC20("", "");
        new ERC20("", "");
        assertEq(2, vm.getNonce(player));
        vm.stopPrank();
    }
}

forge --version
forge 0.2.0 (0116be1 2024-07-09T00:18:45.429480000Z)

forge test --match-contract VmNonceTest -vvv

[FAIL. Reason: assertion failed: 2 != 1] testNonce() (gas: 479181)
Traces:
  [479181] VmNonceTest::testNonce()
    ├─ [0] VM::startPrank(0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895, 0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895)
    │   └─ ← [Return] 
    ├─ [432587] → new ERC20@0x1eaEcB92761a82978FdE14BCdd01383fb84162Fe
    │   └─ ← [Return] 2130 bytes of code
    ├─ [7138] ERC20::transfer(0x0000000000000000000000000000000000000002, 0)
    │   ├─ emit Transfer(from: 0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895, to: 0x0000000000000000000000000000000000000002, value: 0)
    │   └─ ← [Return] true
    ├─ [0] VM::getNonce(0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895) [staticcall]
    │   └─ ← [Return] 1
    ├─ [0] VM::assertEq(2, 1) [staticcall]
    │   └─ ← [Revert] assertion failed: 2 != 1
    └─ ← [Revert] assertion failed: 2 != 1

[PASS] testNonce_2() (gas: 937254)
```</div>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions