Skip to content

Commit 473dd32

Browse files
grandizzyrplusq
authored andcommitted
feat(cast): add decode-event sig data (foundry-rs#9413)
1 parent 129b8aa commit 473dd32

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

crates/cast/bin/args.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ pub enum CastSubcommand {
512512
///
513513
/// Similar to `abi-decode --input`, but function selector MUST be prefixed in `calldata`
514514
/// string
515-
#[command(visible_aliases = &["--calldata-decode", "cdd"])]
516-
CalldataDecode {
515+
#[command(visible_aliases = &["calldata-decode", "--calldata-decode", "cdd"])]
516+
DecodeCalldata {
517517
/// The function signature in the format `<name>(<in-types>)(<out-types>)`.
518518
sig: String,
519519

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

533+
/// Decode event data.
534+
#[command(visible_aliases = &["event-decode", "--event-decode", "ed"])]
535+
DecodeEvent {
536+
/// The event signature.
537+
sig: String,
538+
/// The event data to decode.
539+
data: String,
540+
},
541+
533542
/// Decode ABI-encoded input or output data.
534543
///
535544
/// Defaults to decoding output data. To decode input data pass --input.
536545
///
537546
/// When passing `--input`, function selector must NOT be prefixed in `calldata` string
538-
#[command(name = "abi-decode", visible_aliases = &["ad", "--abi-decode"])]
539-
AbiDecode {
547+
#[command(name = "decode-abi", visible_aliases = &["abi-decode", "--abi-decode", "ad"])]
548+
DecodeAbi {
540549
/// The function signature in the format `<name>(<in-types>)(<out-types>)`.
541550
sig: String,
542551

crates/cast/bin/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate tracing;
33

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

191191
// ABI encoding & decoding
192-
CastSubcommand::AbiDecode { sig, calldata, input } => {
192+
CastSubcommand::DecodeAbi { sig, calldata, input } => {
193193
let tokens = SimpleCast::abi_decode(&sig, &calldata, input)?;
194194
print_tokens(&tokens);
195195
}
@@ -200,17 +200,22 @@ async fn main_args(args: CastArgs) -> Result<()> {
200200
sh_println!("{}", SimpleCast::abi_encode_packed(&sig, &args)?)?
201201
}
202202
}
203-
CastSubcommand::CalldataDecode { sig, calldata } => {
203+
CastSubcommand::DecodeCalldata { sig, calldata } => {
204204
let tokens = SimpleCast::calldata_decode(&sig, &calldata, true)?;
205205
print_tokens(&tokens);
206206
}
207207
CastSubcommand::CalldataEncode { sig, args } => {
208208
sh_println!("{}", SimpleCast::calldata_encode(sig, &args)?)?;
209209
}
210-
CastSubcommand::StringDecode { data } => {
210+
CastSubcommand::DecodeString { data } => {
211211
let tokens = SimpleCast::calldata_decode("Any(string)", &data, true)?;
212212
print_tokens(&tokens);
213213
}
214+
CastSubcommand::DecodeEvent { sig, data } => {
215+
let event = get_event(sig.as_str())?;
216+
let decoded_event = event.decode_log_parts(None, &hex::decode(data)?, false)?;
217+
print_tokens(&decoded_event.body);
218+
}
214219
CastSubcommand::Interface(cmd) => cmd.run().await?,
215220
CastSubcommand::CreationCode(cmd) => cmd.run().await?,
216221
CastSubcommand::ConstructorArgs(cmd) => cmd.run().await?,

crates/cast/tests/cli/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,14 @@ casttest!(string_decode, |_prj, cmd| {
15391539
"#]]);
15401540
});
15411541

1542+
casttest!(event_decode, |_prj, cmd| {
1543+
cmd.args(["decode-event", "MyEvent(uint256,address)", "0x000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000d0004f"]).assert_success().stdout_eq(str![[r#"
1544+
78
1545+
0x0000000000000000000000000000000000D0004F
1546+
1547+
"#]]);
1548+
});
1549+
15421550
casttest!(format_units, |_prj, cmd| {
15431551
cmd.args(["format-units", "1000000", "6"]).assert_success().stdout_eq(str![[r#"
15441552
1

0 commit comments

Comments
 (0)