eth_getLogs include monitoring smart contract events, tracking token transfers, and analyzing blockchain data.
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.latest — the most recent block in the blockchain and the current state of the blockchain at the most recent blockearliest — the earliest available or genesis blockpending — 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.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 originatedtopics — an array of 32-byte data fields containing indexed event parametersdata — the non-indexed data that was emitted along with the eventblocknumber — 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 exampleseth_getLogs method.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 100,000 blocks for eth_getLogs on Avalanche to maintain a good balance between node and application performance.
fromBlock and toBlock parameters should not exceed the given block range when querying logs.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.
The following program uses eth_getLogs and web3.js to track the transfers of the token number ‘4776’ from the Cloudheadz collection on Avalanche. This will retrieve the logs and display the address that initiated the transfer and the block number where it occurred.
getTokenTransferLogs which takes a contractAddress and a tokenId as input parameters.
Within the function, the tokenId is converted to hexadecimal format and then used to create a topic for a filter to get past logs for a specific token transfer. The getPastLogs function is an API that returns logs that match a certain filter.
Once the logs have been retrieved, the function loops through each log and extracts the relevant information, such as the sender’s address and the block number. It also checks whether the sender’s address is a valid address; this is in case the parsing logic fails.
For each valid transfer log, the function logs a summary to the console and creates a transfer log object that includes the fromAddress, tokenId, and blockNumber properties.
The function returns an array of transfer log objects, and an exception is thrown if there are any errors.
The main function calls getTokenTransferLogs with a specific contractAddress and tokenId, and then logs the array of transfer log objects to the console. If there are any errors, an error message is logged to the console instead.