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