Skip to content

feat(cast): Add file option for calldata input #10397

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

Merged
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: 13 additions & 2 deletions crates/cast/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,19 @@ pub async fn run_command(args: CastArgs) -> Result<()> {
let tokens = SimpleCast::calldata_decode(&sig, &calldata, true)?;
print_tokens(&tokens);
}
CastSubcommand::CalldataEncode { sig, args } => {
sh_println!("{}", SimpleCast::calldata_encode(sig, &args)?)?;
CastSubcommand::CalldataEncode { sig, args, file } => {
let final_args = if let Some(file_path) = file {
let contents = fs::read_to_string(file_path)?;
contents
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(String::from)
.collect()
} else {
args
};
Comment on lines +200 to +209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks alright to me,
having 1 line per arg seems okay

sh_println!("{}", SimpleCast::calldata_encode(sig, &final_args)?)?;
}
CastSubcommand::DecodeString { data } => {
let tokens = SimpleCast::calldata_decode("Any(string)", &data, true)?;
Expand Down
17 changes: 17 additions & 0 deletions crates/cast/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ pub enum CastSubcommand {
/// The arguments to encode.
#[arg(allow_hyphen_values = true)]
args: Vec<String>,

// Path to file containing arguments to encode.
#[arg(long, value_name = "PATH")]
file: Option<PathBuf>,
},

/// Get the symbolic name of the current chain.
Expand Down Expand Up @@ -1147,6 +1151,19 @@ mod tests {
};
}

#[test]
fn parse_call_data_with_file() {
let args: Cast = Cast::parse_from(["foundry-cli", "calldata", "f()", "--file", "test.txt"]);
match args.cmd {
CastSubcommand::CalldataEncode { sig, file, args } => {
assert_eq!(sig, "f()".to_string());
assert_eq!(file, Some(PathBuf::from("test.txt")));
assert!(args.is_empty());
}
_ => unreachable!(),
};
}

// <https://github.com/foundry-rs/book/issues/1019>
#[test]
fn parse_signature() {
Expand Down
Loading