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.
This method takes a filter object as a parameter and returns a filter ID that can be used with eth_getFilterChanges
and eth_getFilterLogs
.
Filter options object
Block number/tag to start filtering from (e.g., "0x1" or "latest")
Block number/tag to filter to (e.g., "0x1000" or "latest")
Contract address(es) to filter logs
Topics to filter by, in order, with null for wildcard matching
Filter ID (hex string) used for polling with eth_getFilterChanges
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1a4b0d22dbb7284cf485c969d2857ab3"
}
The topics
array is one of the most powerful features of Ethereum log filtering. It allows you to match specific indexed event parameters:
Event signatures are created by taking the keccak256 hash of the event name and its parameter types:
// Solidity event definition
event Transfer(address indexed from, address indexed to, uint256 value);
// Event signature is calculated as:
keccak256("Transfer(address,address,uint256)")
// Which equals:
// 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
Ethereum events can have up to 4 topics (1 event signature + 3 indexed parameters).
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) |
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