Linea
eth_newFilter
The eth_newFilter method creates a filter object to notify when new logs (events) matching the given filter criteria are included in the blockchain. This method is crucial for monitoring smart contract events and is one of the fundamental building blocks for reactive dApp architectures.
Use Cases
- Monitoring smart contract events in real-time applications
- Tracking token transfers (ERC-20, ERC-721, ERC-1155) for wallets
- Detecting state changes in DeFi protocols and liquidity pools
- Building event-driven blockchain applications with high responsiveness
- Implementing real-time notifications for user activities
- Historical data aggregation from contract events for analytics
- Automatic UI updates based on blockchain events in dApps
- Monitoring NFT marketplace activities (listings, sales, bids)
- Tracking governance votes and proposal executions
- Creating activity feeds for blockchain applications
Method Details
This method takes a filter object as a parameter and returns a filter ID that can be used with eth_getFilterChanges and eth_getFilterLogs.
Response Example
Understanding Topics and Event Signatures
The topics array is one of the most powerful features of Ethereum log filtering. It allows you to match specific indexed event parameters:
- Topic[0]: Usually the event signature hash (keccak256 of the event name and parameter types)
- Topic[1]: First indexed parameter
- Topic[2]: Second indexed parameter
- Topic[3]: Third indexed parameter
How Event Signatures Work
Event signatures are created by taking the keccak256 hash of the event name and its parameter types:
Ethereum events can have up to 4 topics (1 event signature + 3 indexed parameters).
Topic Matching Rules:
- Null: Matches any value in that position
- String: Matches exact value in that position
- Array of strings: Matches any value in the array (logical OR)
Common Event Signatures
| Event Type | Event Signature | Event Format |
|---|---|---|
| ERC-20 Transfer | 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef | Transfer(address,address,uint256) |
| ERC-20 Approval | 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925 | Approval(address,address,uint256) |
| ERC-721 Transfer | 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef | Transfer(address,address,uint256) |
| Uniswap V2 Swap | 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 | Swap(address,uint256,uint256,uint256,uint256,address) |
| PairCreated | 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 | PairCreated(address,address,address,uint256) |
Using the Filter ID
After creating a filter, you'll receive a filter ID that can be used with:
eth_getFilterChanges- To poll for new logs since the last polleth_getFilterLogs- To get all logs matching the filter criteriaeth_uninstallFilter- To remove the filter when no longer needed
Important Notes and Best Practices
- Filter Expiration: Filters expire after a period of inactivity (typically around 5 minutes)
- Topic Padding: Address topics must be padded to 32 bytes (zero-padded on the left)
- Performance: Wide filters (e.g., many addresses or blocks) can be resource-intensive
- Rate Limits: Excessive polling can lead to rate limiting from RPC providers
- Block Range: Very large block ranges may cause timeouts or be rejected
- Filter Limits: Some providers limit the number of active filters per connection
- Alternative: Consider using WebSocket subscriptions for real-time events when available
- Indexed vs Non-indexed: Only indexed parameters appear in topics; non-indexed are in data field
- Data Field: For complex events, the data field needs ABI decoding to extract values
- Topic Order: Topic order is critical and must match the order of indexed parameters
See also
- eth_getFilterChanges - Retrieve new logs since last poll
- eth_getFilterLogs - Get all logs matching filter criteria
- eth_uninstallFilter - Remove a filter when no longer needed
- eth_getLogs - Query logs without filter persistence
- eth_newBlockFilter - Create filter for new blocks
- eth_newPendingTransactionFilter - Create filter for pending transactions
Parameters
JSON filter object: { fromBlock?, toBlock?, address?, topics? }