Skip to main content

foundry_cli/utils/
abi.rs

1use alloy_chains::Chain;
2use alloy_ens::NameOrAddress;
3use alloy_json_abi::Function;
4use alloy_primitives::{Address, hex};
5use alloy_provider::{Network, Provider};
6use eyre::{OptionExt, Result};
7use foundry_common::abi::{
8    encode_function_args, encode_function_args_raw, get_func, get_func_etherscan,
9};
10use futures::future::join_all;
11
12async fn resolve_name_args<N: Network, P: Provider<N>>(
13    args: &[String],
14    provider: &P,
15) -> Vec<String> {
16    join_all(args.iter().map(|arg| async {
17        if arg.contains('.') {
18            let addr = NameOrAddress::Name(arg.clone()).resolve(provider).await;
19            match addr {
20                Ok(addr) => addr.to_string(),
21                Err(_) => arg.clone(),
22            }
23        } else {
24            arg.clone()
25        }
26    }))
27    .await
28}
29
30pub async fn parse_function_args<N: Network, P: Provider<N>>(
31    sig: &str,
32    args: Vec<String>,
33    to: Option<Address>,
34    chain: Chain,
35    provider: &P,
36    etherscan_api_key: Option<&str>,
37    etherscan_api_url: Option<&str>,
38) -> Result<(Vec<u8>, Option<Function>)> {
39    if sig.trim().is_empty() {
40        eyre::bail!("Function signature or calldata must be provided.")
41    }
42
43    let args = resolve_name_args(&args, provider).await;
44
45    // Try to decode as hex calldata first, otherwise treat as function signature
46    if let Ok(data) = hex::decode(sig) {
47        return Ok((data, None));
48    } else if sig.starts_with("0x") || sig.starts_with("0X") {
49        let e = hex::decode(sig).unwrap_err();
50        eyre::bail!("Invalid hex calldata '{}': {e}", sig);
51    }
52
53    let func = if sig.contains('(') {
54        // a regular function signature with parentheses
55        get_func(sig)?
56    } else {
57        info!(
58            "function signature does not contain parentheses, fetching function data from Etherscan"
59        );
60        let etherscan_api_key = etherscan_api_key.ok_or_eyre(
61            "Function signature does not contain parentheses. If you wish to fetch function data from Etherscan, please provide an API key.",
62        )?;
63        let to = to.ok_or_eyre("A 'to' address must be provided to fetch function data.")?;
64        get_func_etherscan(sig, to, &args, chain, etherscan_api_key, etherscan_api_url).await?
65    };
66
67    if to.is_none() {
68        // if this is a CREATE call we must exclude the (constructor) function selector: https://github.com/foundry-rs/foundry/issues/10947
69        Ok((encode_function_args_raw(&func, &args)?, Some(func)))
70    } else {
71        Ok((encode_function_args(&func, &args)?, Some(func)))
72    }
73}