Skip to content

Commit

Permalink
Encode constructor args
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Oct 23, 2024
1 parent 261d82f commit d38086c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion crates/cast/bin/cmd/constructor_args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_dyn_abi::DynSolType;
use alloy_primitives::{Address, Bytes};
use clap::{command, Parser};
use eyre::{eyre, OptionExt, Result};
Expand Down Expand Up @@ -87,9 +88,17 @@ async fn parse_constructor_args(
.enumerate()
.map(|(i, arg)| {
let arg = arg.to_vec();
format!("{} {}", constructor.inputs[i].ty, Bytes::from(arg))
format_arg(&constructor.inputs[i].ty, arg).expect("Failed to format argument.")
})
.collect();

Ok(display_args)
}

fn format_arg(ty: &str, arg: Vec<u8>) -> Result<String> {
let arg_type: DynSolType = ty.parse().expect("Invalid ABI type.");
let bytes = Bytes::from(arg.clone());
let decoded = arg_type.abi_decode(&arg)?;

Ok(format!("{bytes} -> {decoded:?}"))
}
6 changes: 3 additions & 3 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,10 +1368,10 @@ casttest!(fetch_creation_code_only_args_from_etherscan, |_prj, cmd| {

// tests that displays a sample contract creation args
// <https://etherscan.io/address/0x0923cad07f06b2d0e5e49e63b8b35738d4156b95>
casttest!(fetch_creation_args_from_etherscan, |_prj, cmd| {
casttest!(fetch_constructor_args_from_etherscan, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
cmd.args([
"creation-args",
"constructor-args",
"--etherscan-api-key",
&next_mainnet_etherscan_api_key(),
"0x6982508145454ce325ddbe47a25d4ec3d2311933",
Expand All @@ -1380,7 +1380,7 @@ casttest!(fetch_creation_args_from_etherscan, |_prj, cmd| {
])
.assert_success()
.stdout_eq(str![[r#"
uint256 0x00000000000000000000000000000000000014bddab3e51a57cff87a50000000
0x00000000000000000000000000000000000014bddab3e51a57cff87a50000000 -> Uint(420690000000000000000000000000000, 256)
"#]]);
});

0 comments on commit d38086c

Please sign in to comment.