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.

prank example
/// 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.

startPrank example
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.

  1. 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.

  2. Emit the event we are supposed to see during the next call.

  3. 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.

Matching sequences

In functions that emit a lot of events, it's possible to "skip" events and only match a specific sequence, but this sequence must always be in order. As an example, let's say a function emits events: A, B, C, D, E, F, F, G.

expectEmit will be able to match ranges with and without skipping events in between:

  • [A, B, C] is valid.

  • [B, D, F] is valid.

  • [G] or any other single event combination is valid.

  • [B, A] or similar out-of-order combinations are invalid (events must be in order).

  • [C, F, F] is valid.

  • [F, F, C] is invalid (out of order).

expectEmit Examples

envUint

Read an environment variable as uint256 or uint256[].

load

Read a storage value directly.

Last updated