Skip to content

Commit 2d2ebb0

Browse files
authored
feat(cheats): setup config env vars (#11236)
1 parent 5aa5567 commit 2d2ebb0

File tree

11 files changed

+1183
-4
lines changed

11 files changed

+1183
-4
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 400 additions & 0 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: 5 additions & 0 deletions
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ pub enum Group {
8686
///
8787
/// Safety: safe.
8888
Scripting,
89+
/// Cheatcodes that interact with the program's forking configuration.
90+
///
91+
/// Examples: `forkChains`, `forkChainRpcUrl`, `forkUint`.
92+
///
93+
/// Safety: safe.
94+
Forking,
8995
/// Cheatcodes that interact with the OS or filesystem.
9096
///
9197
/// Examples: `ffi`, `projectRoot`, `writeFile`.
@@ -140,6 +146,7 @@ impl Group {
140146
match self {
141147
Self::Evm | Self::Testing => None,
142148
Self::Scripting
149+
| Self::Forking
143150
| Self::Filesystem
144151
| Self::Environment
145152
| Self::String
@@ -157,6 +164,7 @@ impl Group {
157164
Self::Evm => "evm",
158165
Self::Testing => "testing",
159166
Self::Scripting => "scripting",
167+
Self::Forking => "forking",
160168
Self::Filesystem => "filesystem",
161169
Self::Environment => "environment",
162170
Self::String => "string",

crates/cheatcodes/spec/src/vm.rs

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,8 +2179,113 @@ interface Vm {
21792179
#[cheatcode(group = Environment)]
21802180
function isContext(ForgeContext context) external view returns (bool result);
21812181

2182-
// ======== Scripts ========
2182+
// ======== Forks ========
2183+
2184+
/// Returns an array with the name of all the configured fork chains.
2185+
///
2186+
/// Note that the configured fork chains are subsections of the `[fork]` section of 'foundry.toml'.
2187+
#[cheatcode(group = Forking)]
2188+
function forkChains() external view returns (string[] memory);
2189+
2190+
/// Returns an array with the ids of all the configured fork chains.
2191+
///
2192+
/// Note that the configured fork chains are subsections of the `[fork]` section of 'foundry.toml'.
2193+
#[cheatcode(group = Forking)]
2194+
function forkChainIds() external view returns (uint256[] memory);
2195+
2196+
/// Returns the chain name of the currently selected fork.
2197+
#[cheatcode(group = Forking)]
2198+
function forkChain() external view returns (string memory);
21832199

2200+
/// Returns the chain id of the currently selected fork.
2201+
#[cheatcode(group = Forking)]
2202+
function forkChainId() external view returns (uint256);
2203+
2204+
/// Returns the rpc url of the currently selected fork.
2205+
///
2206+
/// By default, the rpc url of each fork is derived from the `[rpc_endpoints]`, unless
2207+
/// the rpc config is specifically informed in the fork config for that specific chain.
2208+
#[cheatcode(group = Forking)]
2209+
function forkRpcUrl() external view returns (string memory);
2210+
2211+
/// Returns the rpc url of the corresponding chain id.
2212+
///
2213+
/// By default, the rpc url of each fork is derived from the `[rpc_endpoints]`, unless
2214+
/// the rpc config is specifically informed in the fork config for that specific chain.
2215+
#[cheatcode(group = Forking)]
2216+
function forkChainRpcUrl(uint256 id) external view returns (string memory);
2217+
2218+
/// Gets the value for the key `key` from the currently active fork and parses it as `bool`.
2219+
/// Reverts if the key was not found or the value could not be parsed.
2220+
#[cheatcode(group = Forking)]
2221+
function forkBool(string memory key) external view returns (bool);
2222+
2223+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bool`.
2224+
/// Reverts if the key was not found or the value could not be parsed.
2225+
#[cheatcode(group = Forking)]
2226+
function forkChainBool(uint256 chain, string memory key) external view returns (bool);
2227+
2228+
/// Gets the value for the key `key` from the currently active fork and parses it as `int256`.
2229+
/// Reverts if the key was not found or the value could not be parsed.
2230+
#[cheatcode(group = Forking)]
2231+
function forkInt(string memory key) external view returns (int256);
2232+
2233+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `int256`.
2234+
/// Reverts if the key was not found or the value could not be parsed.
2235+
#[cheatcode(group = Forking)]
2236+
function forkChainInt(uint256 chain, string memory key) external view returns (int256);
2237+
2238+
/// Gets the value for the key `key` from the currently active fork and parses it as `uint256`.
2239+
/// Reverts if the key was not found or the value could not be parsed.
2240+
#[cheatcode(group = Forking)]
2241+
function forkUint(string memory key) external view returns (uint256);
2242+
2243+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `uint256`.
2244+
/// Reverts if the key was not found or the value could not be parsed.
2245+
#[cheatcode(group = Forking)]
2246+
function forkChainUint(uint256 chain, string memory key) external view returns (uint256);
2247+
2248+
/// Gets the value for the key `key` from the currently active fork and parses it as `address`.
2249+
/// Reverts if the key was not found or the value could not be parsed.
2250+
#[cheatcode(group = Forking)]
2251+
function forkAddress(string memory key) external view returns (address);
2252+
2253+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `address`.
2254+
/// Reverts if the key was not found or the value could not be parsed.
2255+
#[cheatcode(group = Forking)]
2256+
function forkChainAddress(uint256 chain, string memory key) external view returns (address);
2257+
2258+
/// Gets the value for the key `key` from the currently active fork and parses it as `bytes32`.
2259+
/// Reverts if the key was not found or the value could not be parsed.
2260+
#[cheatcode(group = Forking)]
2261+
function forkBytes32(string memory key) external view returns (bytes32);
2262+
2263+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bytes32`.
2264+
/// Reverts if the key was not found or the value could not be parsed.
2265+
#[cheatcode(group = Forking)]
2266+
function forkChainBytes32(uint256 chain, string memory key) external view returns (bytes32);
2267+
2268+
/// Gets the value for the key `key` from the currently active fork and parses it as `string`.
2269+
/// Reverts if the key was not found or the value could not be parsed.
2270+
#[cheatcode(group = Forking)]
2271+
function forkString(string memory key) external view returns (string memory);
2272+
2273+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `string`.
2274+
/// Reverts if the key was not found or the value could not be parsed.
2275+
#[cheatcode(group = Forking)]
2276+
function forkChainString(uint256 chain, string memory key) external view returns (string memory);
2277+
2278+
/// Gets the value for the key `key` from the currently active fork and parses it as `bytes`.
2279+
/// Reverts if the key was not found or the value could not be parsed.
2280+
#[cheatcode(group = Forking)]
2281+
function forkBytes(string memory key) external view returns (bytes memory);
2282+
2283+
/// Gets the value for the key `key` from the fork config for chain `chain` and parses it as `bytes`.
2284+
/// Reverts if the key was not found or the value could not be parsed.
2285+
#[cheatcode(group = Forking)]
2286+
function forkChainBytes(uint256 chain, string memory key) external view returns (bytes memory);
2287+
2288+
// ======== Scripts ========
21842289
// -------- Broadcasting Transactions --------
21852290

21862291
/// Has the next call (at this call depth only) create transactions that can later be signed and sent onchain.

crates/cheatcodes/src/config.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ 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, FsPermissions, ResolvedRpcEndpoint, ResolvedRpcEndpoints, RpcEndpoint, RpcEndpointUrl,
8-
cache::StorageCachingConfig, fs_permissions::FsAccessKind,
7+
Config, ForkConfig, FsPermissions, ResolvedRpcEndpoint, ResolvedRpcEndpoints, RpcEndpoint,
8+
RpcEndpointUrl, cache::StorageCachingConfig, fs_permissions::FsAccessKind,
99
};
1010
use foundry_evm_core::opts::EvmOpts;
1111
use std::{
@@ -63,6 +63,8 @@ pub struct CheatsConfig {
6363
pub chains: HashMap<String, ChainData>,
6464
/// Mapping of chain IDs to their aliases
6565
pub chain_id_to_alias: HashMap<u64, String>,
66+
/// Fork configuration
67+
pub forks: HashMap<String, ForkConfig>,
6668
}
6769

6870
/// Chain data for getChain cheatcodes
@@ -114,6 +116,7 @@ impl CheatsConfig {
114116
internal_expect_revert: config.allow_internal_expect_revert,
115117
chains: HashMap::new(),
116118
chain_id_to_alias: HashMap::new(),
119+
forks: config.forks.clone(),
117120
}
118121
}
119122

@@ -318,6 +321,7 @@ impl Default for CheatsConfig {
318321
internal_expect_revert: false,
319322
chains: HashMap::new(),
320323
chain_id_to_alias: HashMap::new(),
324+
forks: Default::default(),
321325
}
322326
}
323327
}

0 commit comments

Comments
 (0)