Get you own node endpoint todayStart for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.
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 ofDATAtopics. 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 overfromBlockandtoBlock.
Possible tags for
fromBlock and toBlocklatest— 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.
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 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.nullif it is pending.transactionhash— the hash of the transaction that triggered the event.nullif pending.transactionindex— the integer index of the transaction within the block’s list of transactions.nullif it is pending.blockhash— the hash of the block in which the event was included.nullif it is pending.logindex— the integer identifying the index of the event within the block’s list of events.nullif pending.removed— the boolean value indicating if the event was removed from the blockchain due to a chain reorganization.Trueif the log was removed.Falseif it is a valid log.
eth_getLogs code examples
Learn more about the
ChainstackProvider in ethers.js: ethers ChainstackProvider Documentation.Read Tracking some Bored Apes: The Ethereum event logs tutorial to learn more about the
eth_getLogs method.Block range limitations
Whileeth_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 5,000 blocks for eth_getLogs on Ethereum 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.Use case
One use case foreth_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 ‘6750’ from the BoredApeYachtClub collection on Ethereum. This will retrieve the logs and display the address that initiated the transfer and the block number where it occurred.
index.js
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 valid 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.Body
application/json