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 string-decode to decode string #9237

Merged
merged 5 commits into from
Oct 31, 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
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 @@ -1417,6 +1417,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
Loading