Skip to content

Commit

Permalink
feat(cast): add decode-event sig data (#9413)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Nov 26, 2024
1 parent 0045384 commit 31dd1f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
21 changes: 15 additions & 6 deletions crates/cast/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ pub enum CastSubcommand {
///
/// Similar to `abi-decode --input`, but function selector MUST be prefixed in `calldata`
/// string
#[command(visible_aliases = &["--calldata-decode", "cdd"])]
CalldataDecode {
#[command(visible_aliases = &["calldata-decode", "--calldata-decode", "cdd"])]
DecodeCalldata {
/// The function signature in the format `<name>(<in-types>)(<out-types>)`.
sig: String,

Expand All @@ -524,19 +524,28 @@ pub enum CastSubcommand {
/// Decode ABI-encoded string.
///
/// Similar to `calldata-decode --input`, but the function argument is a `string`
#[command(visible_aliases = &["--string-decode", "sd"])]
StringDecode {
#[command(visible_aliases = &["string-decode", "--string-decode", "sd"])]
DecodeString {
/// The ABI-encoded string.
data: String,
},

/// Decode event data.
#[command(visible_aliases = &["event-decode", "--event-decode", "ed"])]
DecodeEvent {
/// The event signature.
sig: String,
/// The event data to decode.
data: String,
},

/// Decode ABI-encoded input or output data.
///
/// Defaults to decoding output data. To decode input data pass --input.
///
/// When passing `--input`, function selector must NOT be prefixed in `calldata` string
#[command(name = "abi-decode", visible_aliases = &["ad", "--abi-decode"])]
AbiDecode {
#[command(name = "decode-abi", visible_aliases = &["abi-decode", "--abi-decode", "ad"])]
DecodeAbi {
/// The function signature in the format `<name>(<in-types>)(<out-types>)`.
sig: String,

Expand Down
13 changes: 9 additions & 4 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate tracing;

use alloy_dyn_abi::DynSolValue;
use alloy_dyn_abi::{DynSolValue, EventExt};
use alloy_primitives::{eip191_hash_message, hex, keccak256, Address, B256};
use alloy_provider::Provider;
use alloy_rpc_types::{BlockId, BlockNumberOrTag::Latest};
Expand Down Expand Up @@ -189,7 +189,7 @@ async fn main_args(args: CastArgs) -> Result<()> {
}

// ABI encoding & decoding
CastSubcommand::AbiDecode { sig, calldata, input } => {
CastSubcommand::DecodeAbi { sig, calldata, input } => {
let tokens = SimpleCast::abi_decode(&sig, &calldata, input)?;
print_tokens(&tokens);
}
Expand All @@ -200,17 +200,22 @@ async fn main_args(args: CastArgs) -> Result<()> {
sh_println!("{}", SimpleCast::abi_encode_packed(&sig, &args)?)?
}
}
CastSubcommand::CalldataDecode { sig, calldata } => {
CastSubcommand::DecodeCalldata { sig, calldata } => {
let tokens = SimpleCast::calldata_decode(&sig, &calldata, true)?;
print_tokens(&tokens);
}
CastSubcommand::CalldataEncode { sig, args } => {
sh_println!("{}", SimpleCast::calldata_encode(sig, &args)?)?;
}
CastSubcommand::StringDecode { data } => {
CastSubcommand::DecodeString { data } => {
let tokens = SimpleCast::calldata_decode("Any(string)", &data, true)?;
print_tokens(&tokens);
}
CastSubcommand::DecodeEvent { sig, data } => {
let event = get_event(sig.as_str())?;
let decoded_event = event.decode_log_parts(None, &hex::decode(data)?, false)?;
print_tokens(&decoded_event.body);
}
CastSubcommand::Interface(cmd) => cmd.run().await?,
CastSubcommand::CreationCode(cmd) => cmd.run().await?,
CastSubcommand::ConstructorArgs(cmd) => cmd.run().await?,
Expand Down
8 changes: 8 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,14 @@ casttest!(string_decode, |_prj, cmd| {
"#]]);
});

casttest!(event_decode, |_prj, cmd| {
cmd.args(["decode-event", "MyEvent(uint256,address)", "0x000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000d0004f"]).assert_success().stdout_eq(str![[r#"
78
0x0000000000000000000000000000000000D0004F
"#]]);
});

casttest!(format_units, |_prj, cmd| {
cmd.args(["format-units", "1000000", "6"]).assert_success().stdout_eq(str![[r#"
1
Expand Down

0 comments on commit 31dd1f7

Please sign in to comment.