Skip to main content

foundry_cli/utils/
cmd.rs

1use alloy_json_abi::JsonAbi;
2use eyre::{Result, WrapErr};
3use foundry_common::{TestFunctionExt, fs, fs::json_files, selectors::SelectorKind, shell};
4use foundry_compilers::{
5    Artifact, ArtifactId, ProjectCompileOutput, artifacts::CompactBytecode, utils::read_json_file,
6};
7use foundry_config::{Chain, Config, NamedChain, error::ExtractConfigError, figment::Figment};
8use foundry_evm::{
9    core::evm::FoundryEvmNetwork,
10    executors::{DeployResult, EvmError, RawCallResult},
11    opts::EvmOpts,
12    traces::{
13        CallTraceDecoder, TraceKind, Traces, decode_trace_arena, identifier::SignaturesCache,
14        prune_trace_depth, render_trace_arena_inner,
15    },
16};
17use std::{
18    fmt::Write,
19    path::{Path, PathBuf},
20};
21use yansi::Paint;
22
23/// Given a `Project`'s output, finds the contract by path and name and returns its
24/// ABI, creation bytecode, and `ArtifactId`.
25#[track_caller]
26pub fn find_contract_artifacts(
27    output: ProjectCompileOutput,
28    path: &Path,
29    name: &str,
30) -> Result<(JsonAbi, CompactBytecode, ArtifactId)> {
31    let mut other = Vec::new();
32    let Some((id, contract)) = output.into_artifacts().find_map(|(id, artifact)| {
33        if id.name == name && id.source == path {
34            Some((id, artifact))
35        } else {
36            other.push(id.name);
37            None
38        }
39    }) else {
40        let mut err = format!("could not find artifact: `{name}`");
41        if let Some(suggestion) = super::did_you_mean(name, other).pop()
42            && suggestion != name
43        {
44            err = format!(
45                r#"{err}
46
47        Did you mean `{suggestion}`?"#
48            );
49        }
50        eyre::bail!(err)
51    };
52
53    let abi = contract
54        .get_abi()
55        .ok_or_else(|| eyre::eyre!("contract {} does not contain abi", name))?
56        .into_owned();
57
58    let bin = contract
59        .get_bytecode()
60        .ok_or_else(|| eyre::eyre!("contract {} does not contain bytecode", name))?
61        .into_owned();
62
63    Ok((abi, bin, id))
64}
65
66/// Returns error if constructor has arguments.
67pub fn ensure_clean_constructor(abi: &JsonAbi) -> Result<()> {
68    if let Some(constructor) = &abi.constructor
69        && !constructor.inputs.is_empty()
70    {
71        eyre::bail!(
72            "Contract constructor should have no arguments. Add those arguments to  `run(...)` instead, and call it with `--sig run(...)`."
73        );
74    }
75    Ok(())
76}
77
78pub fn needs_setup(abi: &JsonAbi) -> bool {
79    let setup_fns: Vec<_> = abi.functions().filter(|func| func.name.is_setup()).collect();
80
81    for setup_fn in &setup_fns {
82        if setup_fn.name != "setUp" {
83            let _ = sh_warn!(
84                "Found invalid setup function \"{}\" did you mean \"setUp()\"?",
85                setup_fn.signature()
86            );
87        }
88    }
89
90    setup_fns.len() == 1 && setup_fns[0].name == "setUp"
91}
92
93pub fn eta_key(state: &indicatif::ProgressState, f: &mut dyn Write) {
94    write!(f, "{:.1}s", state.eta().as_secs_f64()).unwrap()
95}
96
97pub fn init_progress(len: u64, label: &str) -> indicatif::ProgressBar {
98    let pb = indicatif::ProgressBar::new(len);
99    let mut template =
100        "{prefix}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} "
101            .to_string();
102    write!(template, "{label}").unwrap();
103    template += " ({eta})";
104    pb.set_style(
105        indicatif::ProgressStyle::with_template(&template)
106            .unwrap()
107            .with_key("eta", crate::utils::eta_key)
108            .progress_chars("#>-"),
109    );
110    pb
111}
112
113/// True if the network calculates gas costs differently.
114pub fn has_different_gas_calc(chain_id: u64) -> bool {
115    let chain = Chain::from(chain_id);
116    if let Some(chain) = chain.named() {
117        return chain.is_tempo()
118            || chain.is_arbitrum()
119            || chain.is_elastic()
120            || matches!(
121                chain,
122                NamedChain::Acala
123                    | NamedChain::AcalaMandalaTestnet
124                    | NamedChain::AcalaTestnet
125                    | NamedChain::Etherlink
126                    | NamedChain::EtherlinkShadownet
127                    | NamedChain::Karura
128                    | NamedChain::KaruraTestnet
129                    | NamedChain::Kusama
130                    | NamedChain::Mantle
131                    | NamedChain::MantleSepolia
132                    | NamedChain::MegaEth
133                    | NamedChain::MegaEthTestnet
134                    | NamedChain::Metis
135                    | NamedChain::Monad
136                    | NamedChain::MonadTestnet
137                    | NamedChain::Moonbase
138                    | NamedChain::Moonbeam
139                    | NamedChain::MoonbeamDev
140                    | NamedChain::Moonriver
141                    | NamedChain::Polkadot
142                    | NamedChain::PolkadotTestnet
143            );
144    }
145    false
146}
147
148/// True if it supports broadcasting in batches.
149pub fn has_batch_support(chain_id: u64) -> bool {
150    if let Some(chain) = Chain::from(chain_id).named() {
151        return !chain.is_arbitrum();
152    }
153    true
154}
155
156/// Helpers for loading configuration.
157///
158/// This is usually implemented through the macros defined in [`foundry_config`]. See
159/// [`foundry_config::impl_figment_convert`] for more details.
160///
161/// By default each function will emit warnings generated during loading, unless the `_no_warnings`
162/// variant is used.
163pub trait LoadConfig {
164    /// Load the [`Config`] based on the options provided in self.
165    fn figment(&self) -> Figment;
166
167    /// Load and sanitize the [`Config`] based on the options provided in self.
168    fn load_config(&self) -> Result<Config, ExtractConfigError> {
169        self.load_config_no_warnings().inspect(emit_warnings)
170    }
171
172    /// Same as [`LoadConfig::load_config`] but does not emit warnings.
173    fn load_config_no_warnings(&self) -> Result<Config, ExtractConfigError> {
174        self.load_config_unsanitized_no_warnings().map(Config::sanitized)
175    }
176
177    /// Load [`Config`] but do not sanitize. See [`Config::sanitized`] for more information.
178    fn load_config_unsanitized(&self) -> Result<Config, ExtractConfigError> {
179        self.load_config_unsanitized_no_warnings().inspect(emit_warnings)
180    }
181
182    /// Same as [`LoadConfig::load_config_unsanitized`] but also emits warnings generated
183    fn load_config_unsanitized_no_warnings(&self) -> Result<Config, ExtractConfigError> {
184        Config::from_provider(self.figment())
185    }
186
187    /// Load and sanitize the [`Config`], as well as extract [`EvmOpts`] from self
188    fn load_config_and_evm_opts(&self) -> Result<(Config, EvmOpts)> {
189        self.load_config_and_evm_opts_no_warnings().inspect(|(config, _)| emit_warnings(config))
190    }
191
192    /// Same as [`LoadConfig::load_config_and_evm_opts`] but also emits warnings generated
193    fn load_config_and_evm_opts_no_warnings(&self) -> Result<(Config, EvmOpts)> {
194        let figment = self.figment();
195
196        let mut evm_opts = figment.extract::<EvmOpts>().map_err(ExtractConfigError::new)?;
197        let config = Config::from_provider(figment)?.sanitized();
198
199        // update the fork url if it was an alias
200        if let Some(fork_url) = config.get_rpc_url() {
201            trace!(target: "forge::config", ?fork_url, "Update EvmOpts fork url");
202            evm_opts.fork_url = Some(fork_url?.into_owned());
203        }
204
205        Ok((config, evm_opts))
206    }
207}
208
209impl<T> LoadConfig for T
210where
211    for<'a> Figment: From<&'a T>,
212{
213    fn figment(&self) -> Figment {
214        self.into()
215    }
216}
217
218fn emit_warnings(config: &Config) {
219    for warning in &config.warnings {
220        let _ = sh_warn!("{warning}");
221    }
222}
223
224/// Read contract constructor arguments from the given file.
225pub fn read_constructor_args_file(constructor_args_path: PathBuf) -> Result<Vec<String>> {
226    if !constructor_args_path.exists() {
227        eyre::bail!("Constructor args file \"{}\" not found", constructor_args_path.display());
228    }
229    let args = if constructor_args_path.extension() == Some(std::ffi::OsStr::new("json")) {
230        read_json_file(&constructor_args_path).wrap_err(format!(
231            "Constructor args file \"{}\" must encode a json array",
232            constructor_args_path.display(),
233        ))?
234    } else {
235        fs::read_to_string(constructor_args_path)?.split_whitespace().map(str::to_string).collect()
236    };
237    Ok(args)
238}
239
240/// A slimmed down return from the executor used for returning minimal trace + gas metering info
241#[derive(Debug)]
242pub struct TraceResult {
243    pub success: bool,
244    pub traces: Option<Traces>,
245    pub gas_used: u64,
246}
247
248impl TraceResult {
249    /// Create a new [`TraceResult`] from a [`RawCallResult`].
250    pub fn from_raw<FEN: FoundryEvmNetwork>(
251        raw: RawCallResult<FEN>,
252        trace_kind: TraceKind,
253    ) -> Self {
254        let RawCallResult { gas_used, traces, reverted, .. } = raw;
255        Self { success: !reverted, traces: traces.map(|arena| vec![(trace_kind, arena)]), gas_used }
256    }
257}
258
259impl<FEN: FoundryEvmNetwork> From<DeployResult<FEN>> for TraceResult {
260    fn from(result: DeployResult<FEN>) -> Self {
261        Self::from_raw(result.raw, TraceKind::Deployment)
262    }
263}
264
265impl<FEN: FoundryEvmNetwork> TryFrom<Result<DeployResult<FEN>, EvmError<FEN>>> for TraceResult {
266    type Error = EvmError<FEN>;
267
268    fn try_from(value: Result<DeployResult<FEN>, EvmError<FEN>>) -> Result<Self, Self::Error> {
269        match value {
270            Ok(result) => Ok(Self::from(result)),
271            Err(EvmError::Execution(err)) => Ok(Self::from_raw(err.raw, TraceKind::Deployment)),
272            Err(err) => Err(err),
273        }
274    }
275}
276
277impl<FEN: FoundryEvmNetwork> From<RawCallResult<FEN>> for TraceResult {
278    fn from(result: RawCallResult<FEN>) -> Self {
279        Self::from_raw(result, TraceKind::Execution)
280    }
281}
282
283pub async fn print_traces(
284    result: &mut TraceResult,
285    decoder: &CallTraceDecoder,
286    verbose: bool,
287    state_changes: bool,
288    trace_depth: Option<usize>,
289) -> Result<()> {
290    let traces = result.traces.as_mut().expect("No traces found");
291
292    if !shell::is_json() {
293        sh_println!("Traces:")?;
294    }
295
296    for (_, arena) in traces {
297        decode_trace_arena(arena, decoder).await;
298
299        if let Some(trace_depth) = trace_depth {
300            prune_trace_depth(arena, trace_depth);
301        }
302
303        sh_println!("{}", render_trace_arena_inner(arena, verbose, state_changes))?;
304    }
305
306    if shell::is_json() {
307        return Ok(());
308    }
309
310    sh_println!()?;
311    if result.success {
312        sh_println!("{}", "Transaction successfully executed.".green())?;
313    } else {
314        sh_err!("Transaction failed.")?;
315    }
316    sh_println!("Gas used: {}", result.gas_used)?;
317
318    Ok(())
319}
320
321/// Traverse the artifacts in the project to generate local signatures and merge them into the cache
322/// file.
323pub fn cache_local_signatures(output: &ProjectCompileOutput) -> Result<()> {
324    let Some(cache_dir) = Config::foundry_cache_dir() else {
325        eyre::bail!("Failed to get `cache_dir` to generate local signatures.");
326    };
327    let path = cache_dir.join("signatures");
328    let mut signatures = SignaturesCache::load(&path);
329    for (_, artifact) in output.artifacts() {
330        if let Some(abi) = &artifact.abi {
331            signatures.extend_from_abi(abi);
332        }
333
334        // External libraries don't have functions included in the ABI, but `methodIdentifiers`.
335        if let Some(method_identifiers) = &artifact.method_identifiers {
336            signatures.extend(method_identifiers.iter().filter_map(|(signature, selector)| {
337                Some((SelectorKind::Function(selector.parse().ok()?), signature.clone()))
338            }));
339        }
340    }
341    signatures.save(&path);
342    Ok(())
343}
344
345/// Traverses all files at `folder_path`, parses any JSON ABI files found,
346/// and caches their function/event/error signatures to the local signatures cache.
347pub fn cache_signatures_from_abis(folder_path: impl AsRef<Path>) -> Result<()> {
348    let Some(cache_dir) = Config::foundry_cache_dir() else {
349        eyre::bail!("Failed to get `cache_dir` to generate local signatures.");
350    };
351    let path = cache_dir.join("signatures");
352    let mut signatures = SignaturesCache::load(&path);
353
354    json_files(folder_path.as_ref())
355        .filter_map(|path| std::fs::read_to_string(&path).ok())
356        .filter_map(|content| serde_json::from_str::<JsonAbi>(&content).ok())
357        .for_each(|json_abi| signatures.extend_from_abi(&json_abi));
358
359    signatures.save(&path);
360    Ok(())
361}
362
363#[cfg(test)]
364mod tests {
365    use super::*;
366    use std::fs;
367    use tempfile::tempdir;
368
369    #[test]
370    fn test_cache_signatures_from_abis() {
371        let temp_dir = tempdir().unwrap();
372        let abi_json = r#"[
373              {
374                  "type": "function",
375                  "name": "myCustomFunction",
376                  "inputs": [{"name": "amount", "type": "uint256"}],
377                  "outputs": [],
378                  "stateMutability": "nonpayable"
379              },
380              {
381                  "type": "event",
382                  "name": "MyCustomEvent",
383                  "inputs": [{"name": "value", "type": "uint256", "indexed": false}],
384                  "anonymous": false
385              },
386              {
387                  "type": "error",
388                  "name": "MyCustomError",
389                  "inputs": [{"name": "code", "type": "uint256"}]
390              }
391          ]"#;
392
393        let abi_path = temp_dir.path().join("test.json");
394        fs::write(&abi_path, abi_json).unwrap();
395
396        cache_signatures_from_abis(temp_dir.path()).unwrap();
397
398        let cache_dir = Config::foundry_cache_dir().unwrap();
399        let cache_path = cache_dir.join("signatures");
400        let cache = SignaturesCache::load(&cache_path);
401
402        let func_selector: alloy_primitives::Selector = "0x2e2dbaf7".parse().unwrap();
403        assert!(cache.contains_key(&SelectorKind::Function(func_selector)));
404
405        let event_selector: alloy_primitives::B256 =
406            "0x8cc20c47f3a2463817352f75dec0dbf43a7a771b5f6817a92bd5724c1f4aa745".parse().unwrap();
407        assert!(cache.contains_key(&SelectorKind::Event(event_selector)));
408
409        let error_selector: alloy_primitives::Selector = "0xd35f45de".parse().unwrap();
410        assert!(cache.contains_key(&SelectorKind::Error(error_selector)));
411    }
412}