This article applies to:
- Ethereum
- Polygon
- BNB Smart Chain
- Avalanche
- Fantom
- Harmony
Introduction
Events are defined with the event keyword in EVM smart contracts.
When the defined event is called, the event is emitted.
An emitted event is recorded to the receipt trieāthe log records stored locally alongside an EVM node. The trie of the stored logs is recorded as a hash called receipt root to the block header of a mined block. See also Merkling in Ethereum.
Each event type emitted by a smart contract is identified as an event signature. An event signature is Keccak-256 hashed as topic[0].
Each smart contract interaction has topic[0] identifying the type of event that occurred on the interaction.
To get the topic[0] hash of an event signature:
- You can get the event signature and use an online converter. For example, the most common event type is Transfer. The event signature is Transfer(address,address,uint256). The event signature converts to the topic[0] value: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
- An easier way is to find your contract on Etherscan and check the contract's Events tab. All topic[0] values represent events.
There are four common ways to retrieve the logs:
- By subscribing to the log records through web3.eth.subscribe.
- By calling eth_getLogs.
- By creating a new filter with eth_newFilter and calling eth_getFilterLogs.
- Buy using the getPastEvents method in web3.js.
How-to
web3.eth.subscribe
See Subscribing to smart contract events with web3.js.
eth_getLogs
Retrieve logs through your node's endpoint:
curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_getLogs",
"params":
[
{
"fromBlock": "BLOCK_HEX",
"toBlock":"latest",
"address":"ADDRESS",
"topics":[
"TOPIC"
]
}
],
"id":1,
"jsonrpc":"2.0"
}' 'ENDPOINT'
where
- BLOCK_HEX is the block number from which to start the logs retrieval in hex format.
- ADDRESS is the address that you want to retrieve the related logs from.
- TOPIC is the hashed event signature.
- ENDPOINT is the endpoint of your node. See also Ethereum tools, Polygon tools, BNB Smart Chain tools.
Example to retrieve all transfer event logs for a Chainlink contract starting from block 13438564:
curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_getLogs",
"params":
[
{
"fromBlock": "0xCD0E64",
"toBlock":"latest",
"address":"0x514910771af9ca656af840dff83e8264ecf986ca",
"topics":[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
],
"id":1,
"jsonrpc":"2.0"
}' 'https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d'
eth_getFilterLogs
Create a new filter
curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_newFilter",
"params":
[
{
"fromBlock": "BLOCK_HEX",
"toBlock":"latest",
"address":"ADDRESS",
"topics":[
"TOPIC"
]
}
],
"id":1,
"jsonrpc":"2.0"
}' 'ENDPOINT'
where
- BLOCK_HEX is the block number from which to start the logs retrieval in hex format.
- ADDRESS is the address that you want to retrieve the related logs from.
- TOPIC is the hashed event signature.
- ENDPOINT is the endpoint of your node. See also Ethereum tools, Polygon tools, BNB Smart Chain tools.
Example to create a filter for all transfer event logs for a Chainlink contract starting from block 13438564:
$ curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_newFilter",
"params":
[
{
"fromBlock": "0xCD0E64",
"toBlock":"latest",
"address":"0x514910771af9ca656af840dff83e8264ecf986ca",
"topics":[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
],
"id":1,
"jsonrpc":"2.0"
}' 'https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d'
{"jsonrpc":"2.0","id":1,"result":"0xb85dcf61893301a80392d3551aebbfa6"}
The result value is the created filter.
Retrieve the logs with the created filter
curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_getFilterLogs",
"params": [
"FILTER"
],
"id":1,
"jsonrpc":"2.0"
}' 'ENDPOINT'
where
- FILTER is the filter you created at the previous step.
- ENDPOINT is the endpoint of your node. See also Ethereum tools, Polygon tools, BNB Smart Chain tools.
Example to retrieve all logs for the filter 0xb85dcf61893301a80392d3551aebbfa6:
curl -XPOST -H "Content-type: application/json" -d '{
"method": "eth_getFilterLogs",
"params": [
"0xb85dcf61893301a80392d3551aebbfa6"
],
"id":1,
"jsonrpc":"2.0"
}' 'https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d'
Comments
0 comments
Article is closed for comments.