Filter

Struct Filter 

Source
pub struct Filter {
    pub block_option: FilterBlockOption,
    pub address: FilterSet<Address>,
    pub topics: [FilterSet<FixedBytes<32>>; 4],
}
Expand description

Filter for logs.

Fields§

§block_option: FilterBlockOption

Filter block options, specifying on which blocks the filter should match.

§address: FilterSet<Address>

A filter set for matching contract addresses in log queries.

This field determines which contract addresses the filter applies to. It supports:

  • A single address to match logs from that address only.
  • Multiple addresses to match logs from any of them.

§Notes:

  • An empty array ([]) may result in no logs being returned.
  • Some RPC providers handle empty arrays differently than None.
  • Large address lists may affect performance or hit provider limits.
§topics: [FilterSet<FixedBytes<32>>; 4]

Topics (maximum of 4)

Implementations§

Source§

impl Filter

Source

pub fn new() -> Filter

Creates a new, empty filter

Source

pub fn select(self, filter: impl Into<FilterBlockOption>) -> Filter

Sets the inner filter object

NOTE: ranges are always inclusive

§Examples

Match only a specific block

let filter = Filter::new().select(69u64);

This is the same as Filter::new().from_block(1337u64).to_block(1337u64)

Match the latest block only

let filter = Filter::new().select(BlockNumberOrTag::Latest);

Match a block by its hash

let filter = Filter::new().select(B256::ZERO);

This is the same as at_block_hash

Match a range of blocks

let filter = Filter::new().select(0u64..=100u64);

Match all blocks in range (1337..BlockNumberOrTag::Latest)

let filter = Filter::new().select(1337u64..);

Match all blocks in range (BlockNumberOrTag::Earliest..1337)

let filter = Filter::new().select(..=1337u64);
Source

pub fn from_block<T>(self, block: T) -> Filter

Sets the from block number

Source

pub fn to_block<T>(self, block: T) -> Filter

Sets the to block number

Source

pub fn is_pending_block_filter(&self) -> bool

Return true if filter configured to match pending block. This means that both from_block and to_block are set to the pending tag.

Source

pub fn extract_block_range(&self) -> (Option<u64>, Option<u64>)

Extracts the block number range from the filter, if applicable.

Returns a tuple of (from_block, to_block) where each element is Some(block_number) if the corresponding block in the filter is a specific number, or None otherwise.

This method only works with FilterBlockOption::Range variants. For FilterBlockOption::AtBlockHash variants, it returns (None, None).

Block numbers are extracted only from BlockNumberOrTag::Number(_) variants. Other variants like BlockNumberOrTag::Latest, BlockNumberOrTag::Pending, etc. are treated as None.

Source

pub fn at_block_hash<T>(self, hash: T) -> Filter
where T: Into<FixedBytes<32>>,

Pins the block hash for the filter

Source

pub fn address<T>(self, address: T) -> Filter

Sets the address to query with this filter.

§Examples

Match only a specific address ("0xAc4b3DacB91461209Ae9d41EC517c2B9Cb1B7DAF")

let filter = Filter::new()
    .address("0xAc4b3DacB91461209Ae9d41EC517c2B9Cb1B7DAF".parse::<Address>().unwrap());

Match all addresses in array (vec!["0xAc4b3DacB91461209Ae9d41EC517c2B9Cb1B7DAF", "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8"])

let addresses = vec![
    "0xAc4b3DacB91461209Ae9d41EC517c2B9Cb1B7DAF".parse::<Address>().unwrap(),
    "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".parse::<Address>().unwrap(),
];
let filter = Filter::new().address(addresses);
Source

pub fn event(self, event_name: &str) -> Filter

Given the event signature in string form, it hashes it and adds it to the topics to monitor

Source

pub fn events( self, events: impl IntoIterator<Item = impl AsRef<[u8]>>, ) -> Filter

Hashes all event signatures and sets them as array to event_signature(topic0)

Source

pub fn event_signature<T>(self, topic: T) -> Filter
where T: Into<FilterSet<FixedBytes<32>>>,

Sets event_signature(topic0) (the event name for non-anonymous events)

Source

pub fn topic1<T>(self, topic: T) -> Filter
where T: Into<FilterSet<FixedBytes<32>>>,

Sets the 1st indexed topic

Source

pub fn topic2<T>(self, topic: T) -> Filter
where T: Into<FilterSet<FixedBytes<32>>>,

Sets the 2nd indexed topic

Source

pub fn topic3<T>(self, topic: T) -> Filter
where T: Into<FilterSet<FixedBytes<32>>>,

Sets the 3rd indexed topic

Source

pub fn is_paginatable(&self) -> bool

Returns true if this is a range filter and has a from block

Source

pub fn get_to_block(&self) -> Option<u64>

Returns the numeric value of the toBlock field

Source

pub fn get_from_block(&self) -> Option<u64>

Returns the numeric value of the fromBlock field

Source

pub const fn get_block_hash(&self) -> Option<FixedBytes<32>>

Returns the value of the blockHash field

Source

pub fn has_topics(&self) -> bool

Returns true if at least one topic is set

Source

pub fn address_bloom_filter(&self) -> Cow<'_, BloomFilter>

Create the BloomFilter for the addresses.

Source

pub fn topics_bloom_filter(&self) -> [Cow<'_, BloomFilter>; 4]

Create a BloomFilter for each topic filter.

Source

pub fn matches_bloom(&self, bloom: Bloom) -> bool

Check whether the provided bloom contains all topics and the address we wish to filter on.

Source

pub fn matches_topics(&self, topics: &[FixedBytes<32>]) -> bool

Returns true if the filter matches the given topics.

Source

pub fn matches_address(&self, address: Address) -> bool

Returns true if the filter matches the given address.

Source

pub const fn matches_block_range(&self, block_number: u64) -> bool

Returns true if the block matches the filter.

Source

pub fn matches_block_hash(&self, block_hash: FixedBytes<32>) -> bool

Returns true if the filter matches the given block hash.

Source

pub fn matches_block(&self, block: &NumHash) -> bool

Returns true if the filter matches the given block. Checks both the block number and hash.

Source

pub fn matches_log_block<T>(&self, log: &Log<T>) -> bool

Returns true if either of the following is true:

  • the filter and log are both pending
  • the filter matches the block in the log. I.e. Self::matches_block returns true when called with the block number and hash from the log.
Source

pub fn matches(&self, log: &Log) -> bool

Check if a Log matches the filter. This will check topics and address.

This checks Log<LogData>, the raw, primitive type carrying un-parsed LogData.

Source

pub fn rpc_matches(&self, log: &Log) -> bool

Check if a crate::Log matches the filter. This will check topics, address, and block option.

This function checks crate::Log<LogData>, the RPC type carrying un-parsed LogData.

Source

pub fn matches_parsed<T, U>(&self, log: &T) -> bool
where T: AsRef<Log<U>>, &'a U: for<'a> Into<LogData>,

Check if a parsed Log<T> matches the filter. This will check topics and address.

This function checks Log<T>, the primitive Log type carrying some parsed T, usually implementing SolEvent.

Source

pub fn rpc_matches_parsed<U>(&self, log: &Log<U>) -> bool
where &'a U: for<'a> Into<LogData>,

Check if a parsed rpc log crate::Log<T> matches the filter. This will check topics, address, and block option.

If the RPC log block hash or number is None (indicating an uncled block), this function will return false.

This function checks crate::Log<T>, the RPC type carrying some parsed T, usually implementing SolEvent.

Source

pub fn append_matching_block_logs<'a, I, R>( &self, all_logs: &mut Vec<Log>, block_num_hash: NumHash, block_timestamp: u64, tx_hashes_and_receipts: I, removed: bool, )
where I: IntoIterator<Item = (FixedBytes<32>, &'a R)>, R: TxReceipt<Log = Log> + 'a,

Appends logs matching the filter from a block’s receipts.

Iterates through receipts, filters logs, and appends them with block metadata. Includes block number/hash matching.

§Arguments
  • all_logs - Vector to append matching logs to
  • block_num_hash - Block number and hash of the block
  • block_timestamp - Block timestamp
  • tx_hashes_and_receipts - Iterator of (transaction_hash, receipt) pairs
  • removed - Whether logs are from a removed block (reorg)
Source

pub fn matching_block_logs<'a, I, R>( &self, block_num_hash: NumHash, block_timestamp: u64, tx_hashes_and_receipts: I, removed: bool, ) -> Vec<Log>
where I: IntoIterator<Item = (FixedBytes<32>, &'a R)>, R: TxReceipt<Log = Log> + 'a,

Returns matching logs from a block’s receipts grouped by transaction hashes.

§Arguments
  • block_num_hash - Block number and hash of the block
  • block_timestamp - Block timestamp
  • tx_hashes_and_receipts - Iterator of (transaction_hash, receipt) pairs
  • removed - Whether logs are from a removed block (reorg)
Source

pub fn filter_receipts<I, R>( &self, receipts: I, ) -> FilterReceiptsIter<'_, <I as IntoIterator>::IntoIter, R>
where I: IntoIterator, <I as IntoIterator>::Item: IntoIterator<Item = R>, R: TxReceipt<Log = Log>,

Creates an iterator that filters receipts for matching logs.

This method takes an iterator of blocks (where each block is an iterator of receipts) and returns an iterator that yields all logs matching this filter.

§Example
let filter = Filter::new()
    .address("0x1234...".parse::<Address>().unwrap())
    .event_signature(B256::from([0x01; 32]));

let logs: Vec<Log> = filter.filter_receipts(receipts).collect();

Trait Implementations§

Source§

impl Clone for Filter

Source§

fn clone(&self) -> Filter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Filter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Filter

Source§

fn default() -> Filter

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Filter

Available on crate feature serde only.
Source§

fn deserialize<D>( deserializer: D, ) -> Result<Filter, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for Filter

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Filter

Source§

fn eq(&self, other: &Filter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Filter

Available on crate feature serde only.
Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Filter

Source§

impl StructuralPartialEq for Filter

Auto Trait Implementations§

§

impl !Freeze for Filter

§

impl RefUnwindSafe for Filter

§

impl Send for Filter

§

impl Sync for Filter

§

impl Unpin for Filter

§

impl UnwindSafe for Filter

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<'de, T> BorrowedRpcObject<'de> for T
where T: RpcBorrow<'de> + RpcSend,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<'de, T> RpcBorrow<'de> for T
where T: Deserialize<'de> + Debug + Send + Sync + Unpin,

Source§

impl<T> RpcObject for T
where T: RpcSend + RpcRecv,

Source§

impl<T> RpcRecv for T
where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static,

Source§

impl<T> RpcSend for T
where T: Serialize + Clone + Debug + Send + Sync + Unpin,