Cheatcodes
To manipulate the state of the blockchain, as well as test for specific reverts and events, Foundry is shipped with a set of cheatcodes.
Cheatcodes are accessed using the vm instance available in Forge Standard Library's Test contract.
prank
Sets msg.sender to the specified address for the next call. "The next call" includes static calls as well, but not calls to the cheat code address.
If the alternative signature of prank is used, then tx.origin is set as well for the next call.
/// function withdraw() public {
/// require(msg.sender == owner);
vm.prank(owner);
myContract.withdraw(); // [PASS]startPrank
Sets msg.sender for all subsequent calls until stopPrank is called.
If the alternative signature of startPrank is used, then tx.origin is set as well for all subsequent calls.
function startPrank(address) external;
function startPrank(address sender, address origin) external;stopPrank
Stops an active prank started by startPrank, resetting msg.sender and tx.origin to the values before startPrank was called.
deal
Sets the balance of an address who to newBalance.
hoax
Similar to prank + deal except that it adds to the existing user's balance where deal resets the balance to the specified amount.
txGasPrice
Sets tx.gasprice for the rest of the transaction.
warp
Sets block.timestamp
roll
Sets block.number.
recordLogs
Tells the VM to start recording all the emitted events. To access them, use getRecordedLogs.
expectEmit
Assert a specific log is emitted during the next call.
Call the cheat code, specifying whether we should check the first, second or third topic, and the log data (
expectEmit()checks them all). Topic 0 is always checked.Emit the event we are supposed to see during the next call.
Perform the call.
You can perform steps 1 and 2 multiple times to match a sequence of events in the next call.
If the event is not available in the current scope (e.g. if we are using an interface, or an external smart contract), we can define the event ourselves with an identical event signature.
There are 2 varieties of expectEmit:
Without checking the emitter address: Asserts the topics match without checking the emitting address.
With
address: Asserts the topics match and that the emitting address matches.
expectEmit Examples
envUint
Read an environment variable as uint256 or uint256[].
load
Read a storage value directly.
Last updated