Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cast): add decode-event sig data #9413

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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