use crate::ContractError; use ethers_providers::{networks::Network, JsonRpcClient, Provider}; use ethers_core::{ abi::{Detokenize, Event as AbiEvent, RawLog}, types::{BlockNumber, Filter, ValueOrArray, H256}, }; use std::{collections::HashMap, marker::PhantomData}; pub struct Event<'a, 'b, P, N, D> { pub filter: Filter, pub(crate) provider: &'a Provider
,
pub(crate) event: &'b AbiEvent,
pub(crate) datatype: PhantomData > {
Ok(self.query_with_hashes().await?.values().cloned().collect())
}
/// Queries the blockchain for the selected filter and returns a vector of matching
/// event logs
pub async fn query_with_hashes(self) -> Result > {
// get the logs
let logs = self
.provider
.get_logs(&self.filter)
.await
.map_err(ContractError::CallError)?;
let events = logs
.into_iter()
.map(|log| {
// ethabi parses the unindexed and indexed logs together to a
// vector of tokens
let tokens = self
.event
.parse_log(RawLog {
topics: log.topics,
data: log.data.0,
})?
.params
.into_iter()
.map(|param| param.value)
.collect:: >((
log.transaction_hash.expect("should have tx hash"),
D::from_tokens(tokens)?,
))
})
.collect::