Skip to content

Instantly share code, notes, and snippets.

@pawanjay176
Created October 26, 2020 20:50
Show Gist options
  • Save pawanjay176/4340020c41898cfee7d0430538cee986 to your computer and use it in GitHub Desktop.
Save pawanjay176/4340020c41898cfee7d0430538cee986 to your computer and use it in GitHub Desktop.
use ethabi::{param_type::Reader, Contract, Event, EventParam, Function, Param, ParamType};
fn parse_line(line: &str, abi: &mut Contract) {
if line.starts_with("function") {
parse_function(&line[9..], abi); // Exclude space after function
} else if line.starts_with("event") {
parse_event(&line[6..], abi); // Exclude space after event
} else {
panic!("Invalid signature");
}
}
fn parse_event(line: &str, abi: &mut Contract) {
if let Some(index) = line.find("(") {
let (name, rest) = line.split_at(index);
let mut event_params = Vec::new();
for param in rest[1..rest.len() - 1].split(",") {
event_params.push(parse_event_input(param).unwrap());
}
let event = Event {
name: name.to_string(),
anonymous: false, // TODO: Handle this according to input
inputs: event_params,
};
abi.events
.entry(event.name.clone())
.or_default()
.push(event);
}
}
fn parse_event_input(param: &str) -> Option<EventParam> {
let param = param.trim_start();
let tokens: Vec<&str> = param.split(" ").collect();
if tokens.len() == 2 {
let ty: ParamType = Reader::read(tokens[0]).unwrap();
Some(EventParam {
name: tokens[1].to_string(),
kind: ty,
indexed: false,
})
} else if tokens.len() == 3 {
let ty: ParamType = Reader::read(tokens[0]).unwrap();
Some(EventParam {
name: tokens[2].to_string(),
kind: ty,
indexed: true,
})
} else {
None
}
}
let human_abi = ["event Transfer(address indexed from, address indexed to, uint amount)"];
let mut abi = Contract::default();
for line in human_abi.iter() {
parse_line(line, &mut abi);
}
dbg!(abi);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment