Skip to content

Commit

Permalink
feat(cast): add string-decode to decode string (foundry-rs#9237)
Browse files Browse the repository at this point in the history
* feat(cast): add error-decode to decode error string

Signed-off-by: jsvisa <delweng@gmail.com>

* feat(cast): error-decode -> string-decode

Signed-off-by: jsvisa <delweng@gmail.com>

* add test case for string-decode

Signed-off-by: jsvisa <delweng@gmail.com>

* Apply suggestions from code review

---------

Signed-off-by: jsvisa <delweng@gmail.com>
Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
  • Loading branch information
2 people authored and rplusq committed Nov 29, 2024
1 parent 748f9fb commit 2a2bfec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/cast/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ pub enum CastSubcommand {
///
/// Similar to `abi-decode --input`, but function selector MUST be prefixed in `calldata`
/// string
#[command(visible_aliases = &["--calldata-decode","cdd"])]
#[command(visible_aliases = &["--calldata-decode", "cdd"])]
CalldataDecode {
/// The function signature in the format `<name>(<in-types>)(<out-types>)`.
sig: String,
Expand All @@ -536,6 +536,19 @@ pub enum CastSubcommand {
json: bool,
},

/// Decode ABI-encoded string.
///
/// Similar to `calldata-decode --input`, but the function argument is a `string`
#[command(visible_aliases = &["--string-decode", "sd"])]
StringDecode {
/// The ABI-encoded string.
data: String,

/// Print the decoded string as JSON.
#[arg(long, short, help_heading = "Display options")]
json: bool,
},

/// Decode ABI-encoded input or output data.
///
/// Defaults to decoding output data. To decode input data pass --input.
Expand Down
4 changes: 4 additions & 0 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ async fn main_args(args: CastArgs) -> Result<()> {
CastSubcommand::CalldataEncode { sig, args } => {
sh_println!("{}", SimpleCast::calldata_encode(sig, &args)?)?;
}
CastSubcommand::StringDecode { data, json } => {
let tokens = SimpleCast::calldata_decode("Any(string)", &data, true)?;
print_tokens(&tokens, json)
}
CastSubcommand::Interface(cmd) => cmd.run().await?,
CastSubcommand::CreationCode(cmd) => cmd.run().await?,
CastSubcommand::ConstructorArgs(cmd) => cmd.run().await?,
Expand Down
7 changes: 7 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,13 @@ casttest!(parse_units, |_prj, cmd| {
"#]]);
});

casttest!(string_decode, |_prj, cmd| {
cmd.args(["string-decode", "0x88c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000054753303235000000000000000000000000000000000000000000000000000000"]).assert_success().stdout_eq(str![[r#"
"GS025"
"#]]);
});

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

0 comments on commit 2a2bfec

Please sign in to comment.