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

Unable to convert web3::contract::error to web3::error #691

Open
jolow99 opened this issue May 1, 2023 · 0 comments
Open

Unable to convert web3::contract::error to web3::error #691

jolow99 opened this issue May 1, 2023 · 0 comments

Comments

@jolow99
Copy link

jolow99 commented May 1, 2023

I am trying to build an event listener in web3 Rust. However, I am getting this error message below. I believe this is because .events returns a web3::contract::error, but my function returns a web3::error.

However, the web3::error from the web3 crate does not have a method to convert between the two errors. Is there anything I can do to get around this?

error[E0277]: `?` couldn't convert the error to `web3::Error`
  --> src/main.rs:24:94
   |
24 |     let event_listener = contract_object.events("Vote", topic0, "".to_string(), topic2).await?;
   |                                                                                              ^ the trait `From<web3::contract::Error>` is not implemented for `web3::Error`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following other types implement trait `From<T>`:
             <web3::Error as From<RecoveryError>>
             <web3::Error as From<jsonrpc_core::types::error::Error>>
             <web3::Error as From<native_tls::Error>>
             <web3::Error as From<rocket::tokio::sync::mpsc::error::SendError<ipc::TransportMessage>>>
             <web3::Error as From<rocket::tokio::sync::oneshot::error::RecvError>>
             <web3::Error as From<serde_json::error::Error>>
             <web3::Error as From<soketto::connection::Error>>
             <web3::Error as From<soketto::handshake::Error>>
           and 3 others
   = note: required for `Result<std::string::String, web3::Error>` to implement `FromResidual<Result<Infallible, web3::contract::Error>>`
#[macro_use] extern crate rocket; 
async fn get_donations(address: &String) -> web3::Result<String> {   
    let ws = web3::transports::WebSocket::new("wss://mainnet.infura.io/ws/v3/").await?;
    let web3 = web3::Web3::new(ws);

    let contract_address: H160 = Address::from_str("0x0F8e03A2175184228A429A118653D068F3a0Bb35").unwrap();
    let abi = include_bytes!("../abi.json");
    let contract_object = match Contract::from_json(web3.eth(), contract_address, abi) {
        Ok(contract) => contract,
        Err(e) => panic!("Error loading contract: {:?}", e),
    };

    let topic0:String= "0x0064caa73f1d59b69adbeb65654b0f0953597994e4241ee2460b560b8d65aaa2".to_string();
    let topic2:String = "0xf0572ad90c411f8f5d5dcb0938f0021e1b5d28d7cf427ddc34cc3a53cc740336".to_string();
    let event_listener = contract_object.events("Vote", topic0, "".to_string(), topic2).await?;
   


    let address: H160 = Address::from_str(&address).unwrap();
    let balance: U256 = web3.eth().balance(address,Some(BlockNumber::Latest)).await?;
    let balance: String = balance.to_string();
    Ok(balance)
}
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

1 participant