rust-ethereum-abi
rust-ethereum-abi copied to clipboard
web3::types::H256 with decode_log_from_slice function
Hello!
I was happy to find this crate, because I thought I can decode the response from the emitted events, but I encountered some problems. First of all the from_reader function is removed in the 4th version of the library but the tutorial still contains it. It would be good to have an easy to read in ABI function. For example I created this for myself:
pub fn read_abi_from_file(p: &str) -> Result<Abi, anyhow::Error> {
// Open the file in read-only mode with buffer.
let path = Path::new(&p);
let file = File::open(path)?;
let reader = BufReader::new(file);
// Read the JSON contains the ABI
let abi = serde_json::from_reader(reader)?;
// Return the `User`.
Ok(abi)
}
But the bigger problem just came later. When I tried to use decode_log_from_slice function. I'm using web3 to listen for the event stream and it works great, but its getting a Vec<H256> and this library neads &[H256]. Okay I checked the compiler and told me I need primitive_types::H256. Still wrote me a weird error message:
= note: expected reference `&[primitive_types::H256]` (struct `primitive_types::H256`)
found reference `&[primitive_types::H256]` (struct `primitive_types::H256`)
Okay I checked the library's toml file and I found out it used ethereum_types, so I just copied and put into my toml. Did the weird type conversation like:
let topic0 = log.clone().unwrap().topics[0].to_fixed_bytes();
let topic1 = log.clone().unwrap().topics[1].to_fixed_bytes();
let topic2 = log.clone().unwrap().topics[2].to_fixed_bytes();
let topics: &[ethereum_types::H256] = &[
ethereum_types::H256::from_slice(&topic0),
ethereum_types::H256::from_slice(&topic1),
ethereum_types::H256::from_slice(&topic2),
];
And I got the error again:
= note: expected reference `&[primitive_types::H256]`
found reference `&[ethereum_types::H256]`
I would suggest to have a new function what can handle web3 types for the topics Vec<H256> and also the Bytes one https://docs.rs/web3/0.18.0/web3/types/struct.Log.html
If that would be implemented decoding the events would be so easy. Thank you if you considering it.