Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3::types::H256 with decode_log_from_slice function #60

Open
palinko91 opened this issue Oct 24, 2022 · 2 comments
Open

web3::types::H256 with decode_log_from_slice function #60

palinko91 opened this issue Oct 24, 2022 · 2 comments

Comments

@palinko91
Copy link

palinko91 commented Oct 24, 2022

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 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 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.

@TAnas0
Copy link

TAnas0 commented Apr 15, 2023

I can second all of this step by step. I faced the same issue of confusion of type H256 between ethereum_types and primitive_types. I am trying to use this library to listen to a stream of Event from a smart contract.

Was about to make an issue for the inexisten ABI::from_reader function and found this issue, so I am just dropping this small repo as reproduction for it: https://github.com/TAnas0/rust-ethereum-abi-bug

This question was asked 3 months ago, so I am not holding my breath.

@DirectX
Copy link

DirectX commented Oct 28, 2023

Also got confused with inconsistency of ethereum-types and primitive-types version of U256.

Finally found that when using standard crate ethereum_abi version 0.4.0 then it is necessary to specify outdated version of ethereum-types 0.13.1:

[dependencies]
ethereum_abi = "0.4.0"
ethereum-types = { version = "0.13.1", default-features = false, features = ["std"] }

Another option is to check out the latest not released version of ethereum_abi from git into separate folder and use local crate along with actual version of ethereum-types:

[dependencies]
ethereum_abi = { path = "../custom_libs/rust-ethereum-abi" }
ethereum-types = { version = "0.14.1", default-features = false, features = ["std"] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants