2020-05-27 08:46:16 +00:00
|
|
|
use crate::ContractError;
|
|
|
|
|
2020-06-01 23:15:33 +00:00
|
|
|
use ethers_providers::{JsonRpcClient, Provider};
|
2020-05-27 08:46:16 +00:00
|
|
|
|
2020-05-31 16:01:34 +00:00
|
|
|
use ethers_core::{
|
2020-05-28 09:04:12 +00:00
|
|
|
abi::{Detokenize, Event as AbiEvent, RawLog},
|
2020-05-31 16:01:34 +00:00
|
|
|
types::{BlockNumber, Filter, ValueOrArray, H256},
|
2020-05-28 09:04:12 +00:00
|
|
|
};
|
2020-05-27 08:46:16 +00:00
|
|
|
|
2020-05-31 21:17:50 +00:00
|
|
|
use std::{collections::HashMap, marker::PhantomData};
|
2020-05-27 08:46:16 +00:00
|
|
|
|
2020-06-01 23:15:33 +00:00
|
|
|
pub struct Event<'a, 'b, P, D> {
|
2020-05-27 08:46:16 +00:00
|
|
|
pub filter: Filter,
|
2020-06-01 23:15:33 +00:00
|
|
|
pub(crate) provider: &'a Provider<P>,
|
2020-05-27 08:46:16 +00:00
|
|
|
pub(crate) event: &'b AbiEvent,
|
|
|
|
pub(crate) datatype: PhantomData<D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Improve these functions
|
2020-06-01 23:15:33 +00:00
|
|
|
impl<'a, 'b, P, D: Detokenize> Event<'a, 'b, P, D> {
|
2020-05-27 08:46:16 +00:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
|
|
|
pub fn from_block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
|
|
|
|
self.filter.from_block = Some(block.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::wrong_self_convention)]
|
|
|
|
pub fn to_block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
|
|
|
|
self.filter.to_block = Some(block.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn topic0<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
|
2020-05-30 14:11:51 +00:00
|
|
|
self.filter.topics[0] = Some(topic.into());
|
2020-05-27 08:46:16 +00:00
|
|
|
self
|
|
|
|
}
|
2020-05-31 21:17:50 +00:00
|
|
|
|
|
|
|
pub fn topic1<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
|
|
|
|
self.filter.topics[1] = Some(topic.into());
|
|
|
|
self
|
|
|
|
}
|
2020-05-27 08:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 23:15:33 +00:00
|
|
|
impl<'a, 'b, P, D> Event<'a, 'b, P, D>
|
2020-05-27 08:46:16 +00:00
|
|
|
where
|
2020-06-01 23:15:33 +00:00
|
|
|
P: JsonRpcClient,
|
|
|
|
D: Detokenize + Clone,
|
2020-05-27 08:46:16 +00:00
|
|
|
{
|
|
|
|
/// Queries the blockchain for the selected filter and returns a vector of matching
|
|
|
|
/// event logs
|
2020-06-01 23:15:33 +00:00
|
|
|
pub async fn query(self) -> Result<Vec<D>, ContractError> {
|
2020-05-31 21:17:50 +00:00
|
|
|
Ok(self.query_with_hashes().await?.values().cloned().collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Queries the blockchain for the selected filter and returns a vector of matching
|
|
|
|
/// event logs
|
2020-06-01 23:15:33 +00:00
|
|
|
pub async fn query_with_hashes(self) -> Result<HashMap<H256, D>, ContractError> {
|
2020-05-27 08:46:16 +00:00
|
|
|
// get the logs
|
2020-06-01 23:15:33 +00:00
|
|
|
let logs = self.provider.get_logs(&self.filter).await?;
|
2020-05-27 08:46:16 +00:00
|
|
|
|
|
|
|
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::<Vec<_>>();
|
|
|
|
|
|
|
|
// convert the tokens to the requested datatype
|
2020-06-01 23:15:33 +00:00
|
|
|
Ok::<_, ContractError>((
|
2020-05-31 21:17:50 +00:00
|
|
|
log.transaction_hash.expect("should have tx hash"),
|
|
|
|
D::from_tokens(tokens)?,
|
|
|
|
))
|
2020-05-27 08:46:16 +00:00
|
|
|
})
|
2020-05-31 21:17:50 +00:00
|
|
|
.collect::<Result<HashMap<H256, D>, _>>()?;
|
2020-05-27 08:46:16 +00:00
|
|
|
|
|
|
|
Ok(events)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add filter watchers
|
|
|
|
}
|