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: FilterBlockOptionFilter 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
impl Filter
Sourcepub fn select(self, filter: impl Into<FilterBlockOption>) -> Filter
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);Sourcepub fn from_block<T>(self, block: T) -> Filterwhere
T: Into<BlockNumberOrTag>,
pub fn from_block<T>(self, block: T) -> Filterwhere
T: Into<BlockNumberOrTag>,
Sets the from block number
Sourcepub fn to_block<T>(self, block: T) -> Filterwhere
T: Into<BlockNumberOrTag>,
pub fn to_block<T>(self, block: T) -> Filterwhere
T: Into<BlockNumberOrTag>,
Sets the to block number
Sourcepub fn is_pending_block_filter(&self) -> bool
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.
Sourcepub fn extract_block_range(&self) -> (Option<u64>, Option<u64>)
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.
Sourcepub fn at_block_hash<T>(self, hash: T) -> Filterwhere
T: Into<FixedBytes<32>>,
pub fn at_block_hash<T>(self, hash: T) -> Filterwhere
T: Into<FixedBytes<32>>,
Pins the block hash for the filter
Sourcepub fn address<T>(self, address: T) -> Filter
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);Sourcepub fn event(self, event_name: &str) -> Filter
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
Sourcepub fn events(
self,
events: impl IntoIterator<Item = impl AsRef<[u8]>>,
) -> Filter
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)
Sourcepub fn event_signature<T>(self, topic: T) -> Filter
pub fn event_signature<T>(self, topic: T) -> Filter
Sets event_signature(topic0) (the event name for non-anonymous events)
Sourcepub fn is_paginatable(&self) -> bool
pub fn is_paginatable(&self) -> bool
Returns true if this is a range filter and has a from block
Sourcepub fn get_to_block(&self) -> Option<u64>
pub fn get_to_block(&self) -> Option<u64>
Returns the numeric value of the toBlock field
Sourcepub fn get_from_block(&self) -> Option<u64>
pub fn get_from_block(&self) -> Option<u64>
Returns the numeric value of the fromBlock field
Sourcepub const fn get_block_hash(&self) -> Option<FixedBytes<32>>
pub const fn get_block_hash(&self) -> Option<FixedBytes<32>>
Returns the value of the blockHash field
Sourcepub fn has_topics(&self) -> bool
pub fn has_topics(&self) -> bool
Returns true if at least one topic is set
Sourcepub fn address_bloom_filter(&self) -> Cow<'_, BloomFilter>
pub fn address_bloom_filter(&self) -> Cow<'_, BloomFilter>
Create the BloomFilter for the addresses.
Sourcepub fn topics_bloom_filter(&self) -> [Cow<'_, BloomFilter>; 4]
pub fn topics_bloom_filter(&self) -> [Cow<'_, BloomFilter>; 4]
Create a BloomFilter for each topic filter.
Sourcepub fn matches_bloom(&self, bloom: Bloom) -> bool
pub fn matches_bloom(&self, bloom: Bloom) -> bool
Check whether the provided bloom contains all topics and the address we wish to filter on.
Sourcepub fn matches_topics(&self, topics: &[FixedBytes<32>]) -> bool
pub fn matches_topics(&self, topics: &[FixedBytes<32>]) -> bool
Returns true if the filter matches the given topics.
Sourcepub fn matches_address(&self, address: Address) -> bool
pub fn matches_address(&self, address: Address) -> bool
Returns true if the filter matches the given address.
Sourcepub const fn matches_block_range(&self, block_number: u64) -> bool
pub const fn matches_block_range(&self, block_number: u64) -> bool
Returns true if the block matches the filter.
Sourcepub fn matches_block_hash(&self, block_hash: FixedBytes<32>) -> bool
pub fn matches_block_hash(&self, block_hash: FixedBytes<32>) -> bool
Returns true if the filter matches the given block hash.
Sourcepub fn matches_block(&self, block: &NumHash) -> bool
pub fn matches_block(&self, block: &NumHash) -> bool
Returns true if the filter matches the given block. Checks both the
block number and hash.
Sourcepub fn matches_log_block<T>(&self, log: &Log<T>) -> bool
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_blockreturns true when called with the block number and hash from the log.
Sourcepub fn matches(&self, log: &Log) -> bool
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.
- For un-parsed RPC logs
crate::Log<LogData>, seeSelf::rpc_matchesandSelf::rpc_matches_parsed. - For parsed
Logs (e.g. those returned by a contract), seeSelf::matches_parsed.
Sourcepub fn rpc_matches(&self, log: &Log) -> bool
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.
- For parsed
Log<T>s (e.g. those returned by a contract), seeSelf::matches_parsed. - For parsed
crate::Log<T>s (e.g. those returned by a contract), seeSelf::rpc_matches.
Sourcepub fn matches_parsed<T, U>(&self, log: &T) -> bool
pub fn matches_parsed<T, U>(&self, log: &T) -> bool
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.
- For un-parsed
Log<LogData>seeSelf::matches. - For un-parsed RPC logs
crate::Log<LogData>seeSelf::rpc_matches. - For parsed RPC
crate::Log<T>s (e.g. those returned by a contract), seeSelf::rpc_matches_parsed.
Sourcepub fn rpc_matches_parsed<U>(&self, log: &Log<U>) -> bool
pub fn rpc_matches_parsed<U>(&self, log: &Log<U>) -> bool
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.
- For un-parsed
Log<LogData>seeSelf::matches. - For parsed
Log<T>s (e.g. those returned by a contract), seeSelf::matches_parsed. - For un-parsed RPC logs
crate::Log<LogData>seeSelf::rpc_matches.
Sourcepub 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,
)
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, )
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 toblock_num_hash- Block number and hash of the blockblock_timestamp- Block timestamptx_hashes_and_receipts- Iterator of (transaction_hash, receipt) pairsremoved- Whether logs are from a removed block (reorg)
Sourcepub fn matching_block_logs<'a, I, R>(
&self,
block_num_hash: NumHash,
block_timestamp: u64,
tx_hashes_and_receipts: I,
removed: bool,
) -> Vec<Log>
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>
Returns matching logs from a block’s receipts grouped by transaction hashes.
§Arguments
block_num_hash- Block number and hash of the blockblock_timestamp- Block timestamptx_hashes_and_receipts- Iterator of (transaction_hash, receipt) pairsremoved- Whether logs are from a removed block (reorg)
Sourcepub fn filter_receipts<I, R>(
&self,
receipts: I,
) -> FilterReceiptsIter<'_, <I as IntoIterator>::IntoIter, R>
pub fn filter_receipts<I, R>( &self, receipts: I, ) -> FilterReceiptsIter<'_, <I as IntoIterator>::IntoIter, R>
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<'de> Deserialize<'de> for Filter
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Filter
serde only.Source§fn deserialize<D>(
deserializer: D,
) -> Result<Filter, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Filter, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Serialize for Filter
Available on crate feature serde only.
impl Serialize for Filter
serde only.Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for Filter
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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