Struct ethers_contract::Event
source · pub struct Event<B, M, D> {
pub filter: Filter,
/* private fields */
}
providers
only.Expand description
Helper for managing the event filter before querying or streaming its logs
Fields§
§filter: Filter
The event filter’s state
Implementations§
source§impl<B, M, D> Event<B, M, D>
impl<B, M, D> Event<B, M, D>
sourcepub fn from_block<T: Into<BlockNumber>>(self, block: T) -> Self
pub fn from_block<T: Into<BlockNumber>>(self, block: T) -> Self
Sets the filter’s from
block
sourcepub fn to_block<T: Into<BlockNumber>>(self, block: T) -> Self
pub fn to_block<T: Into<BlockNumber>>(self, block: T) -> Self
Sets the filter’s to
block
sourcepub fn at_block_hash<T: Into<H256>>(self, hash: T) -> Self
pub fn at_block_hash<T: Into<H256>>(self, hash: T) -> Self
Sets the filter’s blockHash
. Setting this will override previously
set from_block
and to_block
fields.
sourcepub fn topic0<T: Into<Topic>>(self, topic: T) -> Self
pub fn topic0<T: Into<Topic>>(self, topic: T) -> Self
Sets the filter’s 0th topic (typically the event name for non-anonymous events)
sourcepub fn address(self, address: ValueOrArray<Address>) -> Self
pub fn address(self, address: ValueOrArray<Address>) -> Self
Sets the filter’s address.
source§impl<B, M, D> Event<B, M, D>
impl<B, M, D> Event<B, M, D>
sourcepub async fn stream(
&self
) -> Result<EventStream<'_, FilterWatcher<'_, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
pub async fn stream( &self ) -> Result<EventStream<'_, FilterWatcher<'_, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
Turns this event filter into Stream
that yields decoded events.
This will first install a new logs filter via eth_newFilter
using the configured
filter
object. See also FilterWatcher
.
Once the filter is created, this will periodically call eth_getFilterChanges
to get the
newest logs and decode them.
Note: Compared to Self::subscribe
, which is only available on PubsubClient
s, such
as Websocket, this is a poll-based subscription, as the node does not notify us when a new
matching log is available, instead we have to actively ask for new logs using additional RPC
requests, and this is done on an interval basis.
§Example
// The event we want to get
#[derive(Clone, Debug, EthEvent)]
pub struct Approval {
#[ethevent(indexed)]
pub token_owner: Address,
#[ethevent(indexed)]
pub spender: Address,
pub tokens: U256,
}
let ev = contract.event::<Approval>().from_block(1337).to_block(2000);
let mut event_stream = ev.stream().await.unwrap();
while let Some(Ok(approval)) = event_stream.next().await {
let Approval{token_owner,spender,tokens} = approval;
}
sourcepub async fn stream_with_meta(
&self
) -> Result<EventStream<'_, FilterWatcher<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
pub async fn stream_with_meta( &self ) -> Result<EventStream<'_, FilterWatcher<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
As Self::stream
, but does not discard Log
metadata.
source§impl<B, M, D> Event<B, M, D>
impl<B, M, D> Event<B, M, D>
sourcepub async fn subscribe(
&self
) -> Result<EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
pub async fn subscribe( &self ) -> Result<EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
Returns a subscription for the event
See also Self::stream().
sourcepub async fn subscribe_with_meta(
&self
) -> Result<EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
pub async fn subscribe_with_meta( &self ) -> Result<EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
As Self::subscribe
, but includes event metadata
source§impl<B, M, D> Event<B, M, D>
impl<B, M, D> Event<B, M, D>
sourcepub async fn query(&self) -> Result<Vec<D>, ContractError<M>>
pub async fn query(&self) -> Result<Vec<D>, ContractError<M>>
Queries the blockchain for the selected filter and returns a vector of matching event logs
sourcepub async fn query_with_meta(
&self
) -> Result<Vec<(D, LogMeta)>, ContractError<M>>
pub async fn query_with_meta( &self ) -> Result<Vec<(D, LogMeta)>, ContractError<M>>
Queries the blockchain for the selected filter and returns a vector of logs along with their metadata