Sitelet https://github.com/foundry-rs/forge-std/pull/768/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.
Expand Down Expand Up @@ -45,11 +46,12 @@ contract ErrorsTest {

### 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 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 `SLOAD`s and `SSTORE`s 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.:

```solidity
struct T {
// depth 0
Expand Down Expand Up @@ -167,11 +169,11 @@ contract Storage {

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:

```solidity

// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
Expand Down Expand Up @@ -255,9 +257,9 @@ First, see if the answer to your question can be found in [book](https://getfoun

If the answer is not there:

- Join the [support Telegram](https://t.me/foundry_support) to get help, or
- Open a [discussion](https://github.com/foundry-rs/foundry/discussions/new/choose) with your question, or
- Open an issue with [the bug](https://github.com/foundry-rs/foundry/issues/new/choose)
- Join the [support Telegram](https://t.me/foundry_support) to get help, or
- Open a [discussion](https://github.com/foundry-rs/foundry/discussions/new/choose) with your question, or
- Open an issue with [the bug](https://github.com/foundry-rs/foundry/issues/new/choose)

If you want to contribute, or follow along with contributor discussion, you can use our [main telegram](https://t.me/foundry_rs) to chat with us about the development of Foundry!

Expand Down
2 changes: 1 addition & 1 deletion src/Base.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

import {StdStorage} from "./StdStorage.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/Config.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;

import {console} from "./console.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/LibVariable.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;

// Enable globally.
Expand Down
2 changes: 1 addition & 1 deletion src/Script.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

// 💬 ABOUT
Expand Down
2 changes: 1 addition & 1 deletion src/StdAssertions.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;
pragma experimental ABIEncoderV2;

Expand Down
2 changes: 1 addition & 1 deletion src/StdChains.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;
pragma experimental ABIEncoderV2;

Expand Down
2 changes: 1 addition & 1 deletion src/StdCheats.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/StdConfig.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;

import {VmSafe} from "./Vm.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/StdConstants.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

import {IMulticall3} from "./interfaces/IMulticall3.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/StdError.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
// Panics work for versions >=0.8.0, but we lowered the pragma to make this compatible with Test
pragma solidity >=0.6.2 <0.9.0;

Expand Down
2 changes: 1 addition & 1 deletion src/StdInvariant.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/StdJson.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.0 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/StdMath.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

library stdMath {
Expand Down
2 changes: 1 addition & 1 deletion src/StdStorage.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

import {Vm} from "./Vm.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/StdStyle.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.4.22 <0.9.0;

import {VmSafe} from "./Vm.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/StdToml.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.0 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/StdUtils.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/Test.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
24 changes: 8 additions & 16 deletions src/console.sol
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.4.22 <0.9.0;

library console {
address constant CONSOLE_ADDRESS =
0x000000000000000000636F6e736F6c652e6c6f67;
address constant CONSOLE_ADDRESS = 0x000000000000000000636F6e736F6c652e6c6f67;

function _sendLogPayloadImplementation(bytes memory payload) internal view {
address consoleAddress = CONSOLE_ADDRESS;
/// @solidity memory-safe-assembly
assembly {
pop(
staticcall(
gas(),
consoleAddress,
add(payload, 32),
mload(payload),
0,
0
)
)
pop(staticcall(gas(), consoleAddress, add(payload, 32), mload(payload), 0, 0))
}
}

function _castToPure(
function(bytes memory) internal view fnIn
) internal pure returns (function(bytes memory) pure fnOut) {
function _castToPure(function(bytes memory) internal view fnIn)
internal
pure
returns (function(bytes memory) pure fnOut)
{
assembly {
fnOut := fnIn
}
Expand Down
2 changes: 1 addition & 1 deletion src/console2.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.4.22 <0.9.0;

import {console as console2} from "./console.sol";
2 changes: 1 addition & 1 deletion src/interfaces/IERC1155.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC165} from "./IERC165.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC165.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

interface IERC165 {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

/// @dev Interface of the ERC20 standard as defined in the EIP.
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC4626.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC20} from "./IERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC6909.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC165} from "./IERC165.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC721.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC165} from "./IERC165.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC7540.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC7575} from "./IERC7575.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC7575.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2;

import {IERC165} from "./IERC165.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IMulticall3.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion src/safeconsole.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

/// @author philogy <https://github.com/philogy>
Expand Down
2 changes: 1 addition & 1 deletion test/CommonBase.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {CommonBase} from "../src/Base.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/Config.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;

import {Test} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/LibVariable.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.13;

import {Test} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdAssertions.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {StdAssertions} from "../src/StdAssertions.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdChains.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {Test} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdCheats.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {StdCheats} from "../src/StdCheats.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdConstants.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {StdConstants} from "../src/StdConstants.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdError.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0 <0.9.0;

import {stdError} from "../src/StdError.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdJson.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {Test, stdJson} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdMath.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0 <0.9.0;

import {stdMath} from "../src/StdMath.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdStorage.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {stdStorage, StdStorage} from "../src/StdStorage.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdStyle.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {Test, console2, StdStyle} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdToml.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {Test, stdToml} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/StdUtils.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.0 <0.9.0;

import {Test, StdUtils} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/Vm.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0 <0.9.0;

import {Test} from "../src/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/compilation/CompilationScript.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion test/compilation/CompilationScriptBase.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion test/compilation/CompilationTest.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down
2 changes: 1 addition & 1 deletion test/compilation/CompilationTestBase.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;
Expand Down