Skip to main content

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 indexed in Solidity)

Creating TopicFilter

from

Parameters:
  • topics: Array of up to 4 entries, each being:
    • HashType - Match this specific hash
    • HashType[] - Match any of these hashes (OR)
    • null - Wildcard, matches anything
Returns: TopicFilterType Throws: InvalidTopicFilterError if array has >4 entries or invalid hashes

Operations

matches

Checks if a log’s topics match the filter criteria. Uses:
  • AND logic across positions: All non-null positions must match
  • OR logic within arrays: Any hash in array matches
Examples:

isEmpty

Returns 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:
Bloom filter properties:
  • 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)
Best practices:
  • Use smallest block ranges possible
  • Filter by contract address when possible
  • Add topic filters to reduce result set
  • Paginate large queries by block range

See Also