1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#![allow(clippy::return_self_not_must_use)]

use crate::{
    event_core::parse_log, log::LogMeta, stream::EventStream, ContractError, EthLogDecode,
};
use ethers_core::{
    abi::Address,
    types::{BlockNumber, Filter, Log, Topic, ValueOrArray, H256},
};
use ethers_providers::{FilterWatcher, Middleware, PubsubClient, SubscriptionStream};
use std::{borrow::Borrow, marker::PhantomData};

/// Helper for managing the event filter before querying or streaming its logs
#[derive(Debug)]
#[must_use = "event filters do nothing unless you `query` or `stream` them"]
pub struct Event<B, M, D> {
    /// The event filter's state
    pub filter: Filter,
    pub(crate) provider: B,
    /// Stores the event datatype
    pub(crate) datatype: PhantomData<D>,
    pub(crate) _m: PhantomData<M>,
}

// TODO: Improve these functions
impl<B, M, D> Event<B, M, D>
where
    B: Borrow<M>,
    M: Middleware,
    D: EthLogDecode,
{
    /// 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 = self.filter.from_block(block);
        self
    }

    /// 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 = self.filter.to_block(block);
        self
    }

    /// Sets the filter's `blockHash`. Setting this will override previously
    /// set `from_block` and `to_block` fields.
    #[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
    }

    /// Sets the filter's 0th topic (typically the event name for non-anonymous events)
    pub fn topic0<T: Into<Topic>>(mut self, topic: T) -> Self {
        self.filter.topics[0] = Some(topic.into());
        self
    }

    /// Sets the filter's 1st topic
    pub fn topic1<T: Into<Topic>>(mut self, topic: T) -> Self {
        self.filter.topics[1] = Some(topic.into());
        self
    }

    /// Sets the filter's 2nd topic
    pub fn topic2<T: Into<Topic>>(mut self, topic: T) -> Self {
        self.filter.topics[2] = Some(topic.into());
        self
    }

    /// Sets the filter's 3rd topic
    pub fn topic3<T: Into<Topic>>(mut self, topic: T) -> Self {
        self.filter.topics[3] = Some(topic.into());
        self
    }

    /// Sets the filter's address.
    pub fn address(mut self, address: ValueOrArray<Address>) -> Self {
        self.filter = self.filter.address(address);
        self
    }
}

impl<B, M, D> Event<B, M, D>
where
    B: Borrow<M>,
    M: Middleware,
    D: EthLogDecode,
{
    /// 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.
    ///
    /// [`eth_newFilter`]: https://docs.alchemy.com/alchemy/apis/ethereum/eth-newfilter
    /// [`eth_getFilterChanges`]: https://docs.alchemy.com/alchemy/apis/ethereum/eth-getfilterchanges
    ///
    /// # Example
    // Ignore because `ethers-contract-derive` macros do not work in doctests in `ethers-contract`.
    /// ```ignore
    /// # #[cfg(feature = "abigen")]
    /// # async fn test<M:ethers_providers::Middleware>(contract: ethers_contract::Contract<M>) {
    /// # use ethers_core::types::*;
    /// # use futures_util::stream::StreamExt;
    /// # use ethers_contract::{Contract, EthEvent};
    ///
    /// // 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;
    /// }
    /// # }
    /// ```
    pub async fn stream(
        &self,
    ) -> Result<
        // Wraps the FilterWatcher with a mapping to the event
        EventStream<'_, FilterWatcher<'_, M::Provider, Log>, D, ContractError<M>>,
        ContractError<M>,
    > {
        let filter = self
            .provider
            .borrow()
            .watch(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        Ok(EventStream::new(filter.id, filter, Box::new(move |log| Ok(parse_log(log)?))))
    }

    /// As [`Self::stream`], but does not discard [`Log`] metadata.
    pub async fn stream_with_meta(
        &self,
    ) -> Result<
        // Wraps the FilterWatcher with a mapping to the event
        EventStream<'_, FilterWatcher<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>,
        ContractError<M>,
    > {
        let filter = self
            .provider
            .borrow()
            .watch(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        Ok(EventStream::new(
            filter.id,
            filter,
            Box::new(move |log| {
                let meta = LogMeta::from(&log);
                Ok((parse_log(log)?, meta))
            }),
        ))
    }
}

impl<B, M, D> Event<B, M, D>
where
    B: Borrow<M>,
    M: Middleware,
    <M as Middleware>::Provider: PubsubClient,
    D: EthLogDecode,
{
    /// Returns a subscription for the event
    ///
    /// See also [Self::stream()].
    pub async fn subscribe(
        &self,
    ) -> Result<
        // Wraps the SubscriptionStream with a mapping to the event
        EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, D, ContractError<M>>,
        ContractError<M>,
    > {
        let filter = self
            .provider
            .borrow()
            .subscribe_logs(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        Ok(EventStream::new(filter.id, filter, Box::new(move |log| Ok(parse_log(log)?))))
    }

    /// As [`Self::subscribe`], but includes event metadata
    pub async fn subscribe_with_meta(
        &self,
    ) -> Result<
        // Wraps the SubscriptionStream with a mapping to the event
        EventStream<'_, SubscriptionStream<'_, M::Provider, Log>, (D, LogMeta), ContractError<M>>,
        ContractError<M>,
    > {
        let filter = self
            .provider
            .borrow()
            .subscribe_logs(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        Ok(EventStream::new(
            filter.id,
            filter,
            Box::new(move |log| {
                let meta = LogMeta::from(&log);
                Ok((parse_log(log)?, meta))
            }),
        ))
    }
}

impl<B, M, D> Event<B, M, D>
where
    B: Borrow<M>,
    M: Middleware,
    D: EthLogDecode,
{
    /// Queries the blockchain for the selected filter and returns a vector of matching
    /// event logs
    pub async fn query(&self) -> Result<Vec<D>, ContractError<M>> {
        let logs = self
            .provider
            .borrow()
            .get_logs(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        let events = logs
            .into_iter()
            .map(|log| Ok(parse_log(log)?))
            .collect::<Result<Vec<_>, ContractError<M>>>()?;
        Ok(events)
    }

    /// Queries the blockchain for the selected filter and returns a vector of logs
    /// along with their metadata
    pub async fn query_with_meta(&self) -> Result<Vec<(D, LogMeta)>, ContractError<M>> {
        let logs = self
            .provider
            .borrow()
            .get_logs(&self.filter)
            .await
            .map_err(ContractError::from_middleware_error)?;
        let events = logs
            .into_iter()
            .map(|log| {
                let meta = LogMeta::from(&log);
                let event = parse_log(log)?;
                Ok((event, meta))
            })
            .collect::<Result<_, ContractError<M>>>()?;
        Ok(events)
    }
}