Skip to content

Commit

Permalink
feat(cast): add cast 4byte-event (gakonst#680)
Browse files Browse the repository at this point in the history
* feat: added 4byte-event to cast

* feat: fourbyte_event() tests
  • Loading branch information
0xTomoyo authored Feb 6, 2022
1 parent 64c37b6 commit 63644de
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [x] `--to-wei`
- [x] `4byte`
- [x] `4byte-decode`
- [ ] `4byte-event`
- [x] `4byte-event`
- [x] `abi-encode`
- [x] `age`
- [x] `balance`
Expand Down
1 change: 1 addition & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ SUBCOMMANDS:
--to-wei convert an ETH amount into wei
4byte Fetches function signatures given the selector from 4byte.directory
4byte-decode Decodes transaction calldata by fetching the signature using 4byte.directory
4byte-event Takes a 32 byte topic and prints the response from querying 4byte.directory for that topic
abi-encode
age Prints the timestamp of a block
balance Print the balance of <account> in wei
Expand Down
4 changes: 4 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ async fn main() -> eyre::Result<()> {

tokens.for_each(|t| println!("{}", t));
}
Subcommands::FourByteEvent { topic } => {
let sigs = foundry_utils::fourbyte_event(&topic).await?;
sigs.iter().for_each(|sig| println!("{}", sig.0));
}
Subcommands::Age { block, rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!(
Expand Down
6 changes: 6 additions & 0 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ pub enum Subcommands {
#[clap(long, help = "the 4byte selector id to use, can also be earliest/latest")]
id: Option<String>,
},
#[clap(name = "4byte-event")]
#[clap(about = "Takes a 32 byte topic and prints the response from querying 4byte.directory for that topic")]
FourByteEvent {
#[clap(help = "the 32 byte topic")]
topic: String,
},
#[clap(name = "age")]
#[clap(about = "Prints the timestamp of a block")]
Age {
Expand Down
41 changes: 41 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,37 @@ pub async fn fourbyte_possible_sigs(calldata: &str, id: Option<String>) -> Resul
}
}

/// Fetches a event signature given the 32 byte topic using 4byte.directory
pub async fn fourbyte_event(topic: &str) -> Result<Vec<(String, i32)>> {
#[derive(Deserialize)]
struct Decoded {
text_signature: String,
id: i32,
}

#[derive(Deserialize)]
struct ApiResponse {
results: Vec<Decoded>,
}

let topic = &topic.strip_prefix("0x").unwrap_or(topic);
if topic.len() < 64 {
return Err(eyre::eyre!("Invalid topic"));
}
let topic = &topic[..8];

let url =
format!("https://www.4byte.directory/api/v1/event-signatures/?hex_signature={}", topic);
let res = reqwest::get(url).await?;
let api_response = res.json::<ApiResponse>().await?;

Ok(api_response
.results
.into_iter()
.map(|d| (d.text_signature, d.id))
.collect::<Vec<(String, i32)>>())
}

pub fn abi_decode(sig: &str, calldata: &str, input: bool) -> Result<Vec<Token>> {
let func = IntoFunction::into(sig);
let calldata = calldata.strip_prefix("0x").unwrap_or(calldata);
Expand Down Expand Up @@ -946,6 +977,16 @@ mod tests {
assert_eq!(sigs[0], "transfer(address,uint256)".to_string());
}

#[tokio::test]
async fn test_fourbyte_event() {
let sigs = fourbyte_event("0x7e1db2a1cd12f0506ecd806dba508035b290666b84b096a87af2fd2a1516ede6").await.unwrap();
assert_eq!(sigs[0].0, "updateAuthority(address,uint8)".to_string());
assert_eq!(sigs[0].1, 79573);

let sigs = fourbyte_event("0xb7009613e63fb13fd59a2fa4c206a992c1f090a44e5d530be255aa17fed0b3dd").await.unwrap();
assert_eq!(sigs[0].0, "canCall(address,address,bytes4)".to_string());
}

#[test]
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn abi2solidity() {
Expand Down

0 comments on commit 63644de

Please sign in to comment.