Skip to content

Commit 7cd93eb

Browse files
feat(cheats): support arrays + rename read + resolve env vars (#11302)
* feat(cheats): add fork cheats to load array vars * add more tests * refactor to reduce duplicate code * fix: test spacing * style: clippy * fix: test spacing * style: rename to `readFork..` * docs: improve cheat comments * style: rename missing cheats * docs: update cmnts * fix: use existing toml and json parsing helper fns * feat: resolve env vars in 'fork' config * fix: test spacing * fix: expected test logs * fix: use `let Self { .. } = self` syntax --------- Co-authored-by: grandizzy <[email protected]>
1 parent 15233c6 commit 7cd93eb

File tree

11 files changed

+2771
-2032
lines changed

11 files changed

+2771
-2032
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 1984 additions & 1704 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/assets/cheatcodes.schema.json

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

crates/cheatcodes/spec/src/cheatcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub enum Group {
8888
Scripting,
8989
/// Cheatcodes that interact with the program's forking configuration.
9090
///
91-
/// Examples: `forkChains`, `forkChainRpcUrl`, `forkUint`.
91+
/// Examples: `readForkChains`, `readForkChainRpcUrl`, `readForkUint`, `readForkAddressArray`.
9292
///
9393
/// Safety: safe.
9494
Forking,

crates/cheatcodes/spec/src/vm.rs

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,105 +2189,175 @@ interface Vm {
21892189
///
21902190
/// Note that the configured fork chains are subsections of the `[fork]` section of 'foundry.toml'.
21912191
#[cheatcode(group = Forking)]
2192-
function forkChains() external view returns (string[] memory);
2192+
function readForkChains() external view returns (string[] memory);
21932193

21942194
/// Returns an array with the ids of all the configured fork chains.
21952195
///
21962196
/// Note that the configured fork chains are subsections of the `[fork]` section of 'foundry.toml'.
21972197
#[cheatcode(group = Forking)]
2198-
function forkChainIds() external view returns (uint256[] memory);
2198+
function readForkChainIds() external view returns (uint256[] memory);
21992199

22002200
/// Returns the chain name of the currently selected fork.
22012201
#[cheatcode(group = Forking)]
2202-
function forkChain() external view returns (string memory);
2202+
function readForkChain() external view returns (string memory);
22032203

22042204
/// Returns the chain id of the currently selected fork.
22052205
#[cheatcode(group = Forking)]
2206-
function forkChainId() external view returns (uint256);
2206+
function readForkChainId() external view returns (uint256);
22072207

22082208
/// Returns the rpc url of the currently selected fork.
22092209
///
22102210
/// By default, the rpc url of each fork is derived from the `[rpc_endpoints]`, unless
22112211
/// the rpc config is specifically informed in the fork config for that specific chain.
22122212
#[cheatcode(group = Forking)]
2213-
function forkRpcUrl() external view returns (string memory);
2213+
function readForkRpcUrl() external view returns (string memory);
22142214

22152215
/// Returns the rpc url of the corresponding chain id.
22162216
///
22172217
/// By default, the rpc url of each fork is derived from the `[rpc_endpoints]`, unless
22182218
/// the rpc config is specifically informed in the fork config for that specific chain.
22192219
#[cheatcode(group = Forking)]
2220-
function forkChainRpcUrl(uint256 id) external view returns (string memory);
2220+
function readForkChainRpcUrl(uint256 id) external view returns (string memory);
22212221

2222-
/// Gets the value for the key `key` from the currently active fork and parses it as `bool`.
2222+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `bool`.
22232223
/// Reverts if the key was not found or the value could not be parsed.
22242224
#[cheatcode(group = Forking)]
2225-
function forkBool(string calldata key) external view returns (bool);
2225+
function readForkBool(string calldata key) external view returns (bool);
22262226

2227-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bool`.
2227+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `bool`.
22282228
/// Reverts if the key was not found or the value could not be parsed.
22292229
#[cheatcode(group = Forking)]
2230-
function forkChainBool(uint256 chain, string calldata key) external view returns (bool);
2230+
function readForkChainBool(uint256 chain, string calldata key) external view returns (bool);
22312231

2232-
/// Gets the value for the key `key` from the currently active fork and parses it as `int256`.
2232+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `bool`.
2233+
/// Reverts if a key was not found or any of the values could not be parsed.
2234+
#[cheatcode(group = Forking)]
2235+
function readForkBoolArray(string calldata key) external view returns (bool[] memory);
2236+
2237+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `bool`.
2238+
/// Reverts if a key was not found or any of the values could not be parsed.
2239+
#[cheatcode(group = Forking)]
2240+
function readForkChainBoolArray(uint256 chain, string calldata key) external view returns (bool[] memory);
2241+
2242+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `int256`.
22332243
/// Reverts if the key was not found or the value could not be parsed.
22342244
#[cheatcode(group = Forking)]
2235-
function forkInt(string calldata key) external view returns (int256);
2245+
function readForkInt(string calldata key) external view returns (int256);
22362246

2237-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `int256`.
2247+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `int256`.
22382248
/// Reverts if the key was not found or the value could not be parsed.
22392249
#[cheatcode(group = Forking)]
2240-
function forkChainInt(uint256 chain, string calldata key) external view returns (int256);
2250+
function readForkChainInt(uint256 chain, string calldata key) external view returns (int256);
2251+
2252+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `int256`.
2253+
/// Reverts if a key was not found or any of the values could not be parsed.
2254+
#[cheatcode(group = Forking)]
2255+
function readForkIntArray(string calldata key) external view returns (int256[] memory);
2256+
2257+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `int256`.
2258+
/// Reverts if a key was not found or any of the values could not be parsed.
2259+
#[cheatcode(group = Forking)]
2260+
function readForkChainIntArray(uint256 chain, string calldata key) external view returns (int256[] memory);
22412261

2242-
/// Gets the value for the key `key` from the currently active fork and parses it as `uint256`.
2262+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `uint256`.
22432263
/// Reverts if the key was not found or the value could not be parsed.
22442264
#[cheatcode(group = Forking)]
2245-
function forkUint(string calldata key) external view returns (uint256);
2265+
function readForkUint(string calldata key) external view returns (uint256);
22462266

2247-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `uint256`.
2267+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `uint256`.
22482268
/// Reverts if the key was not found or the value could not be parsed.
22492269
#[cheatcode(group = Forking)]
2250-
function forkChainUint(uint256 chain, string calldata key) external view returns (uint256);
2270+
function readForkChainUint(uint256 chain, string calldata key) external view returns (uint256);
2271+
2272+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `uint256`.
2273+
/// Reverts if a key was not found or any of the values could not be parsed.
2274+
#[cheatcode(group = Forking)]
2275+
function readForkUintArray(string calldata key) external view returns (uint256[] memory);
22512276

2252-
/// Gets the value for the key `key` from the currently active fork and parses it as `address`.
2277+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `uint256`.
2278+
/// Reverts if a key was not found or any of the values could not be parsed.
2279+
#[cheatcode(group = Forking)]
2280+
function readForkChainUintArray(uint256 chain, string calldata key) external view returns (uint256[] memory);
2281+
2282+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `address`.
22532283
/// Reverts if the key was not found or the value could not be parsed.
22542284
#[cheatcode(group = Forking)]
2255-
function forkAddress(string calldata key) external view returns (address);
2285+
function readForkAddress(string calldata key) external view returns (address);
22562286

2257-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `address`.
2287+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `address`.
22582288
/// Reverts if the key was not found or the value could not be parsed.
22592289
#[cheatcode(group = Forking)]
2260-
function forkChainAddress(uint256 chain, string calldata key) external view returns (address);
2290+
function readForkChainAddress(uint256 chain, string calldata key) external view returns (address);
2291+
2292+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `address`.
2293+
/// Reverts if a key was not found or any of the values could not be parsed.
2294+
#[cheatcode(group = Forking)]
2295+
function readForkAddressArray(string calldata key) external view returns (address[] memory);
22612296

2262-
/// Gets the value for the key `key` from the currently active fork and parses it as `bytes32`.
2297+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `address`.
2298+
/// Reverts if a key was not found or any of the values could not be parsed.
2299+
#[cheatcode(group = Forking)]
2300+
function readForkChainAddressArray(uint256 chain, string calldata key) external view returns (address[] memory);
2301+
2302+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `bytes32`.
22632303
/// Reverts if the key was not found or the value could not be parsed.
22642304
#[cheatcode(group = Forking)]
2265-
function forkBytes32(string calldata key) external view returns (bytes32);
2305+
function readForkBytes32(string calldata key) external view returns (bytes32);
22662306

2267-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bytes32`.
2307+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `bytes32`.
22682308
/// Reverts if the key was not found or the value could not be parsed.
22692309
#[cheatcode(group = Forking)]
2270-
function forkChainBytes32(uint256 chain, string calldata key) external view returns (bytes32);
2310+
function readForkChainBytes32(uint256 chain, string calldata key) external view returns (bytes32);
2311+
2312+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `bytes32`.
2313+
/// Reverts if a key was not found or any of the values could not be parsed.
2314+
#[cheatcode(group = Forking)]
2315+
function readForkBytes32Array(string calldata key) external view returns (bytes32[] memory);
22712316

2272-
/// Gets the value for the key `key` from the currently active fork and parses it as `string`.
2317+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `bytes32`.
2318+
/// Reverts if a key was not found or any of the values could not be parsed.
2319+
#[cheatcode(group = Forking)]
2320+
function readForkChainBytes32Array(uint256 chain, string calldata key) external view returns (bytes32[] memory);
2321+
2322+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `string`.
22732323
/// Reverts if the key was not found or the value could not be parsed.
22742324
#[cheatcode(group = Forking)]
2275-
function forkString(string calldata key) external view returns (string memory);
2325+
function readForkString(string calldata key) external view returns (string memory);
22762326

2277-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `string`.
2327+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `string`.
22782328
/// Reverts if the key was not found or the value could not be parsed.
22792329
#[cheatcode(group = Forking)]
2280-
function forkChainString(uint256 chain, string calldata key) external view returns (string memory);
2330+
function readForkChainString(uint256 chain, string calldata key) external view returns (string memory);
2331+
2332+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `string`.
2333+
/// Reverts if a key was not found or any of the values could not be parsed.
2334+
#[cheatcode(group = Forking)]
2335+
function readForkStringArray(string calldata key) external view returns (string[] memory);
2336+
2337+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `string`.
2338+
/// Reverts if a key was not found or any of the values could not be parsed.
2339+
#[cheatcode(group = Forking)]
2340+
function readForkChainStringArray(uint256 chain, string calldata key) external view returns (string[] memory);
22812341

2282-
/// Gets the value for the key `key` from the currently active fork and parses it as `bytes`.
2342+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as `bytes`.
22832343
/// Reverts if the key was not found or the value could not be parsed.
22842344
#[cheatcode(group = Forking)]
2285-
function forkBytes(string calldata key) external view returns (bytes memory);
2345+
function readForkBytes(string calldata key) external view returns (bytes memory);
22862346

2287-
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bytes`.
2347+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as `bytes`.
22882348
/// Reverts if the key was not found or the value could not be parsed.
22892349
#[cheatcode(group = Forking)]
2290-
function forkChainBytes(uint256 chain, string calldata key) external view returns (bytes memory);
2350+
function readForkChainBytes(uint256 chain, string calldata key) external view returns (bytes memory);
2351+
2352+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the currently active fork and parses it as an array of `bytes`.
2353+
/// Reverts if a key was not found or any of the values could not be parsed.
2354+
#[cheatcode(group = Forking)]
2355+
function readForkBytesArray(string calldata key) external view returns (bytes[] memory);
2356+
2357+
/// Gets the value for the key `key` from the `[fork.<chain>]` section of `foundry.toml` for the specified chain and parses it as an array of `bytes`.
2358+
/// Reverts if a key was not found or any of the values could not be parsed.
2359+
#[cheatcode(group = Forking)]
2360+
function readForkChainBytesArray(uint256 chain, string calldata key) external view returns (bytes[] memory);
22912361

22922362
// ======== Scripts ========
22932363
// -------- Broadcasting Transactions --------

crates/cheatcodes/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloy_primitives::{U256, map::AddressHashMap};
44
use foundry_common::{ContractsByArtifact, fs::normalize_path};
55
use foundry_compilers::{ArtifactId, ProjectPathsConfig, utils::canonicalize};
66
use foundry_config::{
7-
Config, ForkConfig, FsPermissions, ResolvedRpcEndpoint, ResolvedRpcEndpoints, RpcEndpoint,
7+
Config, ForkConfigs, FsPermissions, ResolvedRpcEndpoint, ResolvedRpcEndpoints, RpcEndpoint,
88
RpcEndpointUrl, cache::StorageCachingConfig, fs_permissions::FsAccessKind,
99
};
1010
use foundry_evm_core::opts::EvmOpts;
@@ -64,7 +64,7 @@ pub struct CheatsConfig {
6464
/// Mapping of chain IDs to their aliases
6565
pub chain_id_to_alias: HashMap<u64, String>,
6666
/// Fork configuration
67-
pub forks: HashMap<String, ForkConfig>,
67+
pub forks: ForkConfigs,
6868
}
6969

7070
/// Chain data for getChain cheatcodes

0 commit comments

Comments
 (0)