eth_getLogs | Gnosis

Gnosis Chain API method that returns an array of all logs that match a given filter object. Logs are records of smart contract-generated events on the network and contain valuable information about transactions and smart contract interactions.

Some common use cases for eth_getLogs include monitoring smart contract events, tracking token transfers, and analyzing blockchain data.

Parameters

  • object β€” the filter parameters:
    • fromBlock β€” (optional, default: latest) integer that specifies the starting block number from which the logs should be fetched.
    • toBlock β€” (optional, default: latest) integer that specifies the ending block number until which the logs should be fetched.
    • address β€” (optional) the contract address from which the logs should be fetched. It can be a single address or an array of addresses.
    • topics β€” (optional) an array of DATA topics. The event topics for which the logs should be fetched. It can be a single topic or an array of topics.
    • blockhash β€” (optional) the hash of the specific block. Limits logs to a specific block with a 32-byte hash value. It takes precedence over fromBlock and toBlock.

πŸ“˜

Possible tags for fromBlock and toBlock

  • latest β€” the most recent block in the blockchain and the current state of the blockchain at the most recent block.
  • safe β€” the block that received justification from the beacon chain. Although this block could be involved in a chain reorganization, it would necessitate either a coordinated attack by the majority of validators or an instance of severe propagation latency.
  • finalized β€” the block accepted as canonical by more than 2/3 of the validators. A chain reorganization is extremely unlikely, and it would require at least 1/3 of the staked ETH to be burned.
  • earliest β€” the earliest available or genesis block.
  • pending β€” the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

See the default block parameter and How The Merge Impacts Ethereum’s Application Layer.

Response

  • array β€” an array of log objects that match the specified filter or an empty array if there have been no new events since the last poll:
    • address β€” the contract address from which the event originated
    • topics β€” an array of 32-byte data fields containing indexed event parameters
    • data β€” the non-indexed data that was emitted along with the event
    • blocknumber β€” the block number in which the event was included. null if it is pending.
    • transactionhash β€” the hash of the transaction that triggered the event. null if pending.
    • transactionindex β€” the integer index of the transaction within the block's list of transactions. null if it is pending.
    • blockhash β€” the hash of the block in which the event was included. null if it is pending.
    • logindex β€” the integer identifying the index of the event within the block's list of events. null if pending.
    • removed β€” the boolean value indicating if the event was removed from the blockchain due to a chain reorganization. True if the log was removed. False if it is a valid log.

eth_getLogs code examples

The following examples retrieve the logs of the Transfer event from the Matic token smart contract on Gnosis Chain.

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getLogs() {
  const latestBlock = await web3.eth.getBlockNumber();

  const filter = {
    fromBlock: latestBlock - 10,
    toBlock: 'latest',
    address: '0x7122d7661c4564b7C6Cd4878B06766489a6028A2',
    topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']
  };
  const logs = await web3.eth.getPastLogs(filter);
  console.log(logs);
}

getLogs();
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const getLogs = async () => {
    const latestBlock = await provider.getBlockNumber();
    const filter = {
      fromBlock: latestBlock - 10,
      toBlock: 'latest',
      address: '0x7122d7661c4564b7C6Cd4878B06766489a6028A2',
      topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']
    };
    const logs = await provider.getLogs(filter);
    console.log(logs);
  };
  
getLogs();
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 
web3 = Web3(Web3.HTTPProvider(node_url))

latest_block = web3.eth.block_number

filter = {
    'fromBlock': latest_block - 10,
    'toBlock': 'latest',
    'address': '0x7122d7661c4564b7C6Cd4878B06766489a6028A2',
    'topics': ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef']
}

logs = web3.eth.get_logs(filter)
print(logs)

πŸ“˜

Read Tracking some Bored Apes: The Ethereum event logs tutorial to learn more about the eth_getLogs method.

Block range limitations

While eth_getLogs is a powerful tool, it's crucial to understand its limitations, particularly when working with different EVM-compatible chains, as these networks often have different constraints. The eth_getLogs method allows you to select a range of blocks to get events from and is important to exercise proper management.

In general, eth_getLogs is a very resource-intensive method and although Chainstack does not pose any arbitrary limitation, some blockchain clients do, and a very large block range can impact your node's performance.

We recommend to keep the block range limit of 10,000 blocks for eth_getLogs on Gnosis to maintain a good balance between node and application performance.

πŸ“˜

These figures mean that the difference between the fromBlock and toBlock parameters should not exceed the given block range when querying logs.

While there are no hard limits for the Gnosis network, it's best practice to limit the range of blocks you are querying in a single request to prevent issues such as timeout errors or overly large responses.

These limitation are particularly important when working with popular smart contracts on busy blockchains, as they can return a large amount of data.

Use case

One use case for eth_getLogs is to retrieve the transfer events for a specific Token ID from an ERC-721 smart contract. ERC-721 is a token standard for non-fungible tokens (NFTs), and each token transfer is recorded as an event on the blockchain. By retrieving these events, developers can track the movement of NFTs and build applications that interact with them.

Try the eth_getLogs RPC method yourself

Language
Click Try It! to start a request and see the response here!