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 foundry_evm_networks::NetworkConfigs;
14use serde::Serialize;
15
16use crate::opts::RpcCommonOpts;
17use foundry_common::shell;
18
19/// `EvmArgs` and `EnvArgs` take the highest precedence in the Config/Figment hierarchy.
20///
21/// All vars are opt-in, their default values are expected to be set by the
22/// [`foundry_config::Config`], and are always present ([`foundry_config::Config::default`])
23///
24/// Both have corresponding types in the `evm_adapters` crate which have mandatory fields.
25/// The expected workflow is
26///   1. load the [`foundry_config::Config`]
27///   2. merge with `EvmArgs` into a `figment::Figment`
28///   3. extract `evm_adapters::Opts` from the merged `Figment`
29///
30/// # Example
31///
32/// ```ignore
33/// use foundry_config::Config;
34/// use forge::executor::opts::EvmOpts;
35/// use foundry_cli::opts::EvmArgs;
36/// # fn t(args: EvmArgs) {
37/// let figment = Config::figment_with_root(".").merge(args);
38/// let opts = figment.extract::<EvmOpts>().unwrap();
39/// # }
40/// ```
41#[derive(Clone, Debug, Default, Serialize, Parser)]
42#[command(next_help_heading = "EVM options", about = None, long_about = None)] // override doc
43pub struct EvmArgs {
44    /// Common RPC options (URL, timeout, rate limiting, etc.).
45    #[command(flatten)]
46    #[serde(flatten)]
47    pub rpc: RpcCommonOpts,
48
49    /// Fetch state from a specific block number over a remote endpoint.
50    ///
51    /// See --rpc-url.
52    #[arg(long, requires = "rpc_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 --rpc-url.
59    #[arg(long, requires = "rpc_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 --rpc-url.
66    #[arg(long, requires = "rpc_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 --rpc-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    /// All ethereum environment related arguments
112    #[command(flatten)]
113    #[serde(flatten)]
114    pub env: EnvArgs,
115
116    /// Whether to enable isolation of calls.
117    /// In isolation mode all top-level calls are executed as a separate transaction in a separate
118    /// EVM context, enabling more precise gas accounting and transaction state changes.
119    #[arg(long)]
120    #[serde(skip)]
121    pub isolate: bool,
122
123    /// Network selection.
124    #[command(flatten)]
125    #[serde(skip)]
126    pub networks: NetworkConfigs,
127}
128
129// Make this set of options a `figment::Provider` so that it can be merged into the `Config`
130impl Provider for EvmArgs {
131    fn metadata(&self) -> Metadata {
132        Metadata::named("Evm Opts Provider")
133    }
134
135    fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
136        let value = Value::serialize(self)?;
137        let error = InvalidType(value.to_actual(), "map".into());
138        let mut dict = value.into_dict().ok_or(error)?;
139
140        if shell::verbosity() > 0 {
141            // need to merge that manually otherwise `from_occurrences` does not work
142            dict.insert("verbosity".to_string(), shell::verbosity().into());
143        }
144
145        if self.ffi {
146            dict.insert("ffi".to_string(), self.ffi.into());
147        }
148
149        if self.live_logs {
150            dict.insert("live_logs".to_string(), self.live_logs.into());
151        }
152
153        if self.isolate {
154            dict.insert("isolate".to_string(), self.isolate.into());
155        }
156
157        if self.always_use_create_2_factory {
158            dict.insert(
159                "always_use_create_2_factory".to_string(),
160                self.always_use_create_2_factory.into(),
161            );
162        }
163
164        if self.no_storage_caching {
165            dict.insert("no_storage_caching".to_string(), self.no_storage_caching.into());
166        }
167
168        // Merge serde-skipped fields from the common RPC options.
169        if self.rpc.no_rpc_rate_limit {
170            dict.insert("no_rpc_rate_limit".to_string(), true.into());
171        }
172        if self.rpc.accept_invalid_certs {
173            dict.insert("eth_rpc_accept_invalid_certs".to_string(), true.into());
174        }
175        if self.rpc.no_proxy {
176            dict.insert("eth_rpc_no_proxy".to_string(), true.into());
177        }
178
179        // Only insert network flags when explicitly set via CLI to avoid overriding
180        // values from foundry.toml (NetworkConfigs is flattened in Config).
181        if let Some(name) = self.networks.active_network_name() {
182            dict.insert("network".to_string(), name.into());
183        }
184        if self.networks.is_celo() {
185            dict.insert("celo".to_string(), true.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 {
301            rpc: RpcCommonOpts { compute_units_per_second: Some(1000), ..Default::default() },
302            ..Default::default()
303        };
304        let data = args.data().expect("provider data");
305        let dict = data.get(&Config::selected_profile()).expect("profile dict");
306        let val = dict.get("compute_units_per_second").expect("cups present");
307        assert_eq!(val, &Value::from(1000u64));
308    }
309
310    #[test]
311    fn rpc_url_arg_does_not_read_eth_rpc_url_env() {
312        use clap::CommandFactory;
313
314        let command = EvmArgs::command();
315        let rpc_url =
316            command.get_arguments().find(|arg| arg.get_id() == "rpc_url").expect("rpc_url arg");
317
318        assert!(rpc_url.get_env().is_none());
319    }
320
321    #[test]
322    fn can_parse_chain_id() {
323        let args = EvmArgs {
324            env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
325            ..Default::default()
326        };
327        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
328        assert_eq!(config.chain, Some(NamedChain::Mainnet.into()));
329
330        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "goerli"]);
331        assert_eq!(env.chain, Some(NamedChain::Goerli.into()));
332    }
333
334    #[test]
335    fn test_memory_limit() {
336        let args = EvmArgs {
337            env: EnvArgs { chain: Some(NamedChain::Mainnet.into()), ..Default::default() },
338            ..Default::default()
339        };
340        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
341        assert_eq!(config.memory_limit, Config::default().memory_limit);
342
343        let env = EnvArgs::parse_from(["foundry-cli", "--memory-limit", "100"]);
344        assert_eq!(env.memory_limit, Some(100));
345    }
346
347    #[test]
348    fn test_chain_id() {
349        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "1"]);
350        assert_eq!(env.chain, Some(Chain::mainnet()));
351
352        let env = EnvArgs::parse_from(["foundry-cli", "--chain-id", "mainnet"]);
353        assert_eq!(env.chain, Some(Chain::mainnet()));
354        let args = EvmArgs { env, ..Default::default() };
355        let config = Config::from_provider(Config::figment().merge(args)).unwrap();
356        assert_eq!(config.chain, Some(Chain::mainnet()));
357    }
358}