Contract Events
What is contract events
Contract events are a way for smart contracts, such as those on the Ethereum blockchain, to communicate action or changes to external entities or systems. They enable the logging of on-chain actions which can be captured and monitored off the blockchain by subscribers. These events are defined in the smart contract code, and they can be emitted during the execution of contract functions to provide information about transactions or changes in state, like token transfers or contract updates. This event-driven architecture facilitates efficient and scalable off-chain processing of blockchain interactions.
The event data in the contract is structured, and we can create a table function to analyze them.
How to create
E.g. If you want to create a table function for ERC20 Transfer event. You can find its event ABI from etherscan
{"anonymous":false,
"inputs":[
{"indexed":true,"internalType":"address","name":"from","type":"address"},
{"indexed":true,"internalType":"address","name":"to","type":"address"},
{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}
],
"name":"Transfer",
"type":"event"
}Create a table function for Transfer event named tranfer_erc20:
pragma blockduck_create_contract_event_rpc('tranfer_erc20', '{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}');inputs
suffix
VARCHAR
Table function suffix to identify the specific event
event_abi
VARCHAR
JSON ABI of the event to be monitored
Example
D pragma blockduck_create_contract_event_rpc('tranfer_erc20', '{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}');
βββββββββββ
β Success β
β boolean β
βββββββββββ€
β 0 rows β
βββββββββββHow to query
After that, table function named eth_contract_event_rpc_tranfer_erc20 already been created. Then you can try to query from it.
Inputs
address
LIST(VARCHAR)
Contract address emitting the event
fromBlock
BigInt
Block number to start scanning from
toBlock
BigInt
Block number to stop scanning at
url
VARCHAR
URL of the Ethereum node
Examples
D set variable eth_url = 'https://eth-mainnet.g.alchemy.com/v2/{API_KEY}';
D select * from eth_contract_event_rpc_tranfer_erc20([]::VARCHAR[], 21104078, 21104078, getvariable('eth_url')) limit 10;
ββββββββββββββββββββββββ¬ββββββββββββββ¬βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ
β address β blockNumber β transactionHash β from β to β amount β
β varchar β int64 β varchar β varchar β varchar β varint β
ββββββββββββββββββββββββΌββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β 0xabec00542d141bddβ¦ β 21104078 β 0x15b539fdf722615aβ¦ β 0x000000000000000000000000abd055069a6β¦ β 0x000000000000000000000000abec00542d141bddf58649bfe860c6449807237c β 105836228163203292253 β
β 0xabec00542d141bddβ¦ β 21104078 β 0x15b539fdf722615aβ¦ β 0x000000000000000000000000abd055069a6β¦ β 0x00000000000000000000000060a8ea6005f7db580bc0c9341e7e6275d114e874 β 10477786588157125933126 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0x15b539fdf722615aβ¦ β 0x0000000000000000000000001f2f10d1c40β¦ β 0x000000000000000000000000abd055069a6b04db7d1547f88dd01cf14ff09cfd β 886249995970281472 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0x15b539fdf722615aβ¦ β 0x00000000000000000000000060a8ea6005fβ¦ β 0x0000000000000000000000001f2f10d1c40777ae1da742455c65828ff36df387 β 188832235783192576 β
β 0xabec00542d141bddβ¦ β 21104078 β 0x1bd4d5006a6b321cβ¦ β 0x00000000000000000000000008903a3be1eβ¦ β 0x000000000000000000000000abec00542d141bddf58649bfe860c6449807237c β 9748415873909063929026 β
β 0xabec00542d141bddβ¦ β 21104078 β 0x1bd4d5006a6b321cβ¦ β 0x00000000000000000000000008903a3be1eβ¦ β 0x00000000000000000000000060a8ea6005f7db580bc0c9341e7e6275d114e874 β 965093171516997328973575 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0x1bd4d5006a6b321cβ¦ β 0x00000000000000000000000060a8ea6005fβ¦ β 0x0000000000000000000000003fc91a3afd70395cd496c647d5a6cc9d4b2b7fad β 14935959444018469899 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0x1bd4d5006a6b321cβ¦ β 0x0000000000000000000000003fc91a3afd7β¦ β 0x000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c β 37339898610046174 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0xe391af23df899353β¦ β 0x000000000000000000000000abd055069a6β¦ β 0x0000000000000000000000001f2f10d1c40777ae1da742455c65828ff36df387 β 910473774728478721 β
β 0xc02aaa39b223fe8dβ¦ β 21104078 β 0xe391af23df899353β¦ β 0x0000000000000000000000001f2f10d1c40β¦ β 0x00000000000000000000000060a8ea6005f7db580bc0c9341e7e6275d114e874 β 173454427502411776 β
ββββββββββββββββββββββββ΄ββββββββββββββ΄βββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββ€
β 10 rows 6 columns β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββDependent RPC Method
eth_getLogs
Last updated