Overview
TopicFilter is an array-based filter for matching event topics (indexed parameters) in Ethereum logs. Topics use AND logic across positions and OR logic within arrays, enabling precise event filtering for applications like DEX trackers and wallet monitors.
Type Definition
Topic Positions
Events can have up to 4 topics (0-3):- topic0: Event signature hash (required for non-anonymous events)
- topic1-3: Indexed parameters (if declared with
indexedin Solidity)
Creating TopicFilter
from
topics: Array of up to 4 entries, each being:HashType- Match this specific hashHashType[]- Match any of these hashes (OR)null- Wildcard, matches anything
TopicFilterType
Throws: InvalidTopicFilterError if array has >4 entries or invalid hashes
Operations
matches
- AND logic across positions: All non-null positions must match
- OR logic within arrays: Any hash in array matches
isEmpty
true if all positions are null or undefined (no filtering).
Matching Logic
Position-based AND
Array-based OR
Wildcards
Common Patterns
Specific Event
Event with Specific Sender
Event with Specific Recipient
Multiple Event Types
Transactions Between Two Addresses
Bloom Filters
Ethereum blocks contain bloom filters that enable efficient topic matching without scanning all logs:- Probabilistic data structure (false positives possible, no false negatives)
- 2048-bit (256-byte) bit array
- 3 hash functions per topic
- Enables fast block scanning for eth_getLogs
Example: DEX Trade Monitor
Performance Considerations
Bloom Filter Optimization
More specific filters = faster queries:Node Resource Limits
Nodes may limit:- Query block range (e.g., 10,000 blocks max)
- Number of results (e.g., 10,000 logs max)
- Query complexity (multiple OR conditions)
- Use smallest block ranges possible
- Filter by contract address when possible
- Add topic filters to reduce result set
- Paginate large queries by block range
Related Types
- LogFilter - Complete log filter with address and block range
- FilterId - Filter identifier for polling
- EventLog - Log entry structure
- EventSignature - Event signature hash

