Skip to content

Commit ada53ee

Browse files
committed
feat(forge): getNonce and setNonce cheatcodes
1 parent 28f6d91 commit ada53ee

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+12
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,25 @@ 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+
870871
HEVMCalls::Label(inner) => {
871872
self.add_debug(CheatOp::LABEL);
872873
let address = inner.0;
873874
let label = inner.1;
874875

875876
self.state_mut().labels.insert(address, label);
876877
}
878+
879+
HEVMCalls::GetNonce(inner) => {
880+
self.add_debug(CheatOp::GETNONCE);
881+
let nonce = self.state().basic(inner.0).nonce;
882+
res = ethers::abi::encode(&[Token::Uint(nonce)]);
883+
}
884+
HEVMCalls::SetNonce(inner) => {
885+
self.add_debug(CheatOp::SETNONCE);
886+
// TODO
887+
888+
}
877889
};
878890

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

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

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ pub enum CheatOp {
180180
EXPECTCALL,
181181
GETCODE,
182182
LABEL,
183+
SETNONCE,
184+
GETNONCE,
183185
}
184186

185187
impl From<CheatOp> for OpCode {
@@ -214,6 +216,8 @@ impl CheatOp {
214216
CheatOp::EXPECTCALL => "VM_EXPECTCALL",
215217
CheatOp::GETCODE => "VM_GETCODE",
216218
CheatOp::LABEL => "VM_LABEL",
219+
CheatOp::GETNONCE => "VM_GETNONCE",
220+
CheatOp::SETNONCE => "VM_SETNONCE",
217221
}
218222
}
219223
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ ethers::contract::abigen!(
7373
expectCall(address,bytes)
7474
getCode(string)
7575
label(address,string)
76+
getNonce(address)
77+
setNonce(address,uint256)
7678
]"#,
7779
);
7880
pub use hevm_mod::{HEVMCalls, HEVM_ABI};

evm-adapters/testdata/CheatCodes.sol

+30
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ interface Hevm {
6161
function getCode(string calldata) external returns (bytes memory);
6262
// Labels an address in call traces
6363
function label(address, string calldata) external;
64+
65+
// Get account nonce
66+
function getNonce(address) external returns (uint256);
67+
68+
// Set account nonce
69+
function setNonce(address,uint256) external;
6470
}
6571

6672
contract HasStorage {
@@ -614,6 +620,25 @@ contract CheatCodes is DSTest {
614620
extcodecopy(who, add(o_code, 0x20), 0, size)
615621
}
616622
}
623+
624+
function testSetAndGetNonce() public {
625+
address alice = address(1337);
626+
assertEq(hevm.getNonce(alice), 0);
627+
628+
MockMe target = new MockMe();
629+
630+
hevm.startPrank(alice);
631+
target.setA();
632+
target.setA();
633+
assertEq(hevm.getNonce(alice), 2);
634+
635+
hevm.setNonce(alice, 20);
636+
assertEq(hevm.getNonce(alice), 20);
637+
638+
hevm.setNonce(alice, 14);
639+
assertEq(hevm.getNonce(alice), 14);
640+
hevm.stopPrank();
641+
}
617642
}
618643

619644
contract Label {
@@ -793,6 +818,7 @@ contract ExpectEmit {
793818
}
794819

795820
contract MockMe {
821+
uint256 _a;
796822
function numberA() public returns (uint256) {
797823
return 1;
798824
}
@@ -804,6 +830,10 @@ contract MockMe {
804830
function add(uint256 a, uint256 b) public returns (uint256) {
805831
return a + b;
806832
}
833+
834+
function setA() public {
835+
_a = 1;
836+
}
807837
}
808838

809839
contract MockInner {

0 commit comments

Comments
 (0)