Skip to content

Commit

Permalink
feat: use improved human readable function parser (#2151)
Browse files Browse the repository at this point in the history
* feat: use improved human readable function parser

* chore(clippy): make clippy happy
  • Loading branch information
mattsse authored Jun 29, 2022
1 parent 0c2cecc commit d5530f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
6 changes: 3 additions & 3 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chrono::NaiveDateTime;
use ethers_core::{
abi::{
token::{LenientTokenizer, Tokenizer},
Abi, AbiParser, Token,
Abi, HumanReadableParser, Token,
},
types::{Chain, *},
utils::{self, get_contract_address, keccak256, parse_units, rlp},
Expand Down Expand Up @@ -926,7 +926,7 @@ impl SimpleCast {
/// # }
/// ```
pub fn abi_encode(sig: &str, args: &[impl AsRef<str>]) -> Result<String> {
let func = AbiParser::default().parse_function(sig.as_ref())?;
let func = HumanReadableParser::parse_function(sig)?;
let calldata = encode_args(&func, args)?.to_hex::<String>();
let encoded = &calldata[8..];
Ok(format!("0x{encoded}"))
Expand Down Expand Up @@ -1272,7 +1272,7 @@ impl SimpleCast {
/// # }
/// ```
pub fn calldata(sig: impl AsRef<str>, args: &[impl AsRef<str>]) -> Result<String> {
let func = AbiParser::default().parse_function(sig.as_ref())?;
let func = HumanReadableParser::parse_function(sig.as_ref())?;
let calldata = encode_args(&func, args)?;
Ok(format!("0x{}", calldata.to_hex::<String>()))
}
Expand Down
8 changes: 3 additions & 5 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ use cast::InterfacePath;
use clap::{IntoApp, Parser};
use clap_complete::generate;
use ethers::{
core::{
abi::AbiParser,
types::{BlockId, BlockNumber::Latest, H256},
},
abi::HumanReadableParser,
core::types::{BlockId, BlockNumber::Latest, H256},
providers::{Middleware, Provider},
types::{Address, NameOrAddress, U256},
};
Expand Down Expand Up @@ -679,7 +677,7 @@ async fn main() -> eyre::Result<()> {
}
}
Subcommands::Sig { sig } => {
let selector = AbiParser::default().parse_function(&sig).unwrap().short_signature();
let selector = HumanReadableParser::parse_function(&sig)?.short_signature();
println!("0x{}", hex::encode(selector));
}
Subcommands::FindBlock(cmd) => cmd.run()?.await?,
Expand Down
4 changes: 2 additions & 2 deletions evm/src/fuzz/strategies/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ pub fn fuzz_param_from_state(param: &ParamType, state: EvmFuzzState) -> BoxedStr
#[cfg(test)]
mod tests {
use crate::fuzz::strategies::{build_initial_state, fuzz_calldata, fuzz_calldata_from_state};
use ethers::abi::AbiParser;
use ethers::abi::HumanReadableParser;
use revm::db::{CacheDB, EmptyDB};

#[test]
fn can_fuzz_array() {
let f = "function testArray(uint64[2] calldata values)";
let func = AbiParser::default().parse_function(f).unwrap();
let func = HumanReadableParser::parse_function(f).unwrap();

let db = CacheDB::new(EmptyDB());
let state = build_initial_state(&db);
Expand Down
22 changes: 5 additions & 17 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use ethers_addressbook::contract;
use ethers_core::{
abi::{
self, parse_abi,
self,
token::{LenientTokenizer, StrictTokenizer, Tokenizer},
Abi, AbiParser, Event, EventParam, Function, Param, ParamType, RawLog, Token,
Abi, Event, EventParam, Function, HumanReadableParser, Param, ParamType, RawLog, Token,
},
types::*,
};
Expand Down Expand Up @@ -260,8 +260,7 @@ impl IntoFunction for String {

impl<'a> IntoFunction for &'a str {
fn into(self) -> Function {
AbiParser::default()
.parse_function(self)
HumanReadableParser::parse_function(self)
.unwrap_or_else(|_| panic!("could not convert {self} to function"))
}
}
Expand Down Expand Up @@ -438,23 +437,12 @@ pub fn to_table(value: serde_json::Value) -> String {

/// Given a function signature string, it tries to parse it as a `Function`
pub fn get_func(sig: &str) -> Result<Function> {
// TODO: Make human readable ABI better / more minimal
let abi = parse_abi(&[sig])?;
// get the function
let (_, func) =
abi.functions.iter().next().ok_or_else(|| eyre::eyre!("function name not found"))?;
let func = func.get(0).ok_or_else(|| eyre::eyre!("functions array empty"))?;
Ok(func.clone())
Ok(HumanReadableParser::parse_function(sig)?)
}

/// Given an event signature string, it tries to parse it as a `Event`
pub fn get_event(sig: &str) -> Result<Event> {
let sig = if !sig.starts_with("event ") { format!("event {}", sig) } else { sig.to_string() };
let abi = parse_abi(&[&sig])?;
// get the event
let (_, event) = abi.events.iter().next().ok_or_else(|| eyre::eyre!("event name not found"))?;
let event = event.get(0).ok_or_else(|| eyre::eyre!("events array empty"))?;
Ok(event.clone())
Ok(HumanReadableParser::parse_event(sig)?)
}

/// Given an event without indexed parameters and a rawlog, it tries to return the event with the
Expand Down

0 comments on commit d5530f0

Please sign in to comment.