eth_getFilterLogs - Ethereum

Retrieve logs for a specified filter using eth_getFilterLogs. Essential for tracking block events, transaction logs, or smart contract interactions in real-time within Ethereum dApps.

The eth_getFilterLogs method is a JSON-RPC Ethereum API call that returns an array of logs for the specified filter

This eth_getFilterLogs RPC Ethereum method is crucial for developers seeking to track block events, transaction logs, or smart contract interactions in real-time. To enhance log retrieval performance, it’s recommended to leave the --auto-log-bloom-caching-enabled option set to the default value of true.

eth_getFilterLogs is a core API endpoint in Ethereum development, providing an easy way to retrieve logs based on filters created by previous methods like eth_newFilter, eth_newPendingTransactionFilter, or eth_newBlockFilter. If a filter is invalid or expired, eth_getFilterLogs error responses are returned.

Supported Networks

  • Filter ID (DATA):

A unique identifier for the filter whose logs are being requested. This Filter ID must be obtained from prior methods like eth_newFilter, eth_newPendingTransactionFilter, or eth_newBlockFilter. The eth_getlogs limit can be set to manage the number of logs returned, which is useful when filtering large amounts of data.

Request

URL (API Endpoint)

https://go.getblock.io/<ACCESS-TOKEN>/
curl --location --request POST 'https://go.getblock.io/<ACCESS-TOKEN>/' \  
--header 'Content-Type: application/json' \  
--data-raw '{  
    "jsonrpc": "2.0",  
    "method": "eth_getFilterLogs",  
    "params": [  
        "0x5ace5de3985749b6a1b2b0d3f3e1fb69"  
    ],  
    "id": "getblock.io"  
}'  

eth_getFilterLogs example: In this example, a JSON-RPC request is made to fetch the logs related to the specified filter ID.

Response

If the filter is not found, the response will include an error

{  
    "error": {  
        "code": -32000,  
        "message": "filter not found"  
    },  
    "id": "getblock.io",  
    "jsonrpc": "2.0"  
} 

If logs are available for the filter, they will be returned as an array of log entries, which can be processed to monitor block or transaction events.

Use Case

Tracking Contract Events:The Web3 eth_getFilterLogs method allows developers to monitor smart contract events. By filtering for specific contract logs, you can track interactions with your decentralized application (dApp) in real time.

Monitoring Block Events:This method is essential for applications that need to monitor block data or events on the Ethereum blockchain, such as when new blocks are mined or specific state changes occur.

Transaction Logs:Developers can use Ethereum eth_getFilterLogs to collect logs associated with transactions, including transaction receipts and their associated data.

Code Example

Here’s how you can implement the eth_getFilterLogs method using different programming languages

import requests
import json

url = 'https://go.getblock.io/<ACCESS-TOKEN>/'
headers = {
    'Content-Type': 'application/json'
}

payload = {
    "jsonrpc": "2.0",
    "method": "eth_getFilterLogs",
    "params": [
        "0x5ace5de3985749b6a1b2b0d3f3e1fb69"
    ],
    "id": "getblock.io"
}

response = requests.post(url, headers=headers, data=json.dumps(payload))

if response.status_code == 200:
    print(response.json())
else:
    print(f"Ошибка: {response.status_code}", response.text)

Setup:Replace <ACCESS-TOKEN> with your GetBlock API key.Use FILTER_ID from a prior filter creation method (e.g., eth_newFilter).

Request:Sends a POST request to the JSON-RPC endpoint, calling the eth_getFilterLogs method and passing the FILTER_ID as a parameter.

Response Handling:Logs any errors, such as the eth_getFilterLogs error like "filter not found," if the filter is expired or invalid.Outputs an array of log entries if successful.

Usage:The eth_getFilterLogs method is part of the Core API for Ethereum, enabling developers to efficiently query logs for specific filters. This method is especially helpful for applications that need to track changes to block or transaction data in real-time.

Last updated

© 2019-2024 GetBlock LLC. All rights reserved ID: 21835790. Address: Belgrade, Serbia.