Skip to main content

foundry_cli/opts/
evm.rs

1//! CLI arguments for configuring the EVM settings.
2
3use alloy_primitives::{Address, B256, U256};
4use clap::Parser;
5use foundry_config::{
6    Chain, Config,
7    figment::{
8        self, Metadata, Profile, Provider,
9        error::Kind::InvalidType,
10        value::{Dict, Map, Value},
11    },
12};
13use serde::Serialize;
14
15use foundry_common::shell;
16
17/// `EvmArgs` and `EnvArgs` take the highest precedence in the Config/Figment hierarchy.
18///
19/// All vars are opt-in, their default values are expected to be set by the
20/// [`foundry_config::Config`], and are always present ([`foundry_config::Config::default`])
21///
22/// Both have corresponding types in the `evm_adapters` crate which have mandatory fields.
23/// The expected workflow is
24///   1. load the [`foundry_config::Config`]
25///   2. merge with `EvmArgs` into a `figment::Figment`
26///   3. extract `evm_adapters::Opts` from the merged `Figment`
27///
28/// # Example
29///
30/// ```ignore
31/// use foundry_config::Config;
32/// use forge::executor::opts::EvmOpts;
33/// use foundry_cli::opts::EvmArgs;
34/// # fn t(args: EvmArgs) {
35/// let figment = Config::figment_with_root(".").merge(args);
36/// let opts = figment.extract::<EvmOpts>().unwrap();
37/// # }
38/// ```
39#[derive(Clone, Debug, Default, Serialize, Parser)]
40#[command(next_help_heading = "EVM options", about = None, long_about = None)] // override doc
41pub struct EvmArgs {
42    /// Fetch state over a remote endpoint instead of starting from an empty state.
43    ///
44    /// If you want to fetch state from a specific block number, see --fork-block-number.
45    #[arg(long, short, visible_alias = "rpc-url", value_name = "URL")]
46    #[serde(rename = "eth_rpc_url", skip_serializing_if = "Option::is_none")]
47    pub fork_url: Option<String>,
48
49    /// Fetch state from a specific block number over a remote endpoint.
50    ///
51    /// See --fork-url.
52    #[arg(long, requires = "fork_url", value_name = "BLOCK")]
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub fork_block_number: Option<u64>,
55
56    /// Number of retries.
57    ///
58    /// See --fork-url.
59    #[arg(long, requires = "fork_url", value_name = "RETRIES")]
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub fork_retries: Option<u32>,
62
63    /// Initial retry backoff on encountering errors.
64    ///
65    /// See --fork-url.
66    #[arg(long, requires = "fork_url", value_name = "BACKOFF")]
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub fork_retry_backoff: Option<u64>,
69
70    /// Explicitly disables the use of RPC caching.
71    ///
72    /// All storage slots are read entirely from the endpoint.
73    ///
74    /// This flag overrides the project's configuration file.
75    ///
76    /// See --fork-url.
77    #[arg(long)]
78    #[serde(skip)]
79    pub no_storage_caching: bool,
80
81    /// The initial balance of deployed test contracts.
82    #[arg(long, value_name = "BALANCE")]
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub initial_balance: Option<U256>,
85
86    /// The address which will be executing tests/scripts.
87    #[arg(long, value_name = "ADDRESS")]
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub sender: Option<Address>,
90
91    /// Enable the FFI cheatcode.
92    #[arg(long)]
93    #[serde(skip)]
94    pub ffi: bool,
95
96    /// Whether to show `console.log` outputs in realtime during script/test execution
97    #[arg(long)]
98    #[serde(skip)]
99    pub live_logs: bool,
100
101    /// Use the create 2 factory in all cases including tests and non-broadcasting scripts.
102    #[arg(long)]
103    #[serde(skip)]
104    pub always_use_create_2_factory: bool,
105
106    /// The CREATE2 deployer address to use, this will override the one in the config.
107    #[arg(long, value_name = "ADDRESS")]
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub create2_deployer: Option<Address>,
110
111    /// Sets the number of assumed available compute units per second for this provider
112    ///
113    /// default value: 330
114    ///
115    /// See also --fork-url and <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
116    #[arg(long, alias = "cups", value_name = "CUPS", help_heading = "Fork config")]
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub compute_units_per_second: Option<u64>,
119
120    /// Disables rate limiting for this node's provider.
121    ///
122    /// See also --fork-url and <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
123    #[arg(
124        long,
125        value_name = "NO_RATE_LIMITS",
126        help_heading = "Fork config",
127        visible_alias = "no-rate-limit"
128    )]
129    #[serde(skip)]
130    pub no_rpc_rate_limit: bool,
131
132    /// All ethereum environment related arguments
133    #[command(flatten)]
134    #[serde(flatten)]
135    pub env: EnvArgs,
136
137    /// Whether to enable isolation of calls.
138    /// In isolation mode all top-level calls are executed as a separate transaction in a separate
139    /// EVM context, enabling more precise gas accounting and transaction state changes.
140    #[arg(long)]
141    #[serde(skip)]
142    pub isolate: bool,
143}
144
145// Make this set of options a `figment::Provider` so that it can be merged into the `Config`
146impl Provider for EvmArgs {
147    fn metadata(&self) -> Metadata {
148        Metadata::named("Evm Opts Provider")
149    }
150
151    fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
152        let value = Value::serialize(self)?;
153        let error = InvalidType(value.to_actual(), "map".into());
154        let mut dict = value.into_dict().ok_or(error)?;
155
156        if shell::verbosity() > 0 {
157            // need to merge that manually otherwise `from_occurrences` does not work
158            dict.insert("verbosity".to_string(), shell::verbosity().into());
159        }
160
161        if self.ffi {
162            dict.insert("ffi".to_string(), self.ffi.into());
163        }
164
165        if self.live_logs {
166            dict.insert("live_logs".to_string(), self.live_logs.into());
167        }
168
169        if self.isolate {
170            dict.insert("isolate".to_string(), self.isolate.into());
171        }
172
173        if self.always_use_create_2_factory {
174            dict.insert(
175                "always_use_create_2_factory".to_string(),
176                self.always_use_create_2_factory.into(),
177            );
178        }
179
180        if self.no_storage_caching {
181            dict.insert("no_storage_caching".to_string(), self.no_storage_caching.into());
182        }
183
184        if self.no_rpc_rate_limit {
185            dict.insert("no_rpc_rate_limit".to_string(), self.no_rpc_rate_limit.into());
186        }
187
188        Ok(Map::from([(Config::selected_profile(), dict)]))
189    }
190}
191
192/// Configures the executor environment during tests.
193#[derive(Clone, Debug, Default, Serialize, Parser)]
194#[command(next_help_heading = "Executor environment config")]
195pub struct EnvArgs {
196    /// EIP-170: Contract code size limit in bytes. Useful to increase this because of tests. By
197    /// default, it is 0x6000 (~25kb).
198    #[arg(long, value_name = "CODE_SIZE")]
199    #[serde(skip_serializing_if = "Option::is_none")]
200    pub code_size_limit: Option<usize>,
201
202    /// The chain name or EIP-155 chain ID.
203    #[arg(long, visible_alias = "chain-id", value_name = "CHAIN")]
204    #[serde(rename = "chain_id", skip_serializing_if = "Option::is_none", serialize_with = "id")]
205    pub chain: Option<Chain>,
206
207    /// The gas price.
208    #[arg(long, value_name = "GAS_PRICE")]
209    #[serde(skip_serializing_if = "Option::is_none")]
210    pub gas_price: Option<u64>,
211
212    /// The base fee in a block.
213    #[arg(long, visible_alias = "base-fee", value_name = "FEE")]
214    #[serde(skip_serializing_if = "Option::is_none")]
215    pub block_base_fee_per_gas: Option<u64>,
216
217    /// The transaction origin.
218    #[arg(long, value_name = "ADDRESS")]
219    #[serde(skip_serializing_if = "Option::is_none")]
220    pub tx_origin: Option<Address>,
221
222    /// The coinbase of the block.
223    #[arg(long, value_name = "ADDRESS")]
224    #[serde(skip_serializing_if = "Option::is_none")]
225    pub block_coinbase: Option<Address>,
226
227    /// The timestamp of the block.
228    #[arg(long, value_name = "TIMESTAMP")]
229    #[serde(skip_serializing_if = "Option::is_none")]
230    pub block_timestamp: Option<u64>,
231
232    /// The block number.
233    #[arg(long, value_name = "BLOCK")]
234    #[serde(skip_serializing_if = "Option::is_none")]
235    pub block_number: Option<u64>,
236
237    /// The block difficulty.
238    #[arg(long, value_name = "DIFFICULTY")]
239    #[serde(skip_serializing_if = "Option::is_none")]
240    pub block_difficulty: Option<u64>,
241
242    /// The block prevrandao value. NOTE: Before merge this field was mix_hash.
243    #[arg(long, value_name = "PREVRANDAO")]
244    #[serde(skip_serializing_if = "Option::is_none")]
245    pub block_prevrandao: Option<B256>,
246
247    /// The block gas limit.
248    #[arg(long, visible_alias = "gas-limit", value_name = "BLOCK_GAS_LIMIT")]
249    #[serde(skip_serializing_if = "Option::is_none")]
250    pub block_gas_limit: Option<u64>,
251
252    /// The memory limit per EVM execution in bytes.
253    /// If this limit is exceeded, a `MemoryLimitOOG` result is thrown.
254    ///
255    /// The default is 128MiB.
256    #[arg(long, value_name = "MEMORY_LIMIT")]
257    #[serde(skip_serializing_if = "Option::is_none")]
258    pub memory_limit: Option<u64>,
259
260    /// Whether to disable the block gas limit checks.
261    #[arg(long, visible_aliases = &["no-block-gas-limit", "no-gas-limit"])]
262    #[serde(skip_serializing_if = "std::ops::Not::not")]
263    pub disable_block_gas_limit: bool,
264
265    /// Whether to enable tx gas limit checks as imposed by Osaka (EIP-7825).
266    #[arg(long, visible_alias = "tx-gas-limit")]
267    #[serde(skip_serializing_if = "std::ops::Not::not")]
268    pub enable_tx_gas_limit: bool,
269}
270
271/// We have to serialize chain IDs and not names because when extracting an EVM `Env`, it expects
272/// `chain_id` to be `u64`.
273fn id<S: serde::Serializer>(chain: &Option<Chain>, s: S) -> Result<S::Ok, S::Error> {
274    if let Some(chain) = chain {
275        s.serialize_u64(chain.id())
276    } else {
277        // skip_serializing_if = "Option::is_none" should prevent this branch from being taken
278        unreachable!()
279    }
280}
281
282#[cfg(test)]
283mod tests {
284    use super::*;
285    use foundry_config::NamedChain;
286
287    #[test]
288    fn compute_units_per_second_skips_when_none() {
289        let args = EvmArgs::default();
290        let data = args.data().expect("provider data");
291        let dict = data.get(&Config::selected_profile()).expect("profile dict");
292        assert!(
293            !dict.contains_key("compute_units_per_second"),
294            "compute_units_per_second should be skipped when None"
295        );
296    }
297
298    #[test]
299    fn compute_units_per_second_present_when_some() {
300        let args = EvmArgs { compute_units_per_second: Some(1000), ..Default::default() };
301        let data = args.data().expect("provider data");
302        let dict = data.get(&Config::selected_profile()).expect("profile dict");
303        let val = dict.get("compute_units_per_second").expect("cups present");
304        assert_eq!(val, &Value::from(1000u64));
305    }
306
307    #[test]
308    fn can_parse_chain_id() {
309        let args = EvmArgs {
310            env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
311            ..Default::default()
312        };
313        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
314        assert_eq!(config.chain, Some(NamedChain::Mainnet.into()));
315
316        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "goerli"]);
317        assert_eq!(env.chain, Some(NamedChain::Goerli.into()));
318    }
319
320    #[test]
321    fn test_memory_limit() {
322        let args = EvmArgs {
323            env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
324            ..Default::default()
325        };
326        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
327        assert_eq!(config.memory_limit, Config::default().memory_limit);
328
329        let env = EnvArgs::parse_from(["foundry-cli", "--memory-limit", "100"]);
330        assert_eq!(env.memory_limit, Some(100));
331    }
332
333    #[test]
334    fn test_chain_id() {
335        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "1"]);
336        assert_eq!(env.chain, Some(Chain::mainnet()));
337
338        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "mainnet"]);
339        assert_eq!(env.chain, Some(Chain::mainnet()));
340        let args = EvmArgs { env, ..Default::default() };
341        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
342        assert_eq!(config.chain, Some(Chain::mainnet()));
343    }
344}