event_scanner/
event_filter.rs

1use alloy::primitives::Address;
2
3#[derive(Clone, Default)]
4pub struct EventFilter {
5    /// Contract address to filter events from. If None, events from all contracts will be tracked.
6    pub contract_address: Option<Address>,
7    /// Human-readable event signature, e.g. "Transfer(address,address,uint256)".
8    /// If None, all events from the specified contract(s) will be tracked.
9    pub event: Option<String>,
10}
11
12impl EventFilter {
13    /// Creates a new [`EventFilter`].
14    ///
15    /// # Examples
16    ///
17    /// ```rust
18    /// use alloy::primitives::Address;
19    /// use event_scanner::EventFilter;
20    ///
21    /// pub async fn create_event_filter() -> EventFilter {
22    ///     let contract_address = Address::ZERO;
23    ///     let filter = EventFilter::new()
24    ///         .with_contract_address(contract_address)
25    ///         .with_event("Transfer(address,address,uint256)");
26    ///
27    ///     filter
28    /// }
29    /// ```
30    #[must_use]
31    pub fn new() -> Self {
32        EventFilter::default()
33    }
34
35    /// Sets the contract address to filter events from.
36    /// If not set, events from all contracts will be tracked.
37    #[must_use]
38    pub fn with_contract_address(mut self, contract_address: Address) -> Self {
39        self.contract_address = Some(contract_address);
40        self
41    }
42
43    /// Sets the event signature to filter specific events.
44    /// If not set, all events from the specified contract(s) will be tracked.
45    #[must_use]
46    pub fn with_event<E: Into<String>>(mut self, event: E) -> Self {
47        self.event = Some(event.into());
48        self
49    }
50}