ethers-rs/ethers-contract/src/event.rs

113 lines
3.5 KiB
Rust
Raw Normal View History

use crate::ContractError;
2020-06-01 23:15:33 +00:00
use ethers_providers::{JsonRpcClient, Provider};
2020-05-31 16:01:34 +00:00
use ethers_core::{
abi::{Detokenize, Event as AbiEvent, RawLog},
2020-06-02 11:33:21 +00:00
types::{BlockNumber, Filter, Log, ValueOrArray, H256},
};
use std::{collections::HashMap, marker::PhantomData};
2020-06-10 18:20:47 +00:00
/// Helper for managing the event filter before querying or streaming its logs
2020-06-02 11:33:21 +00:00
pub struct Event<'a: 'b, 'b, P, D> {
2020-06-10 18:20:47 +00:00
/// The event filter's state
pub filter: Filter,
2020-06-10 18:20:47 +00:00
/// The ABI of the event which is being filtered
pub event: &'b AbiEvent,
2020-06-01 23:15:33 +00:00
pub(crate) provider: &'a Provider<P>,
pub(crate) datatype: PhantomData<D>,
}
// TODO: Improve these functions
2020-06-02 11:33:21 +00:00
impl<P, D: Detokenize> Event<'_, '_, P, D> {
2020-06-10 18:20:47 +00:00
/// Sets the filter's `from` block
#[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
}
2020-06-10 18:20:47 +00:00
/// Sets the filter's `to` block
#[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
}
2020-06-10 18:20:47 +00:00
/// Sets the filter's 0th topic (typically the event name for non-anonymous events)
pub fn topic0<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[0] = Some(topic.into());
self
}
2020-06-10 18:20:47 +00:00
/// Sets the filter's 1st topic
pub fn topic1<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[1] = Some(topic.into());
self
}
2020-06-02 11:33:21 +00:00
2020-06-10 18:20:47 +00:00
/// Sets the filter's 2nd topic
2020-06-02 11:33:21 +00:00
pub fn topic2<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[2] = Some(topic.into());
self
}
2020-06-10 18:20:47 +00:00
/// Sets the filter's 3rd topic
2020-06-02 11:33:21 +00:00
pub fn topic3<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[3] = Some(topic.into());
self
}
}
2020-06-02 11:33:21 +00:00
impl<P, D> Event<'_, '_, P, D>
where
2020-06-01 23:15:33 +00:00
P: JsonRpcClient,
D: Detokenize + Clone,
{
/// Queries the blockchain for the selected filter and returns a vector of matching
/// event logs
2020-06-02 11:33:21 +00:00
pub async fn query(&self) -> Result<Vec<D>, ContractError> {
let logs = self.provider.get_logs(&self.filter).await?;
let events = logs
.into_iter()
.map(|log| self.parse_log(log))
.collect::<Result<Vec<_>, ContractError>>()?;
Ok(events)
}
2020-06-02 11:33:21 +00:00
/// Queries the blockchain for the selected filter and returns a hashmap of
/// txhash -> logs
pub async fn query_with_hashes(&self) -> Result<HashMap<H256, D>, ContractError> {
2020-06-01 23:15:33 +00:00
let logs = self.provider.get_logs(&self.filter).await?;
let events = logs
.into_iter()
.map(|log| {
2020-06-02 11:33:21 +00:00
let tx_hash = log.transaction_hash.expect("should have tx hash");
let event = self.parse_log(log)?;
Ok((tx_hash, event))
})
2020-06-02 11:33:21 +00:00
.collect::<Result<_, ContractError>>()?;
Ok(events)
}
2020-06-02 11:33:21 +00:00
fn parse_log(&self, log: Log) -> Result<D, ContractError> {
// 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
Ok(D::from_tokens(tokens)?)
}
// TODO: Add filter watchers
}