tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.46k stars 470 forks source link

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

Open jolow99 opened 1 year ago

jolow99 commented 1 year ago

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)
}