Forge Standard Library •

Forge Standard Library is a collection of helpful contracts and libraries for use with Forge and Foundry. It leverages Forge's cheatcodes to make writing tests easier and faster, while improving the UX of cheatcodes.

Learn how to use Forge-Std with the 📖 Foundry Book (Forge-Std Guide).

Install

forge install foundry-rs/forge-std

Contracts

stdError

This is a helper contract for errors and reverts. In Forge, this contract is particularly helpful for the expectRevert cheatcode, as it provides all compiler built-in errors.

See the contract itself for all error codes.

Example usage


import "forge-std/Test.sol";

contract TestContract is Test {
    ErrorsTest test;

    function setUp() public {
        test = new ErrorsTest();
    }

    function testExpectArithmetic() public {
        vm.expectRevert(stdError.arithmeticError);
        test.arithmeticError(10);
    }
}

contract ErrorsTest {
    function arithmeticError(uint256 a) public {
        a = a - 100;
    }
}

stdStorage

This is a rather large contract due to all of the overloading to make the UX decent. Primarily, it is a wrapper around the record and accesses cheatcodes. It can always find and write the storage slot(s) associated with a particular variable without knowing the storage layout. The one major caveat to this is while a slot can be found for packed storage variables, we can't write to that variable safely. If a user tries to write to a packed slot, the execution throws an error, unless it is uninitialized (bytes32(0)).

This works by recording all SLOADs and SSTOREs during a function call. If there is a single slot read or written to, it immediately returns the slot. Otherwise, behind the scenes, we iterate through and check each one (assuming the user passed in a depth parameter). If the variable is a struct, you can pass in a depth parameter which is basically the field depth.

I.e.:

Example usage

stdCheats

This is a wrapper over miscellaneous cheatcodes that need wrappers to be more dev friendly. Currently there are only functions related to prank. In general, users may expect ETH to be put into an address on prank, but this is not the case for safety reasons. Explicitly this hoax function should only be used for addresses that have expected balances as it will get overwritten. If an address already has ETH, you should just use prank. If you want to change that balance explicitly, just use deal. If you want to do both, hoax is also right for you.

Example usage:

Std Assertions

Contains various assertions.

console.log

Usage follows the same format as Hardhat. It's recommended to use console2.sol as shown below, as this will show the decoded logs in Forge traces.

If you need compatibility with Hardhat, you must use the standard console.sol instead. Due to a bug in console.sol, logs that use uint256 or int256 types will not be properly decoded in Forge traces.

Contributing

See our contributing guidelines.

Getting Help

First, see if the answer to your question can be found in book.

If the answer is not there:

If you want to contribute, or follow along with contributor discussion, you can use our main telegram to chat with us about the development of Foundry!

License

Forge Standard Library is offered under either MIT or Apache 2.0 license.

Last updated