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) -> Result<(Vec<u8>, Option<Function>)> {
38    if sig.trim().is_empty() {
39        eyre::bail!("Function signature or calldata must be provided.")
40    }
41
42    let args = resolve_name_args(&args, provider).await;
43
44    // Try to decode as hex calldata first, otherwise treat as function signature
45    if let Ok(data) = hex::decode(sig) {
46        return Ok((data, None));
47    } else if sig.starts_with("0x") || sig.starts_with("0X") {
48        let e = hex::decode(sig).unwrap_err();
49        eyre::bail!("Invalid hex calldata '{}': {e}", sig);
50    }
51
52    let func = if sig.contains('(') {
53        // a regular function signature with parentheses
54        get_func(sig)?
55    } else {
56        info!(
57            "function signature does not contain parentheses, fetching function data from Etherscan"
58        );
59        let etherscan_api_key = etherscan_api_key.ok_or_eyre(
60            "Function signature does not contain parentheses. If you wish to fetch function data from Etherscan, please provide an API key.",
61        )?;
62        let to = to.ok_or_eyre("A 'to' address must be provided to fetch function data.")?;
63        get_func_etherscan(sig, to, &args, chain, etherscan_api_key).await?
64    };
65
66    if to.is_none() {
67        // if this is a CREATE call we must exclude the (constructor) function selector: https://github.com/foundry-rs/foundry/issues/10947
68        Ok((encode_function_args_raw(&func, &args)?, Some(func)))
69    } else {
70        Ok((encode_function_args(&func, &args)?, Some(func)))
71    }
72}