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/selectors): add optional selectors resolving #6864

Merged
merged 1 commit into from
Jan 20, 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
31 changes: 25 additions & 6 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use foundry_common::{
fmt::format_tokens,
fs,
selectors::{
decode_calldata, decode_event_topic, decode_function_selector, import_selectors,
parse_signatures, pretty_calldata, ParsedSignatures, SelectorImportData,
decode_calldata, decode_event_topic, decode_function_selector, decode_selectors,
import_selectors, parse_signatures, pretty_calldata, ParsedSignatures, SelectorImportData,
SelectorType,
},
types::{ToAlloy, ToEthers},
};
Expand Down Expand Up @@ -269,10 +270,28 @@ async fn main() -> Result<()> {
Subcommands::Disassemble { bytecode } => {
println!("{}", SimpleCast::disassemble(&bytecode)?);
}
Subcommands::Selectors { bytecode } => {
let s = SimpleCast::extract_selectors(&bytecode)?;
let v: Vec<_> = s.into_iter().map(|r| format!("{}\t{}", r.0, r.1)).collect();
println!("{}", v.join("\n"));
Subcommands::Selectors { bytecode, resolve } => {
let selectors_and_args = SimpleCast::extract_selectors(&bytecode)?;
if resolve {
let selectors_it = selectors_and_args.iter().map(|r| &r.0);
let resolve_results =
decode_selectors(SelectorType::Function, selectors_it).await?;

let max_args_len = selectors_and_args.iter().map(|r| r.1.len()).max().unwrap_or(0);
for ((selector, arguments), func_names) in
selectors_and_args.into_iter().zip(resolve_results.into_iter())
{
let resolved = match func_names {
Some(v) => v.join("|"),
None => "".to_string(),
};
println!("{selector}\t{arguments:max_args_len$}\t{resolved}");
}
} else {
for (selector, arguments) in selectors_and_args {
println!("{selector}\t{arguments}");
}
}
}
Subcommands::FindBlock(cmd) => cmd.run().await?,
Subcommands::GasPrice { rpc } => {
Expand Down
9 changes: 8 additions & 1 deletion crates/cast/bin/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,14 @@ pub enum Subcommands {

/// Extracts function selectors and arguments from bytecode
#[clap(visible_alias = "sel")]
Selectors { bytecode: String },
Selectors {
/// The hex encoded bytecode.
bytecode: String,

/// Resolve the function signatures for the extracted selectors using https://openchain.xyz
#[clap(long, short)]
resolve: bool,
},
}

/// CLI arguments for `cast --to-base`.
Expand Down