cast/cmd/
run.rs

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