Skip to content

Commit fd5a204

Browse files
authored
docs for isContext cheatcode, verify with linked libraries and custom create2 (#1406)
add docs for isContext, note re verify with libraries and custom create2
1 parent ba87c23 commit fd5a204

File tree

8 files changed

+80
-2
lines changed

8 files changed

+80
-2
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
- [`stopAndReturnStateDiff`](./cheatcodes/stop-and-return-state-diff.md)
447447
- [`snapshotState`](./cheatcodes/state-snapshots.md)
448448
- [`snapshotGas`](./cheatcodes/gas-snapshots.md)
449+
- [`isContext`](./cheatcodes/is-context.md)
449450
- [Assertions](./cheatcodes/assertions.md)
450451
- [`expectRevert`](./cheatcodes/expect-revert.md)
451452
- [`expectEmit`](./cheatcodes/expect-emit.md)

src/cheatcodes/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
- [`txGasPrice`](./tx-gas-price.md)
3636
- [`startStateDiffRecording`](./start-state-diff-recording.md)
3737
- [`stopAndReturnStateDiff`](./stop-and-return-state-diff.md)
38+
- [`isContext`](./is-context.md)

src/cheatcodes/is-context.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## `isContext`
2+
3+
### Signature
4+
5+
```solidity
6+
enum ForgeContext {
7+
// Test group execution context (test, coverage or snapshot).
8+
TestGroup,
9+
// `forge test` execution context.
10+
Test,
11+
// `forge coverage` execution context.
12+
Coverage,
13+
// `forge snapshot` execution context.
14+
Snapshot,
15+
// Script group execution context (dry run, broadcast or resume).
16+
ScriptGroup,
17+
// `forge script` execution context.
18+
ScriptDryRun,
19+
// `forge script --broadcast` execution context.
20+
ScriptBroadcast,
21+
// `forge script --resume` execution context.
22+
ScriptResume,
23+
// Unknown `forge` execution context.
24+
Unknown
25+
}
26+
27+
function isContext(ForgeContext context) external view returns (bool result);
28+
```
29+
30+
### Description
31+
32+
Checks the current `forge` execution context.
33+
34+
### Examples
35+
36+
```solidity
37+
import {VmSafe} from "forge-std/Vm.sol";
38+
39+
if (vm.isContext(VmSafe.ForgeContext.ScriptDryRun)) {
40+
// execute specific script dry run logic
41+
}
42+
43+
if (vm.isContext(VmSafe.ForgeContext.ScriptBroadcast)) {
44+
// execute specific script broadcast logic
45+
}
46+
47+
if (vm.isContext(VmSafe.ForgeContext.ScriptResume)) {
48+
// execute specific script resume logic
49+
}
50+
```

src/forge/deploying.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ You must provide:
6868

6969
Moreover, you may need to provide:
7070
- the constructor arguments in the ABI-encoded format, if there are any
71+
- external linked libraries in `src_file_path:library_name:library_address` format, if there are any
7172
- [compiler version](https://etherscan.io/solcversions) used for build, with 8 hex digits from the commit version prefix (the commit will usually not be a nightly build). It is auto-detected if not specified.
7273
- the number of optimizations, if the Solidity optimizer was activated. It is auto-detected if not specified.
7374
- the [chain ID](https://evm-chainlist.netlify.app/), if the contract is not on Ethereum Mainnet
@@ -93,6 +94,15 @@ Submitted contract for verification:
9394
url: https://sepolia.etherscan.io//address/0x6a54…3a4c#code
9495
```
9596

97+
> ℹ️ **Note:**
98+
>
99+
> External libraries can be specified with `--libraries` argument, one for each linked library. For example, to verify a contract with two linked libraries (`Maths` and `Utils`) the `forge verify-command` should be run with
100+
> ```bash
101+
> --libraries src/lib/Maths.sol:Maths:<maths_lib_address> \
102+
> --libraries src/lib/Utils.sol:Utils:<utils_lib_address>
103+
> ```
104+
> arguments.
105+
96106
It is recommended to use the [`--watch`](../reference/forge/forge-verify-contract.md#verify-contract-options) flag along
97107
with `verify-contract` command in order to poll for the verification result.
98108

src/output/hello_foundry/tree

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/static/config.default.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ rpc_endpoints = { optimism = "https://optimism.alchemyapi.io/v2/...", mainnet =
140140
#build_info_path = None
141141
# Whether to allow `expectRevert` for internal functions.
142142
allow_internal_expect_revert = false
143+
# Use the create 2 factory in all cases including tests and non-broadcasting scripts.
144+
always_use_create_2_factory = false
145+
# The CREATE2 deployer address to use.
146+
create2_deployer = "0x4e59b44847b379578588920ca78fbf26c0b4956c"
147+
# CREATE2 salt to use for the library deployment in scripts.
148+
create2_library_salt = "0x0000000000000000000000000000000000000000000000000000000000000000"
143149

144150
[fmt]
145151
# Maximum line length where formatter will try to wrap the line

src/tutorials/create2-tutorial.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ This is in contrast to the `CREATE` opcode, where the address of the deployed co
99
With `CREATE2`, you can use the same deployer account to deploy contracts to the same address across multiple networks, even if the address has varying nonces.
1010

1111
> ℹ️ **Note**
12-
>This guide is intended to help understand `CREATE2`. In most use cases, you won't need to write and use your own deployer, and can use an existing deterministic deployer. In forge scripts, using `new MyContract{salt: salt}()` will use the deterministic deployer at [0x4e59b44847b379578588920ca78fbf26c0b4956c](https://github.com/Arachnid/deterministic-deployment-proxy).
12+
>
13+
> This guide is intended to help understand `CREATE2`. In most use cases, you won't need to write and use your own deployer, and can use an existing deterministic deployer. In forge scripts, using `new MyContract{salt: salt}()` will use by default the deterministic deployer at [0x4e59b44847b379578588920ca78fbf26c0b4956c](https://github.com/Arachnid/deterministic-deployment-proxy).
14+
> A different deployer address can be configured by setting `create2_deployer` in `foundry.toml` or by using `--create2-deployer` argument.
1315
1416
In this tutorial, we will:
1517

src/tutorials/solidity-scripting.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Solidity scripts are like the scripts you write when working with tools like Har
1515
3. Broadcasting - Optional. If the `--broadcast` flag is provided and the previous phases have succeeded, it will broadcast the transactions collected at step `1`. and simulated at step `2`.
1616
4. Verification - Optional. If the `--verify` flag is provided, there's an API key, and the previous phases have succeeded it will attempt to verify the contract. (eg. etherscan).
1717

18+
> 💡 Note:
19+
>
20+
> Transactions that previously failed or timed-out can be submitted again by providing `--resume` flag.
21+
1822
Given this flow, it's important to be aware that transactions whose behaviour can be influenced by external state/actors might have a different result than what was simulated on step `2`. Eg. frontrunning.
1923

2024
### Set Up
@@ -178,6 +182,10 @@ contract MyScript is Script {
178182
}
179183
```
180184

185+
> 💡 Note:
186+
>
187+
> The `vm.isContext` cheatcode can be used to execute logic specific to script phases, by checking if context is one of `ForgeContext.ScriptDryRun`, `ForgeContext.ScriptBroadcast` or `ForgeContext.ScriptResume`.
188+
181189
Now let’s read through the code and figure out what it actually means and does.
182190

183191
```solidity

0 commit comments

Comments
 (0)