Struct ethers_contract::Event
source · pub struct Event<'a, M, D> {
pub filter: Filter,
/* private fields */
}
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<M, D: EthLogDecode> Event<'_, M, D>
impl<M, D: EthLogDecode> Event<'_, 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<'a, M, D> Event<'a, M, D>where
M: Middleware,
D: EthLogDecode,
impl<'a, M, D> Event<'a, M, D>where
M: Middleware,
D: EthLogDecode,
sourcepub async fn stream(
&'a self
) -> Result<EventStream<'a, FilterWatcher<'a, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
pub async fn stream(
&'a self
) -> Result<EventStream<'a, FilterWatcher<'a, 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(
&'a self
) -> Result<EventStream<'a, FilterWatcher<'a, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
pub async fn stream_with_meta(
&'a self
) -> Result<EventStream<'a, FilterWatcher<'a, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
As Self::stream
, but does not discard Log
metadata.
source§impl<'a, M, D> Event<'a, M, D>where
M: Middleware,
<M as Middleware>::Provider: PubsubClient,
D: EthLogDecode,
impl<'a, M, D> Event<'a, M, D>where
M: Middleware,
<M as Middleware>::Provider: PubsubClient,
D: EthLogDecode,
sourcepub async fn subscribe(
&'a self
) -> Result<EventStream<'a, SubscriptionStream<'a, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
pub async fn subscribe(
&'a self
) -> Result<EventStream<'a, SubscriptionStream<'a, M::Provider, Log>, D, ContractError<M>>, ContractError<M>>
Returns a subscription for the event
See also Self::stream().
pub async fn subscribe_with_meta(
&'a self
) -> Result<EventStream<'a, SubscriptionStream<'a, M::Provider, Log>, (D, LogMeta), ContractError<M>>, ContractError<M>>
source§impl<M, D> Event<'_, M, D>where
M: Middleware,
D: EthLogDecode,
impl<M, D> Event<'_, M, D>where
M: Middleware,
D: EthLogDecode,
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