use crate::{log::LogMeta, stream::EventStream, ContractError, EthLogDecode};
use ethers_core::{
abi::{Detokenize, RawLog},
types::{BlockNumber, Filter, Log, ValueOrArray, H256},
};
use ethers_providers::{FilterWatcher, Middleware, PubsubClient, SubscriptionStream};
use std::borrow::Cow;
use std::marker::PhantomData;
pub trait EthEvent: Detokenize + Send + Sync {
fn name() -> Cow<'static, str>;
fn signature() -> H256;
fn abi_signature() -> Cow<'static, str>;
fn decode_log(log: &RawLog) -> Result<Self, ethers_core::abi::Error>
where
Self: Sized;
fn is_anonymous() -> bool;
fn new<M: Middleware>(filter: Filter, provider: &M) -> Event<M, Self>
where
Self: Sized,
{
let filter = filter.event(&Self::abi_signature());
Event {
filter,
provider,
datatype: PhantomData,
}
}
}
impl<T: EthEvent> EthLogDecode for T {
fn decode_log(log: &RawLog) -> Result<Self, ethers_core::abi::Error>
where
Self: Sized,
{
T::decode_log(log)
}
}
#[derive(Debug)]
#[must_use = "event filters do nothing unless you `query` or `stream` them"]
pub struct Event<'a, M, D> {
pub filter: Filter,
pub(crate) provider: &'a M,
pub(crate) datatype: PhantomData<D>,
}
impl<M, D: EthLogDecode> Event<'_, M, D> {
#[allow(clippy::wrong_self_convention)]
pub fn from_block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
self.filter = self.filter.from_block(block);
self
}
#[allow(clippy::wrong_self_convention)]
pub fn to_block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
self.filter = self.filter.to_block(block);
self
}
#[allow(clippy::wrong_self_convention)]
pub fn at_block_hash<T: Into<H256>>(mut self, hash: T) -> Self {
self.filter = self.filter.at_block_hash(hash);
self
}
pub fn topic0<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[0] = Some(topic.into());
self
}
pub fn topic1<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[1] = Some(topic.into());
self
}
pub fn topic2<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[2] = Some(topic.into());
self
}
pub fn topic3<T: Into<ValueOrArray<H256>>>(mut self, topic: T) -> Self {
self.filter.topics[3] = Some(topic.into());
self
}
}
impl<'a, M, D> Event<'a, M, D>
where
M: Middleware,
D: EthLogDecode,
{
pub async fn stream(
&'a self,
) -> Result<
EventStream<'a, FilterWatcher<'a, M::Provider, Log>, D, ContractError<M>>,
ContractError<M>,
> {
let filter = self
.provider
.watch(&self.filter)
.await
.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(
filter.id,
filter,
Box::new(move |log| self.parse_log(log)),
))
}
}
impl<'a, M, D> Event<'a, M, D>
where
M: Middleware,
<M as Middleware>::Provider: PubsubClient,
D: EthLogDecode,
{
pub async fn subscribe(
&'a self,
) -> Result<
EventStream<'a, SubscriptionStream<'a, M::Provider, Log>, D, ContractError<M>>,
ContractError<M>,
> {
let filter = self
.provider
.subscribe_logs(&self.filter)
.await
.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(
filter.id,
filter,
Box::new(move |log| self.parse_log(log)),
))
}
}
impl<M, D> Event<'_, M, D>
where
M: Middleware,
D: EthLogDecode,
{
pub async fn query(&self) -> Result<Vec<D>, ContractError<M>> {
let logs = self
.provider
.get_logs(&self.filter)
.await
.map_err(ContractError::MiddlewareError)?;
let events = logs
.into_iter()
.map(|log| self.parse_log(log))
.collect::<Result<Vec<_>, ContractError<M>>>()?;
Ok(events)
}
pub async fn query_with_meta(&self) -> Result<Vec<(D, LogMeta)>, ContractError<M>> {
let logs = self
.provider
.get_logs(&self.filter)
.await
.map_err(ContractError::MiddlewareError)?;
let events = logs
.into_iter()
.map(|log| {
let meta = LogMeta::from(&log);
let event = self.parse_log(log)?;
Ok((event, meta))
})
.collect::<Result<_, ContractError<M>>>()?;
Ok(events)
}
fn parse_log(&self, log: Log) -> Result<D, ContractError<M>> {
D::decode_log(&RawLog {
topics: log.topics,
data: log.data.to_vec(),
})
.map_err(From::from)
}
}