Skip to content

Commit 3e24dc3

Browse files
committed
feat: getNonce and setNonce cheatcodes
1 parent 708f48f commit 3e24dc3

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

evm-adapters/src/sputnik/cheatcodes/cheatcode_handler.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,15 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> CheatcodeStackExecutor<'a, 'b, B, P>
867867
self.add_debug(CheatOp::EXPECTCALL);
868868
self.state_mut().expected_calls.entry(inner.0).or_default().push(inner.1.to_vec());
869869
}
870+
HEVMCalls::GetNonce(inner) => {
871+
self.add_debug(CheatOp::GETNONCE);
872+
let nonce = self.state().basic(inner.0).nonce;
873+
res = ethers::abi::encode(&[Token::Uint(nonce)]);
874+
}
875+
HEVMCalls::SetNonce(inner) => {
876+
self.add_debug(CheatOp::SETNONCE);
877+
// TODO: wait for https://github.com/rust-blockchain/evm/pull/92
878+
}
870879
};
871880

872881
self.fill_trace(&trace, true, Some(res.clone()), pre_index);

evm-adapters/src/sputnik/cheatcodes/debugger.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ pub enum CheatOp {
179179
CLEARMOCKEDCALLS,
180180
EXPECTCALL,
181181
GETCODE,
182+
SETNONCE,
183+
GETNONCE,
182184
}
183185

184186
impl From<CheatOp> for OpCode {
@@ -212,6 +214,8 @@ impl CheatOp {
212214
CheatOp::CLEARMOCKEDCALLS => "VM_CLEARMOCKEDCALLS",
213215
CheatOp::EXPECTCALL => "VM_EXPECTCALL",
214216
CheatOp::GETCODE => "VM_GETCODE",
217+
CheatOp::GETNONCE => "VM_GETNONCE",
218+
CheatOp::SETNONCE => "VM_SETNONCE",
215219
}
216220
}
217221
}

evm-adapters/src/sputnik/cheatcodes/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ ethers::contract::abigen!(
7272
clearMockedCalls()
7373
expectCall(address,bytes)
7474
getCode(string)
75+
getNonce(address)
76+
setNonce(address,uint256)
7577
]"#,
7678
);
7779
pub use hevm_mod::{HEVMCalls, HEVM_ABI};

evm-adapters/testdata/CheatCodes.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ interface Hevm {
5959
function expectCall(address,bytes calldata) external;
6060

6161
function getCode(string calldata) external returns (bytes memory);
62+
63+
// Get account nonce
64+
function getNonce(address) external returns (uint256);
65+
66+
// Set account nonce
67+
function setNonce(address,uint256) external;
6268
}
6369

6470
contract HasStorage {
@@ -600,6 +606,25 @@ contract CheatCodes is DSTest {
600606
extcodecopy(who, add(o_code, 0x20), 0, size)
601607
}
602608
}
609+
610+
function testSetAndGetNonce() public {
611+
address alice = address(1337);
612+
assertEq(hevm.getNonce(alice), 0);
613+
614+
MockMe target = new MockMe();
615+
616+
hevm.startPrank(alice);
617+
target.setA();
618+
target.setA();
619+
assertEq(hevm.getNonce(alice), 2);
620+
621+
hevm.setNonce(alice, 20);
622+
assertEq(hevm.getNonce(alice), 20);
623+
624+
hevm.setNonce(alice, 14);
625+
assertEq(hevm.getNonce(alice), 14);
626+
hevm.stopPrank();
627+
}
603628
}
604629

605630
contract RecordAccess {
@@ -773,6 +798,7 @@ contract ExpectEmit {
773798
}
774799

775800
contract MockMe {
801+
uint256 _a;
776802
function numberA() public returns (uint256) {
777803
return 1;
778804
}
@@ -784,6 +810,10 @@ contract MockMe {
784810
function add(uint256 a, uint256 b) public returns (uint256) {
785811
return a + b;
786812
}
813+
814+
function setA() public {
815+
_a = 1;
816+
}
787817
}
788818

789819
contract MockInner {

0 commit comments

Comments
 (0)