//! ABI related helper functions. use alloy_chains::Chain; use alloy_dyn_abi::{DecodedEvent, DynSolType, DynSolValue, FunctionExt, JsonAbiExt, Specifier}; use alloy_json_abi::{Error, Event, Function, Param}; use alloy_primitives::{Address, LogData, hex}; use eyre::{Context, ContextCompat, Result}; use foundry_block_explorers::{Client, contract::ContractMetadata, errors::EtherscanError}; use std::pin::Pin; pub fn encode_args(inputs: &[Param], args: I) -> Result> where I: IntoIterator, S: AsRef, { let args: Vec = args.into_iter().collect(); if inputs.len() != args.len() { eyre::bail!("encode length mismatch: expected {} types, got {}", inputs.len(), args.len()); } std::iter::zip(inputs, args) .map(|(input, arg)| coerce_value(&input.selector_type(), arg.as_ref())) .collect() } /// Given a function and a vector of string arguments, it proceeds to convert the args to alloy /// [DynSolValue]s and then ABI encode them, prefixes the encoded data with the function selector. pub fn encode_function_args(func: &Function, args: I) -> Result> where I: IntoIterator, S: AsRef, { Ok(func.abi_encode_input(&encode_args(&func.inputs, args)?)?) } /// Given a function and a vector of string arguments, it proceeds to convert the args to alloy /// [DynSolValue]s and then ABI encode them. Doesn't prefix the function selector. pub fn encode_function_args_raw(func: &Function, args: I) -> Result> where I: IntoIterator, S: AsRef, { Ok(func.abi_encode_input_raw(&encode_args(&func.inputs, args)?)?) } /// Given a function and a vector of string arguments, it proceeds to convert the args to alloy /// [DynSolValue]s and encode them using the packed encoding. pub fn encode_function_args_packed(func: &Function, args: I) -> Result> where I: IntoIterator, S: AsRef, { let args: Vec = args.into_iter().collect(); if func.inputs.len() != args.len() { eyre::bail!( "encode length mismatch: expected {} types, got {}", func.inputs.len(), args.len(), ); } let params: Vec> = std::iter::zip(&func.inputs, args) .map(|(input, arg)| coerce_value(&input.selector_type(), arg.as_ref())) .collect::>>()? .into_iter() .map(|v| v.abi_encode_packed()) .collect(); Ok(params.concat()) } /// Decodes the calldata of the function pub fn abi_decode_calldata( sig: &str, calldata: &str, input: bool, fn_selector: bool, ) -> Result> { let func = get_func(sig)?; let calldata = hex::decode(calldata)?; let mut calldata = calldata.as_slice(); // If function selector is prefixed in "calldata", remove it (first 4 bytes) if input && fn_selector && calldata.len() >= 4 { calldata = &calldata[4..]; } let res = if input { func.abi_decode_input(calldata) } else { func.abi_decode_output(calldata) }?; // in case the decoding worked but nothing was decoded if res.is_empty() { eyre::bail!("no data was decoded"); } Ok(res) } /// Given a function signature string, it tries to parse it as a `Function` pub fn get_func(sig: &str) -> Result { Function::parse(sig).wrap_err("could not parse function signature") } /// Given an event signature string, it tries to parse it as a `Event` pub fn get_event(sig: &str) -> Result { Event::parse(sig).wrap_err("could not parse event signature") } /// ABI-decodes the non-indexed parameters (the log data) of an event. /// /// Indexed parameters are stored in the log topics rather than in the data, so they are skipped. /// This allows decoding the data of a log without knowing its topics. pub fn abi_decode_event_data(event: &Event, data: &[u8]) -> Result> { let body = DynSolType::Tuple( event .inputs .iter() .filter(|input| !input.indexed) .map(|input| input.resolve()) .collect::>()?, ); Ok(match body.abi_decode_sequence(data)? { DynSolValue::Tuple(values) => values, _ => unreachable!("body is a tuple"), }) } /// Given an error signature string, it tries to parse it as a `Error` pub fn get_error(sig: &str) -> Result { Error::parse(sig).wrap_err("could not parse error signature") } /// Given an event without indexed parameters and a rawlog, it tries to return the event with the /// proper indexed parameters. Otherwise, it returns the original event. pub fn get_indexed_event(mut event: Event, raw_log: &LogData) -> Event { if !event.anonymous && raw_log.topics().len() > 1 { let indexed_params = raw_log.topics().len() - 1; let num_inputs = event.inputs.len(); let num_address_params = event.inputs.iter().filter(|p| p.ty == "address").count(); event.inputs.iter_mut().enumerate().for_each(|(index, param)| { if param.name.is_empty() { param.name = format!("param{index}"); } if num_inputs == indexed_params || (num_address_params == indexed_params && param.ty == "address") { param.indexed = true; } }) } event } /// Restore the order of the params of a decoded event, /// as Alloy returns the indexed and unindexed params separately. pub fn reconstruct_event_params(event: &Event, decoded: &DecodedEvent) -> Vec { let mut indexed = 0; let mut unindexed = 0; let mut inputs = vec![]; for input in &event.inputs { // Prevent panic of event `Transfer(from, to)` decoded with a signature // `Transfer(address indexed from, address indexed to, uint256 indexed tokenId)` by making // sure the event inputs is not higher than decoded indexed / un-indexed values. if input.indexed && indexed < decoded.indexed.len() { inputs.push(decoded.indexed[indexed].clone()); indexed += 1; } else if unindexed < decoded.body.len() { inputs.push(decoded.body[unindexed].clone()); unindexed += 1; } } inputs } /// Fetches the overloads of `function_name` from the contract's Etherscan ABI. If the address is a /// proxy, fetches the ABI of the implementation contract. pub async fn get_funcs_etherscan( function_name: &str, contract: Address, chain: Chain, etherscan_api_key: &str, etherscan_api_url: Option<&str>, ) -> Result> { let client = if let Some(api_url) = etherscan_api_url { Client::builder() .with_api_key(etherscan_api_key) .with_api_url(api_url)? .with_url(api_url)? .build()? } else { Client::new(chain, etherscan_api_key)? }; let source = find_source(client, contract).await?; let metadata = source.items.first().wrap_err("etherscan returned empty metadata")?; let mut abi = metadata.abi()?; Ok(abi.functions.remove(function_name).unwrap_or_default()) } /// If the code at `address` is a proxy, recurse until we find the implementation. pub fn find_source( client: Client, address: Address, ) -> Pin>>> { Box::pin(async move { trace!(%address, "find Etherscan source"); let source = client.contract_source_code(address).await?; let metadata = source.items.first().wrap_err("Etherscan returned no data")?; // `Proxy: 1` can still come with an empty `Implementation` (unresolved/unverified); // treat that like "not a proxy" instead of panicking on `.unwrap()`. let implementation = if metadata.proxy == 0 { None } else { metadata.implementation }; if let Some(implementation) = implementation { sh_println!( "Contract at {address} is a proxy, trying to fetch source at {implementation}..." )?; match find_source(client, implementation).await { impl_source @ Ok(_) => impl_source, Err(e) if matches!( e.downcast_ref::(), Some(EtherscanError::ContractCodeNotVerified(address)) if *address == implementation ) => { error!(%e); Ok(source) } Err(e) => Err(e), } } else { if metadata.proxy != 0 { error!(%address, "Etherscan reports this contract as a proxy but returned no implementation address"); } Ok(source) } }) } /// Helper function to coerce a value to a [DynSolValue] given a type string pub fn coerce_value(ty: &str, arg: &str) -> Result { let ty = DynSolType::parse(ty)?; Ok(DynSolType::coerce_str(&ty, arg)?) } #[cfg(test)] mod tests { use super::*; use alloy_dyn_abi::EventExt; use alloy_primitives::{B256, U256, address}; use foundry_block_explorers::contract::Metadata; /// `Proxy: 1` with an empty `Implementation` used to panic on `.unwrap()` /// (real-world shape, see `foundry_block_explorers`' own `can_deserialize_address_opt` test). #[test] fn test_proxy_without_implementation_does_not_panic() { let json = serde_json::json!({ "SourceCode": "// dummy", "ABI": "[]", "ContractName": "Dummy", "CompilerVersion": "v0.8.0+commit.c7dfd78e", "OptimizationUsed": "0", "Runs": "200", "ConstructorArguments": "", "EVMVersion": "Default", "Library": "", "LicenseType": "None", "Proxy": "1", "Implementation": "", "SwarmSource": "" }); let metadata: Metadata = serde_json::from_value(json).expect("realistic Etherscan payload must deserialize"); // This is exactly the combination that used to reach `.unwrap()` on `None`. assert_eq!(metadata.proxy, 1, "Proxy: 1 must deserialize to a nonzero proxy flag"); assert_eq!( metadata.implementation, None, "an empty Implementation string must deserialize to None, not a parsed address" ); // Must not panic: this is the exact decision `find_source` makes. let implementation = if metadata.proxy == 0 { None } else { metadata.implementation }; assert_eq!(implementation, None); } #[test] fn proxy_implementation_decision_follows_real_implementation() { let json = serde_json::json!({ "SourceCode": "// dummy", "ABI": "[]", "ContractName": "Dummy", "CompilerVersion": "v0.8.0+commit.c7dfd78e", "OptimizationUsed": "0", "Runs": "200", "ConstructorArguments": "", "EVMVersion": "Default", "Library": "", "LicenseType": "None", "Proxy": "1", "Implementation": "0x1F98431c8aD98523631AE4a59f267346ea31F984", "SwarmSource": "" }); let metadata: Metadata = serde_json::from_value(json).unwrap(); let implementation = if metadata.proxy == 0 { None } else { metadata.implementation }; assert_eq!(implementation, Some(address!("0x1F98431c8aD98523631AE4a59f267346ea31F984"))); } #[test] fn test_get_func() { let func = get_func("function foo(uint256 a, uint256 b) returns (uint256)"); assert!(func.is_ok()); let func = func.unwrap(); assert_eq!(func.name, "foo"); assert_eq!(func.inputs.len(), 2); assert_eq!(func.inputs[0].ty, "uint256"); assert_eq!(func.inputs[1].ty, "uint256"); // Stripped down function, which [Function] can parse. let func = get_func("foo(bytes4 a, uint8 b)(bytes4)"); assert!(func.is_ok()); let func = func.unwrap(); assert_eq!(func.name, "foo"); assert_eq!(func.inputs.len(), 2); assert_eq!(func.inputs[0].ty, "bytes4"); assert_eq!(func.inputs[1].ty, "uint8"); assert_eq!(func.outputs[0].ty, "bytes4"); } #[test] fn test_indexed_only_address() { let event = get_event("event Ev(address,uint256,address)").unwrap(); let param0 = B256::random(); let param1 = vec![3; 32]; let param2 = B256::random(); let log = LogData::new_unchecked(vec![event.selector(), param0, param2], param1.into()); let event = get_indexed_event(event, &log); assert_eq!(event.inputs.len(), 3); // Only the address fields get indexed since total_params > num_indexed_params let parsed = event.decode_log(&log).unwrap(); assert_eq!(event.inputs.iter().filter(|param| param.indexed).count(), 2); assert_eq!(parsed.indexed[0], DynSolValue::Address(Address::from_word(param0))); assert_eq!(parsed.body[0], DynSolValue::Uint(U256::from_be_bytes([3; 32]), 256)); assert_eq!(parsed.indexed[1], DynSolValue::Address(Address::from_word(param2))); } #[test] fn test_indexed_all() { let event = get_event("event Ev(address,uint256,address)").unwrap(); let param0 = B256::random(); let param1 = vec![3; 32]; let param2 = B256::random(); let log = LogData::new_unchecked( vec![event.selector(), param0, B256::from_slice(¶m1), param2], vec![].into(), ); let event = get_indexed_event(event, &log); assert_eq!(event.inputs.len(), 3); // All parameters get indexed since num_indexed_params == total_params assert_eq!(event.inputs.iter().filter(|param| param.indexed).count(), 3); let parsed = event.decode_log(&log).unwrap(); assert_eq!(parsed.indexed[0], DynSolValue::Address(Address::from_word(param0))); assert_eq!(parsed.indexed[1], DynSolValue::Uint(U256::from_be_bytes([3; 32]), 256)); assert_eq!(parsed.indexed[2], DynSolValue::Address(Address::from_word(param2))); } #[test] fn test_abi_decode_event_data_ignores_indexed() { let event = get_event("event Ev(uint256 indexed a, string b, address indexed c, uint256 d)") .unwrap(); let data = DynSolValue::Tuple(vec![ DynSolValue::String("hello".into()), DynSolValue::Uint(U256::from(42), 256), ]) .abi_encode_params(); let decoded = abi_decode_event_data(&event, &data).unwrap(); assert_eq!( decoded, vec![DynSolValue::String("hello".into()), DynSolValue::Uint(U256::from(42), 256)] ); } #[test] fn test_abi_decode_event_data_without_indexed() { let event = get_event("event Ev(uint256 a, address b)").unwrap(); let addr = Address::random(); let data = DynSolValue::Tuple(vec![DynSolValue::Uint(U256::ONE, 256), DynSolValue::Address(addr)]) .abi_encode_params(); let decoded = abi_decode_event_data(&event, &data).unwrap(); assert_eq!(decoded, vec![DynSolValue::Uint(U256::ONE, 256), DynSolValue::Address(addr)]); } #[test] fn test_encode_args_length_validation() { let params = vec![ Param { name: "a".to_string(), ty: "uint256".to_string(), internal_type: None, components: vec![], }, Param { name: "b".to_string(), ty: "address".to_string(), internal_type: None, components: vec![], }, ]; // Less arguments than parameters let args = vec!["1"]; let res = encode_args(¶ms, &args); assert!(res.is_err()); assert!(format!("{}", res.unwrap_err()).contains("encode length mismatch")); // Exact number of arguments and parameters let args = vec!["1", "0x0000000000000000000000000000000000000001"]; let res = encode_args(¶ms, &args); assert!(res.is_ok()); let values = res.unwrap(); assert_eq!(values.len(), 2); // More arguments than parameters let args = vec!["1", "0x0000000000000000000000000000000000000001", "extra"]; let res = encode_args(¶ms, &args); assert!(res.is_err()); assert!(format!("{}", res.unwrap_err()).contains("encode length mismatch")); } }