Skip to content

Commit 9f73457

Browse files
committed
docs(cheatcodes): standardize all cheatcode reference pages
Improve 100+ cheatcode reference pages with consistent formatting: - Add frontmatter with SEO descriptions - Consolidate function signatures into single code blocks - Add parameter and return value tables - Improve examples with Vocs file name syntax - Add Gotchas sections with proper :::warning/:::note callouts - Add Related Cheatcodes sections with cross-references - Standardize See Also sections - Replace emoji callouts with Vocs syntax - Use :::code-group for multi-file examples (test file first)
1 parent 0e77914 commit 9f73457

File tree

103 files changed

+4283
-3112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4283
-3112
lines changed
Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,69 @@
1+
---
2+
description: Gets all storage slots that have been read or written to on an address
3+
---
4+
15
## `accesses`
26

37
### Signature
48

59
```solidity
6-
function accesses(
7-
address
8-
)
9-
external
10-
returns (
11-
bytes32[] memory reads,
12-
bytes32[] memory writes
13-
);
10+
function accesses(address target) external returns (bytes32[] memory reads, bytes32[] memory writes);
1411
```
1512

1613
### Description
1714

18-
Gets all storage slots that have been read (`reads`) or written to (`writes`) on an address.
15+
Gets all storage slots that have been read (`reads`) or written to (`writes`) on an address since [`record`](/reference/cheatcodes/record) was called.
16+
17+
### Parameters
1918

20-
Note that [`record`](/reference/cheatcodes/record.mdx) must be called first.
19+
| Parameter | Type | Description |
20+
|-----------|-----------|-------------------------------------------------|
21+
| `target` | `address` | The address to get storage accesses for |
2122

22-
> ℹ️ **Note**
23-
>
24-
> Every write also counts as an additional read.
23+
### Returns
24+
25+
| Parameter | Type | Description |
26+
|-----------|-------------|----------------------------------------|
27+
| `reads` | `bytes32[]` | Array of storage slots that were read |
28+
| `writes` | `bytes32[]` | Array of storage slots that were written |
2529

2630
### Examples
2731

28-
```solidity
29-
/// contract NumsContract {
30-
/// uint256 public num1 = 100; // slot 0
31-
/// uint256 public num2 = 200; // slot 1
32-
/// }
33-
34-
vm.record();
35-
numsContract.num2();
36-
(bytes32[] memory reads, bytes32[] memory writes) = vm.accesses(
37-
address(numsContract)
38-
);
39-
emit log_uint(uint256(reads[0])); // 1
32+
:::code-group
33+
34+
```solidity [test/Accesses.t.sol]
35+
function testAccesses() public {
36+
vm.record();
37+
numsContract.num2(); // reads slot 1
38+
39+
(bytes32[] memory reads, bytes32[] memory writes) = vm.accesses(
40+
address(numsContract)
41+
);
42+
43+
assertEq(uint256(reads[0]), 1); // slot 1 was read
44+
assertEq(writes.length, 0);
45+
}
46+
```
47+
48+
```solidity [src/NumsContract.sol]
49+
contract NumsContract {
50+
uint256 public num1 = 100; // slot 0
51+
uint256 public num2 = 200; // slot 1
52+
}
4053
```
54+
55+
:::
56+
57+
### Gotchas
58+
59+
:::warning
60+
You must call [`record`](/reference/cheatcodes/record) before calling `accesses`, otherwise the returned arrays will be empty.
61+
:::
62+
63+
:::note
64+
Every write also counts as an additional read. If you write to a slot, it will appear in both the `reads` and `writes` arrays.
65+
:::
66+
67+
### Related Cheatcodes
68+
69+
- [`record`](/reference/cheatcodes/record) - Starts recording storage accesses
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1+
---
2+
description: Returns the identifier of the currently active fork
3+
---
4+
15
## `activeFork`
26

37
### Signature
48

59
```solidity
6-
function activeFork() external returns (uint256);
10+
function activeFork() external view returns (uint256 forkId);
711
```
812

913
### Description
1014

1115
Returns the identifier for the currently active fork. Reverts if no fork is currently active.
1216

13-
### Examples
17+
### Returns
1418

15-
Get the currently active fork id:
19+
| Parameter | Type | Description |
20+
|-----------|-----------|---------------------------------------|
21+
| `forkId` | `uint256` | Identifier of the active fork |
1622

17-
```solidity
18-
uint256 mainnetForkId = vm.createFork(MAINNET_RPC_URL);
19-
uint256 optimismForkId = vm.createFork(OPTIMISM_RPC_URL);
23+
### Examples
2024

21-
assert(mainnetForkId != optimismForkId);
25+
```solidity [test/ActiveFork.t.sol]
26+
function testActiveFork() public {
27+
uint256 mainnetFork = vm.createFork(MAINNET_RPC_URL);
28+
uint256 optimismFork = vm.createFork(OPTIMISM_RPC_URL);
2229
23-
vm.selectFork(mainnetForkId);
24-
assertEq(vm.activeFork(), mainnetForkId);
30+
vm.selectFork(mainnetFork);
31+
assertEq(vm.activeFork(), mainnetFork);
2532
26-
vm.selectFork(optimismForkId);
27-
assertEq(vm.activeFork(), optimismForkId);
33+
vm.selectFork(optimismFork);
34+
assertEq(vm.activeFork(), optimismFork);
35+
}
2836
```
2937

30-
### SEE ALSO
38+
### Related Cheatcodes
3139

32-
- [createFork](/reference/cheatcodes/create-fork.mdx)
33-
- [selectFork](/reference/cheatcodes/select-fork.mdx)
40+
- [`createFork`](/reference/cheatcodes/create-fork) - Create a new fork
41+
- [`selectFork`](/reference/cheatcodes/select-fork) - Select a fork
42+
- [`createSelectFork`](/reference/cheatcodes/create-select-fork) - Create and select a fork
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1+
---
2+
description: Computes the address for a given private key
3+
---
4+
15
## `addr`
26

37
### Signature
48

59
```solidity
6-
function addr(uint256 privateKey) external returns (address);
10+
function addr(uint256 privateKey) external returns (address keyAddr);
711
```
812

913
### Description
1014

1115
Computes the address for a given private key.
1216

17+
### Parameters
18+
19+
| Parameter | Type | Description |
20+
|--------------|-----------|------------------------------|
21+
| `privateKey` | `uint256` | The private key to derive address from |
22+
23+
### Returns
24+
25+
| Type | Description |
26+
|-----------|--------------------------------------|
27+
| `address` | The address corresponding to the private key |
28+
1329
### Examples
1430

15-
```solidity
31+
```solidity [test/Addr.t.sol]
1632
address alice = vm.addr(1);
17-
emit log_address(alice); // 0x7e5f4552091a69125d5dfcb7b8c2659029395bdf
33+
emit log_address(alice); // 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf
1834
```
35+
36+
### Related Cheatcodes
37+
38+
- [`sign`](/reference/cheatcodes/sign) - Signs a digest with a private key
39+
- [`createWallet`](/reference/cheatcodes/create-wallet) - Creates a Wallet struct from a private key
40+
- [`deriveKey`](/reference/cheatcodes/derive-key) - Derives a private key from a mnemonic
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
1+
---
2+
description: Grants cheatcode access to an address in forking mode
3+
---
4+
15
## `allowCheatcodes`
26

37
### Signature
48

59
```solidity
6-
function allowCheatcodes(address) external;
10+
function allowCheatcodes(address account) external;
711
```
812

913
### Description
1014

11-
In forking mode, explicitly grant the given address cheatcode access.
15+
In forking mode, explicitly grants the given address cheatcode access.
16+
17+
By default, the test contract and its deployer have cheatcode access. Contracts deployed by addresses with cheatcode access also inherit it. However, contracts already deployed on the forked network do not have access.
18+
19+
### Parameters
20+
21+
| Parameter | Type | Description |
22+
|-----------|-----------|------------------------------------------|
23+
| `account` | `address` | The address to grant cheatcode access to |
24+
25+
### Examples
26+
27+
```solidity [test/AllowCheatcodes.t.sol]
28+
function testAllowCheatcodes() public {
29+
vm.createSelectFork(MAINNET_RPC_URL);
30+
31+
// Grant cheatcode access to an existing on-chain contract
32+
address existingContract = 0x1234...;
33+
vm.allowCheatcodes(existingContract);
34+
}
35+
```
36+
37+
### Gotchas
38+
39+
:::note
40+
This is only useful for complex test setups in forking mode where you need existing on-chain contracts to use cheatcodes.
41+
:::
1242

13-
By default, the test contract, and its deployer are allowed to access cheatcodes. In addition to that, cheat code
14-
access is granted if the contract was deployed by an address that already has cheatcode access.
15-
This will prevent cheatcode access from accounts already deployed on the forked network.
43+
### Related Cheatcodes
1644

17-
> ℹ️ **Note**
18-
>
19-
> This is only useful for more complex test setups in forking mode.
45+
- [`createFork`](/reference/cheatcodes/create-fork) - Create a fork
46+
- [`createSelectFork`](/reference/cheatcodes/create-select-fork) - Create and select a fork
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
description: Discards fuzz inputs if the next call reverts
3+
---
4+
15
## `assumeNoRevert`
26

37
### Signature
@@ -8,26 +12,39 @@ function assumeNoRevert() external;
812

913
### Description
1014

11-
The fuzzer will discard the current fuzz inputs and start a new fuzz run if next call reverted.
12-
13-
The test may fail if you hit the max number of rejects.
15+
The fuzzer will discard the current fuzz inputs and start a new fuzz run if the **next call** reverts.
1416

15-
You can configure the rejection thresholds by setting [`fuzz.max_test_rejects`][max-test-rejects] in your `foundry.toml` file.
17+
This is useful when testing functions that have input validation, allowing the fuzzer to automatically find valid inputs.
1618

1719
### Examples
1820

19-
For a function that requires an amount in certain range:
20-
```solidity
21-
function doSomething(uint256 amount) public {
22-
require(amount > 100 ether && amount < 1_000 ether);
23-
}
24-
```
25-
reverts are discarded, resulting in test pass (or fail if max number of rejects hit):
26-
```solidity
27-
function testSomething(uint256 amount) public {
21+
:::code-group
22+
23+
```solidity [test/AssumeNoRevert.t.sol]
24+
function testDoSomething(uint256 amount) public {
2825
vm.assumeNoRevert();
2926
target.doSomething(amount);
30-
// [PASS]
27+
// Passes if valid inputs are found
3128
}
3229
```
3330

31+
```solidity [src/Target.sol]
32+
function doSomething(uint256 amount) public {
33+
require(amount > 100 ether && amount < 1_000 ether);
34+
// ... logic
35+
}
36+
```
37+
38+
:::
39+
40+
### Gotchas
41+
42+
:::warning
43+
The test may fail if you hit the max number of rejects (i.e., too many fuzz inputs cause reverts).
44+
45+
Configure the rejection threshold with [`fuzz.max_test_rejects`](/config/reference/testing#max_test_rejects) in `foundry.toml`.
46+
:::
47+
48+
### Related Cheatcodes
49+
50+
- [`assume`](/reference/cheatcodes/assume) - Discard fuzz inputs based on a boolean condition
Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,67 @@
1+
---
2+
description: Discards fuzz inputs that don't satisfy a condition
3+
---
4+
15
## `assume`
26

37
### Signature
48

59
```solidity
6-
function assume(bool) external;
10+
function assume(bool condition) external;
711
```
812

913
### Description
1014

1115
If the boolean expression evaluates to false, the fuzzer will discard the current fuzz inputs and start a new fuzz run.
1216

13-
The `assume` cheatcode should mainly be used for very narrow checks.
14-
Broad checks will slow down tests as it will take a while to find valid values, and the test may fail if you hit the max number of rejects.
15-
16-
You can configure the rejection thresholds by setting [`fuzz.max_test_rejects`][max-test-rejects] in your `foundry.toml` file.
17+
Use `assume` for **narrow checks** where only specific values should be excluded. For broad range constraints, use [`bound`](/reference/forge-std/bound) instead.
1718

18-
For broad checks, such as ensuring a `uint256` falls within a certain range, you can bound your input with the modulo operator or Forge Standard's [`bound`][forge-std-bound] method.
19+
### Parameters
1920

20-
More information on filtering via `assume` can be found [here][filtering-guide].
21+
| Parameter | Type | Description |
22+
|-------------|--------|--------------------------------------------------|
23+
| `condition` | `bool` | If false, the current fuzz run is discarded |
2124

2225
### Examples
2326

24-
```solidity
25-
// Good example of using assume
26-
function testSomething(uint256 a) public {
27+
#### Excluding specific values
28+
29+
```solidity [test/Assume.t.sol]
30+
function testWithAssume(uint256 a) public {
2731
vm.assume(a != 1);
2832
require(a != 1);
2933
// [PASS]
3034
}
3135
```
3236

33-
```solidity
34-
// In this case assume is not a great fit, so you should bound inputs manually
35-
function testSomethingElse(uint256 a) public {
37+
#### Use bound for ranges instead
38+
39+
```solidity [test/UseBound.t.sol]
40+
function testWithBound(uint256 a) public {
41+
// Don't use assume for ranges - use bound instead
3642
a = bound(a, 100, 1e36);
3743
require(a >= 100 && a <= 1e36);
3844
// [PASS]
3945
}
4046
```
4147

42-
### SEE ALSO
48+
### Gotchas
49+
50+
:::warning
51+
Broad checks will slow down tests significantly as the fuzzer struggles to find valid values. The test may fail if you hit the max number of rejects.
52+
53+
Configure the rejection threshold with [`fuzz.max_test_rejects`](/config/reference/testing#max_test_rejects) in `foundry.toml`.
54+
:::
55+
56+
:::note
57+
For range constraints, prefer [`bound`](/reference/forge-std/bound) or the modulo operator over `assume`. These approaches are much more efficient.
58+
:::
59+
60+
### Related Cheatcodes
61+
62+
- [`assumeNoRevert`](/reference/cheatcodes/assume-no-revert) - Discard fuzz inputs if the next call reverts
4363

44-
Forge Standard Library
45-
[`bound`][forge-std-bound]
64+
### See Also
4665

47-
[max-test-rejects]: /config/reference/testing#max_test_rejects
48-
[forge-std-bound]: /reference/forge-std/bound
49-
[filtering-guide]: https://altsysrq.github.io/proptest-book/proptest/tutorial/filtering.html#filtering
66+
- [`bound`](/reference/forge-std/bound) - Bound a value to a range
67+
- [Filtering Guide](https://altsysrq.github.io/proptest-book/proptest/tutorial/filtering.html#filtering) - More on filtering in property testing

0 commit comments

Comments
 (0)