Skip to content

Commit

Permalink
Merge branch 'master' into issue-4072
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Sep 4, 2024
2 parents 4cc4cc5 + eddb33a commit 1a2cc05
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ tempfile.workspace = true
tokio = { workspace = true, features = ["macros", "signal"] }
tracing.workspace = true
yansi.workspace = true
evmole = "0.3.1"
evmole = "0.4.1"

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }
Expand Down
33 changes: 16 additions & 17 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,24 @@ async fn main() -> Result<()> {
println!("{}", SimpleCast::disassemble(&bytecode)?);
}
CastSubcommand::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 functions = SimpleCast::extract_functions(&bytecode)?;
let max_args_len = functions.iter().map(|r| r.1.len()).max().unwrap_or(0);
let max_mutability_len = functions.iter().map(|r| r.2.len()).max().unwrap_or(0);

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 => String::new(),
};
println!("{selector}\t{arguments:max_args_len$}\t{resolved}");
}
let resolve_results = if resolve {
let selectors_it = functions.iter().map(|r| &r.0);
let ds = decode_selectors(SelectorType::Function, selectors_it).await?;
ds.into_iter().map(|v| v.unwrap_or_default().join("|")).collect()
} else {
for (selector, arguments) in selectors_and_args {
println!("{selector}\t{arguments}");
vec![]
};
for (pos, (selector, arguments, state_mutability)) in functions.into_iter().enumerate()
{
if resolve {
let resolved = &resolve_results[pos];
println!("{selector}\t{arguments:max_args_len$}\t{state_mutability:max_mutability_len$}\t{resolved}");
} else {
println!("{selector}\t{arguments:max_args_len$}\t{state_mutability}");
}
}
}
Expand Down
31 changes: 18 additions & 13 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ impl SimpleCast {
pub fn to_ascii(hex: &str) -> Result<String> {
let bytes = hex::decode(hex)?;
if !bytes.iter().all(u8::is_ascii) {
return Err(eyre::eyre!("Invalid ASCII bytes"))
return Err(eyre::eyre!("Invalid ASCII bytes"));
}
Ok(String::from_utf8(bytes).unwrap())
}
Expand Down Expand Up @@ -1405,7 +1405,7 @@ impl SimpleCast {
let base_in = Base::unwrap_or_detect(base_in, value)?;
let base_out: Base = base_out.parse()?;
if base_in == base_out {
return Ok(value.to_string())
return Ok(value.to_string());
}

let mut n = NumberWithBase::parse_int(value, Some(&base_in.to_string()))?;
Expand Down Expand Up @@ -1469,7 +1469,7 @@ impl SimpleCast {
let s = if let Some(stripped) = s.strip_prefix("000000000000000000000000") {
stripped
} else {
return Err(eyre::eyre!("Not convertible to address, there are non-zero bytes"))
return Err(eyre::eyre!("Not convertible to address, there are non-zero bytes"));
};

let lowercase_address_string = format!("0x{s}");
Expand Down Expand Up @@ -1925,7 +1925,7 @@ impl SimpleCast {
}
if optimize == 0 {
let selector = get_func(signature)?.selector();
return Ok((selector.to_string(), String::from(signature)))
return Ok((selector.to_string(), String::from(signature)));
}
let Some((name, params)) = signature.split_once('(') else {
eyre::bail!("invalid function signature");
Expand All @@ -1947,7 +1947,7 @@ impl SimpleCast {

if selector.iter().take_while(|&&byte| byte == 0).count() == optimize {
found.store(true, Ordering::Relaxed);
return Some((nonce, hex::encode_prefixed(selector), input))
return Some((nonce, hex::encode_prefixed(selector), input));
}

nonce += nonce_step;
Expand All @@ -1961,24 +1961,29 @@ impl SimpleCast {
}
}

/// Extracts function selectors and arguments from bytecode
/// Extracts function selectors, arguments and state mutability from bytecode
///
/// # Example
///
/// ```
/// use cast::SimpleCast as Cast;
///
/// let bytecode = "6080604052348015600e575f80fd5b50600436106026575f3560e01c80632125b65b14602a575b5f80fd5b603a6035366004603c565b505050565b005b5f805f60608486031215604d575f80fd5b833563ffffffff81168114605f575f80fd5b925060208401356001600160a01b03811681146079575f80fd5b915060408401356001600160e01b03811681146093575f80fd5b80915050925092509256";
/// let selectors = Cast::extract_selectors(bytecode)?;
/// assert_eq!(selectors, vec![("0x2125b65b".to_string(), "uint32,address,uint224".to_string())]);
/// let functions = Cast::extract_functions(bytecode)?;
/// assert_eq!(functions, vec![("0x2125b65b".to_string(), "uint32,address,uint224".to_string(), "pure")]);
/// # Ok::<(), eyre::Report>(())
/// ```
pub fn extract_selectors(bytecode: &str) -> Result<Vec<(String, String)>> {
pub fn extract_functions(bytecode: &str) -> Result<Vec<(String, String, &str)>> {
let code = hex::decode(strip_0x(bytecode))?;
let s = evmole::function_selectors(&code, 0);

Ok(s.iter()
.map(|s| (hex::encode_prefixed(s), evmole::function_arguments(&code, s, 0)))
Ok(evmole::function_selectors(&code, 0)
.into_iter()
.map(|s| {
(
hex::encode_prefixed(s),
evmole::function_arguments(&code, &s, 0),
evmole::function_state_mutability(&code, &s, 0),
)
})
.collect())
}

Expand Down
12 changes: 6 additions & 6 deletions crates/forge/bin/cmd/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ impl BindArgs {
let _ = ProjectCompiler::new().compile(&project)?;
}

if !self.alloy {
if self.ethers {
eprintln!(
"Warning: `--ethers` (default) bindings are deprecated and will be removed in the future. \
Consider using `--alloy` instead."
"Warning: `--ethers` bindings are deprecated and will be removed in the future. \
Consider using `--alloy` (default) instead."
);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ impl BindArgs {
fn get_json_files(&self, artifacts: &Path) -> Result<impl Iterator<Item = (String, PathBuf)>> {
let filter = self.get_filter()?;
let alloy_filter = self.get_alloy_filter()?;
let is_alloy = self.alloy;
let is_alloy = !self.ethers;
Ok(json_files(artifacts)
.filter_map(|path| {
// Ignore the build info JSON.
Expand Down Expand Up @@ -264,7 +264,7 @@ impl BindArgs {

/// Check that the existing bindings match the expected abigen output
fn check_existing_bindings(&self, artifacts: &Path, bindings_root: &Path) -> Result<()> {
if !self.alloy {
if self.ethers {
return self.check_ethers(artifacts, bindings_root);
}

Expand Down Expand Up @@ -316,7 +316,7 @@ impl BindArgs {

/// Generate the bindings
fn generate_bindings(&self, artifacts: &Path, bindings_root: &Path) -> Result<()> {
if !self.alloy {
if self.ethers {
return self.generate_ethers(artifacts, bindings_root);
}

Expand Down

0 comments on commit 1a2cc05

Please sign in to comment.