Events & Logs
Events are signals that are emitted from smart contracts. Every event is logged, immutable and accessible using a blockchain node only. Smart contracts can not access events themself.
Client-Applications can either listen to events and act accordingly or use logged events to access historical data.
The example uses a public contract and paginates thru the results.
Example Data
This example used below will utilize the VTHO contract, which manages VeChain's VTHO Token.
Smart Contract Address:
0x0000000000000000000000000000456e65726779
The contract's source code can be found on GitHub at: https://github.com/vechain/thor/blob/f58c17ae50f1ec8698d9daf6e05076d17dcafeaf/builtin/gen/energy.sol
Its Application Binary Interface (ABI) is shared on b32, a repository that gathers publicly available interfaces for VeChain projects: https://github.com/vechain/b32/blob/master/ABIs/energy.json
contracts.load(address, abi)
contracts.load(address, abi)
A contract instance using address and ABI definition provides instant access to logs.
Create Contract Object
To create a contract object it needs to be created from the thor client:
The Contract-Loader always requires a JSON ABI Definition.
Fragments are not supported.
contract.filters.<EventName>()
contract.filters.<EventName>()
The filters provide a simple way to access events in a human-readable way and to populate log requests with the correct criteria.
For example, a filter for all Transfers
can be created using:
Another example filters for all transfers to a specific address:
The parameters are the indexed parameters of the event. Unwanted filters can be skipped by passing null.
Get Logs
To receive the logs from the blockchain, the built filter object provides a .get()
function. Calling it will return the list of matching events.
For pagination, there are three optional parameters that allow filtering for a specific range, paginating, and ordering the result set. All options are optional:
Browse Results
.get()
returns a list of results because it can support multiple requests as well.
The data is available in both raw and decoded forms:
Example Project
filterEventLogs()
: Multiple Events in one Request
filterEventLogs()
: Multiple Events in one RequestWith filterEventLogs()
, logs for multiple events can be requested in a single request, improving network performance and simplifying interaction.
For example, requesting VTHO Transfers from and to an address in one request:
filterRawEventLogs(criteria)
filterRawEventLogs(criteria)
Request Logs
Due to the potentially large amount of log entries, it is essential to implement filtering and pagination mechanisms for efficient data access.
To access and retrieve logs, the logs.filterRawEventLogs
function allows filtering based on a specified range and enables pagination by utilizing offset and limits.
Illustrated in the following example is the process of retrieving transfer events for VTHO tokens. Initially, the event that requires filtering must be encoded into a byte format.
indexed
variables can be filtered directly in the filter request, providing fast access to a subset of information. The list argument on the encoding can provide them.
Our example will filter for the second variable to
:
With the encoded version logs.filterRawEventLogs
can be called to return all matching logs:
For pagination options
({ offset?: number, limit?: number }
) and order
('asc' | 'desc'
) can be passed as additional parameter. Additionally the logs can be restricted to a certain block range by defining a range
({ unit?: 'block' | 'time', from?: number, to?: number }
).
You can find the full documentation in the description of the Filter Event Logs Options.
Handle Response
The response needs to be decoded using the event definition to gain access to all information. event.decodeEventLog(log)
can be used on a selected event or in our example on all returned logs:
Each decoded log will have an attribute for each event variable, like decodedLog.from
and can alternatively be accessed as list in the order of the event parameter definition (decodedLog[0]
equals from
).
The types of the results are fully documented in the Event Logs Interface.
Example Project
Last updated