cast/cmd/
run.rs

1use alloy_consensus::Transaction;
2use alloy_network::{AnyNetwork, TransactionResponse};
3use alloy_primitives::{
4    Address, Bytes, U256,
5    map::{HashMap, HashSet},
6};
7use alloy_provider::{Provider, RootProvider};
8use alloy_rpc_types::BlockTransactions;
9use clap::Parser;
10use eyre::{Result, WrapErr};
11use foundry_cli::{
12    opts::{EtherscanOpts, RpcOpts},
13    utils::{TraceResult, handle_traces, init_progress},
14};
15use foundry_common::{SYSTEM_TRANSACTION_TYPE, is_impersonated_tx, is_known_system_sender, shell};
16use foundry_compilers::artifacts::EvmVersion;
17use foundry_config::{
18    Config,
19    figment::{
20        self, Metadata, Profile,
21        value::{Dict, Map},
22    },
23};
24use foundry_evm::{
25    Env,
26    executors::{EvmError, TracingExecutor},
27    opts::EvmOpts,
28    traces::{InternalTraceMode, TraceMode, Traces},
29    utils::configure_tx_env,
30};
31use foundry_evm_core::env::AsEnvMut;
32
33use crate::utils::apply_chain_and_block_specific_env_changes;
34
35/// CLI arguments for `cast run`.
36#[derive(Clone, Debug, Parser)]
37pub struct RunArgs {
38    /// The transaction hash.
39    tx_hash: String,
40
41    /// Opens the transaction in the debugger.
42    #[arg(long, short)]
43    debug: bool,
44
45    /// Whether to identify internal functions in traces.
46    #[arg(long)]
47    decode_internal: bool,
48
49    /// Print out opcode traces.
50    #[arg(long, short)]
51    trace_printer: bool,
52
53    /// Executes the transaction only with the state from the previous block.
54    ///
55    /// May result in different results than the live execution!
56    #[arg(long)]
57    quick: bool,
58
59    /// Disables the labels in the traces.
60    #[arg(long, default_value_t = false)]
61    disable_labels: bool,
62
63    /// Label addresses in the trace.
64    ///
65    /// Example: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045:vitalik.eth
66    #[arg(long, short)]
67    label: Vec<String>,
68
69    #[command(flatten)]
70    etherscan: EtherscanOpts,
71
72    #[command(flatten)]
73    rpc: RpcOpts,
74
75    /// The EVM version to use.
76    ///
77    /// Overrides the version specified in the config.
78    #[arg(long)]
79    evm_version: Option<EvmVersion>,
80
81    /// Sets the number of assumed available compute units per second for this provider
82    ///
83    /// default value: 330
84    ///
85    /// See also, <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
86    #[arg(long, alias = "cups", value_name = "CUPS")]
87    pub compute_units_per_second: Option<u64>,
88
89    /// Disables rate limiting for this node's provider.
90    ///
91    /// default value: false
92    ///
93    /// See also, <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
94    #[arg(long, value_name = "NO_RATE_LIMITS", visible_alias = "no-rpc-rate-limit")]
95    pub no_rate_limit: bool,
96
97    /// Use current project artifacts for trace decoding.
98    #[arg(long, visible_alias = "la")]
99    pub with_local_artifacts: bool,
100
101    /// Disable block gas limit check.
102    #[arg(long)]
103    pub disable_block_gas_limit: bool,
104
105    /// Enable the tx gas limit checks as imposed by Osaka (EIP-7825).
106    #[arg(long)]
107    pub enable_tx_gas_limit: bool,
108}
109
110impl RunArgs {
111    /// Executes the transaction by replaying it
112    ///
113    /// This replays the entire block the transaction was mined in unless `quick` is set to true
114    ///
115    /// Note: This executes the transaction(s) as is: Cheatcodes are disabled
116    pub async fn run(self) -> Result<()> {
117        let figment = self.rpc.clone().into_figment(self.with_local_artifacts).merge(&self);
118        let evm_opts = figment.extract::<EvmOpts>()?;
119        let mut config = Config::from_provider(figment)?.sanitized();
120
121        let label = self.label;
122        let with_local_artifacts = self.with_local_artifacts;
123        let debug = self.debug;
124        let decode_internal = self.decode_internal;
125        let disable_labels = self.disable_labels;
126        let compute_units_per_second =
127            if self.no_rate_limit { Some(u64::MAX) } else { self.compute_units_per_second };
128
129        let provider = foundry_cli::utils::get_provider_builder(&config)?
130            .compute_units_per_second_opt(compute_units_per_second)
131            .build()?;
132
133        let tx_hash = self.tx_hash.parse().wrap_err("invalid tx hash")?;
134        let tx = provider
135            .get_transaction_by_hash(tx_hash)
136            .await
137            .wrap_err_with(|| format!("tx not found: {tx_hash:?}"))?
138            .ok_or_else(|| eyre::eyre!("tx not found: {:?}", tx_hash))?;
139
140        // check if the tx is a system transaction
141        if is_known_system_sender(tx.from())
142            || tx.transaction_type() == Some(SYSTEM_TRANSACTION_TYPE)
143        {
144            return Err(eyre::eyre!(
145                "{:?} is a system transaction.\nReplaying system transactions is currently not supported.",
146                tx.tx_hash()
147            ));
148        }
149
150        let tx_block_number =
151            tx.block_number.ok_or_else(|| eyre::eyre!("tx may still be pending: {:?}", tx_hash))?;
152
153        // fetch the block the transaction was mined in
154        let block = provider.get_block(tx_block_number.into()).full().await?;
155
156        // we need to fork off the parent block
157        config.fork_block_number = Some(tx_block_number - 1);
158
159        let create2_deployer = evm_opts.create2_deployer;
160        let (mut env, fork, chain, networks) =
161            TracingExecutor::get_fork_material(&config, evm_opts).await?;
162        let mut evm_version = self.evm_version;
163
164        env.evm_env.cfg_env.disable_block_gas_limit = self.disable_block_gas_limit;
165
166        // By default do not enforce transaction gas limits imposed by Osaka (EIP-7825).
167        // Users can opt-in to enable these limits by setting `enable_tx_gas_limit` to true.
168        if !self.enable_tx_gas_limit {
169            env.evm_env.cfg_env.tx_gas_limit_cap = Some(u64::MAX);
170        }
171
172        env.evm_env.cfg_env.limit_contract_code_size = None;
173        env.evm_env.block_env.number = U256::from(tx_block_number);
174
175        if let Some(block) = &block {
176            env.evm_env.block_env.timestamp = U256::from(block.header.timestamp);
177            env.evm_env.block_env.beneficiary = block.header.beneficiary;
178            env.evm_env.block_env.difficulty = block.header.difficulty;
179            env.evm_env.block_env.prevrandao = Some(block.header.mix_hash.unwrap_or_default());
180            env.evm_env.block_env.basefee = block.header.base_fee_per_gas.unwrap_or_default();
181            env.evm_env.block_env.gas_limit = block.header.gas_limit;
182
183            // TODO: we need a smarter way to map the block to the corresponding evm_version for
184            // commonly used chains
185            if evm_version.is_none() {
186                // if the block has the excess_blob_gas field, we assume it's a Cancun block
187                if block.header.excess_blob_gas.is_some() {
188                    evm_version = Some(EvmVersion::Prague);
189                }
190            }
191            apply_chain_and_block_specific_env_changes::<AnyNetwork>(env.as_env_mut(), block);
192        }
193
194        let trace_mode = TraceMode::Call
195            .with_debug(self.debug)
196            .with_decode_internal(if self.decode_internal {
197                InternalTraceMode::Full
198            } else {
199                InternalTraceMode::None
200            })
201            .with_state_changes(shell::verbosity() > 4);
202        let mut executor = TracingExecutor::new(
203            env.clone(),
204            fork,
205            evm_version,
206            trace_mode,
207            networks,
208            create2_deployer,
209            None,
210        )?;
211        let mut env = Env::new_with_spec_id(
212            env.evm_env.cfg_env.clone(),
213            env.evm_env.block_env.clone(),
214            env.tx.clone(),
215            executor.spec_id(),
216        );
217
218        // Set the state to the moment right before the transaction
219        if !self.quick {
220            if !shell::is_json() {
221                sh_println!("Executing previous transactions from the block.")?;
222            }
223
224            if let Some(block) = block {
225                let pb = init_progress(block.transactions.len() as u64, "tx");
226                pb.set_position(0);
227
228                let BlockTransactions::Full(ref txs) = block.transactions else {
229                    return Err(eyre::eyre!("Could not get block txs"));
230                };
231
232                for (index, tx) in txs.iter().enumerate() {
233                    // System transactions such as on L2s don't contain any pricing info so
234                    // we skip them otherwise this would cause
235                    // reverts
236                    if is_known_system_sender(tx.from())
237                        || tx.transaction_type() == Some(SYSTEM_TRANSACTION_TYPE)
238                    {
239                        pb.set_position((index + 1) as u64);
240                        continue;
241                    }
242                    if tx.tx_hash() == tx_hash {
243                        break;
244                    }
245
246                    configure_tx_env(&mut env.as_env_mut(), &tx.inner);
247
248                    env.evm_env.cfg_env.disable_balance_check = true;
249
250                    if let Some(to) = Transaction::to(tx) {
251                        trace!(tx=?tx.tx_hash(),?to, "executing previous call transaction");
252                        executor.transact_with_env(env.clone()).wrap_err_with(|| {
253                            format!(
254                                "Failed to execute transaction: {:?} in block {}",
255                                tx.tx_hash(),
256                                env.evm_env.block_env.number
257                            )
258                        })?;
259                    } else {
260                        trace!(tx=?tx.tx_hash(), "executing previous create transaction");
261                        if let Err(error) = executor.deploy_with_env(env.clone(), None) {
262                            match error {
263                                // Reverted transactions should be skipped
264                                EvmError::Execution(_) => (),
265                                error => {
266                                    return Err(error).wrap_err_with(|| {
267                                        format!(
268                                            "Failed to deploy transaction: {:?} in block {}",
269                                            tx.tx_hash(),
270                                            env.evm_env.block_env.number
271                                        )
272                                    });
273                                }
274                            }
275                        }
276                    }
277
278                    pb.set_position((index + 1) as u64);
279                }
280            }
281        }
282
283        // Execute our transaction
284        let result = {
285            executor.set_trace_printer(self.trace_printer);
286
287            configure_tx_env(&mut env.as_env_mut(), &tx.inner);
288            if is_impersonated_tx(tx.inner.inner.inner()) {
289                env.evm_env.cfg_env.disable_balance_check = true;
290            }
291
292            if let Some(to) = Transaction::to(&tx) {
293                trace!(tx=?tx.tx_hash(), to=?to, "executing call transaction");
294                TraceResult::try_from(executor.transact_with_env(env))?
295            } else {
296                trace!(tx=?tx.tx_hash(), "executing create transaction");
297                TraceResult::try_from(executor.deploy_with_env(env, None))?
298            }
299        };
300
301        let contracts_bytecode = fetch_contracts_bytecode_from_trace(&provider, &result).await?;
302        handle_traces(
303            result,
304            &config,
305            chain,
306            &contracts_bytecode,
307            label,
308            with_local_artifacts,
309            debug,
310            decode_internal,
311            disable_labels,
312        )
313        .await?;
314
315        Ok(())
316    }
317}
318
319pub async fn fetch_contracts_bytecode_from_trace(
320    provider: &RootProvider<AnyNetwork>,
321    result: &TraceResult,
322) -> Result<HashMap<Address, Bytes>> {
323    let mut contracts_bytecode = HashMap::default();
324    if let Some(ref traces) = result.traces {
325        let addresses = gather_trace_addresses(traces);
326        let results = futures::future::join_all(addresses.into_iter().map(async |a| {
327            (
328                a,
329                provider.get_code_at(a).await.unwrap_or_else(|e| {
330                    sh_warn!("Failed to fetch code for {a:?}: {e:?}").ok();
331                    Bytes::new()
332                }),
333            )
334        }))
335        .await;
336        for (address, code) in results {
337            if !code.is_empty() {
338                contracts_bytecode.insert(address, code);
339            }
340        }
341    }
342    Ok(contracts_bytecode)
343}
344
345fn gather_trace_addresses(traces: &Traces) -> HashSet<Address> {
346    let mut addresses = HashSet::default();
347    for (_, trace) in traces {
348        for node in trace.arena.nodes() {
349            if !node.trace.address.is_zero() {
350                addresses.insert(node.trace.address);
351            }
352            if !node.trace.caller.is_zero() {
353                addresses.insert(node.trace.caller);
354            }
355        }
356    }
357    addresses
358}
359
360impl figment::Provider for RunArgs {
361    fn metadata(&self) -> Metadata {
362        Metadata::named("RunArgs")
363    }
364
365    fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
366        let mut map = Map::new();
367
368        if let Some(api_key) = &self.etherscan.key {
369            map.insert("etherscan_api_key".into(), api_key.as_str().into());
370        }
371
372        if let Some(evm_version) = self.evm_version {
373            map.insert("evm_version".into(), figment::value::Value::serialize(evm_version)?);
374        }
375
376        Ok(Map::from([(Config::selected_profile(), map)]))
377    }
378}