When a smart contract is run, the underlying TRON Virtual Machine (TVM) emits various bits of information in the form of logs, which are known as events. Events often contain useful data related to how the respective smart contract operates. The tronr
package offers several functions that can be used to query events based on the block number, transaction ID, or contract address.
Events associated with the latest block can be retrieved using the get_events_of_latest_block()
function, which returns a nested tibble:
library(tronr)
library(dplyr)
library(tidyr)
latest_events <- get_events_of_latest_block()
glimpse(latest_events)
#> Rows: 5
#> Columns: 6
#> $ tx_id <chr> "253c2baac1c5e786e0ffb7b40bb8613f00b5055d41f36267ffe2…
#> $ block_number <chr> "31884307", "31884307", "31884307", "31884307", "3188…
#> $ timestamp <dttm> 2021-07-12 22:04:45, 2021-07-12 22:04:45, 2021-07-12…
#> $ contract_address <chr> "TDWHKzbuvwjVXSjgAerRnphMk7yW1Hka7t", "TMqNKeXA6dfHtH…
#> $ event_name <chr> "Transfer", "Snapshot", "TrxPurchase", "Approval", "T…
#> $ event_data <list> ["0x0000000000000000000000000000000000000000", "0x70…
Event data are stored in the list-column event_data
, where each element is a named list with attributes of the respective event, e.g.:
latest_events$event_data[[1]]
#> $`0`
#> [1] "0x0000000000000000000000000000000000000000"
#>
#> $`1`
#> [1] "0x70f43c9381a996f18a71624be5a7fe9cbcac26db"
#>
#> $`2`
#> [1] "204645900"
#>
#> $from
#> [1] "0x0000000000000000000000000000000000000000"
#>
#> $to
#> [1] "0x70f43c9381a996f18a71624be5a7fe9cbcac26db"
#>
#> $value
#> [1] "204645900"
latest_events$event_data[[2]]
#> named list()
The above examples illustrate that elements of the event_data
column are contract- and event-specific. As the structure of event data emitted by every smart contract existing on the TRON blockchain is impossible to know a priori, these event attributes are returned “as-is”, i.e. without any parsing or other pre-processing. Users are advised to develop their own, task-specific, logic to process event attributes.
Event data associated with a specific block can be retrieved using the function get_events_by_block_number()
:
block_event <- get_events_by_block_number(block_number = "15354550")
glimpse(block_event)
#> Rows: 23
#> Columns: 6
#> $ tx_id <chr> "ba39943229b81ff7d48058647cd825c1f637c544d24c42a9480d…
#> $ block_number <chr> "15354550", "15354550", "15354550", "15354550", "1535…
#> $ timestamp <dttm> 2019-12-14 10:05:30, 2019-12-14 10:05:30, 2019-12-14…
#> $ contract_address <chr> "TFRLPB1kiCyKRSM9e7vFn8KqN66qeTGVUv", "TUezhPtdMYByML…
#> $ event_name <chr> "RingCreate", "Transfer", "RingCreate", "DiceResult",…
#> $ event_data <list> ["310956", "0xcf0b932f6811c41ad48f7d62da60e81ca09352…
Similarly, functions get_events_by_tx_id()
and get_events_by_contact_address()
allow one to retrieve event data for a specific transaction or smart contract address:
# Query by transaction ID:
tx_id <- "270e992bf22a271008032ec09a51616ed963b74f4d808b0fd74fc82341a81391"
tx_events <- get_events_by_tx_id(tx_id = tx_id)
glimpse(tx_events)
#> Rows: 5
#> Columns: 6
#> $ tx_id <chr> "270e992bf22a271008032ec09a51616ed963b74f4d808b0fd74f…
#> $ block_number <chr> "26014725", "26014725", "26014725", "26014725", "2601…
#> $ timestamp <dttm> 2020-12-19 16:52:15, 2020-12-19 16:52:15, 2020-12-19…
#> $ contract_address <chr> "TJRq8Sc2Dnx2PJZYccr37BdHdqVt1X2j89", "TUTik4srgKuzgX…
#> $ event_name <chr> "onSponsor", "onTokenPurchase", "onPrice", "onLeaderB…
#> $ event_data <list> ["0x12e5f88b4ae1abebefa501dcdca654f9cac075ce", "0x5d…
# Query by contract address (bound to a time range):
contract_address <- "TKttnV3FSY1iEoAwB4N52WK2DxdV94KpSd"
min_timestamp <- "1576317786000"
max_timestamp <- "1576317996000"
contr_events <- get_events_by_contract_address(
address = contract_address,
min_timestamp = min_timestamp,
max_timestamp = max_timestamp
)
glimpse(contr_events)
#> Rows: 17
#> Columns: 6
#> $ tx_id <chr> "d52e307c4b3b506c1f8347311fca1ddd9017dfff61d89731de3f…
#> $ block_number <chr> "15354570", "15354566", "15354566", "15354562", "1535…
#> $ timestamp <dttm> 2019-12-14 10:06:30, 2019-12-14 10:06:18, 2019-12-14…
#> $ contract_address <chr> "TKttnV3FSY1iEoAwB4N52WK2DxdV94KpSd", "TKttnV3FSY1iEo…
#> $ event_name <chr> "Transfer", "Transfer", "Approval", "Transfer", "Tran…
#> $ event_data <list> ["0x1d0f4031f9e3eeeb727b10e462ab0e59ee06a2a6", "0xa4…
# Еhe last query could also be filtered by the name
# of a specifc event of interest:
contr_events_filtered <- get_events_by_contract_address(
address = contract_address,
min_timestamp = min_timestamp,
max_timestamp = max_timestamp,
event_name = "Transfer",
direction = "asc"
)
glimpse(contr_events_filtered)
#> Rows: 12
#> Columns: 6
#> $ tx_id <chr> "33a93686582338448d88ab2dbda1090526641e4648cdb0506416…
#> $ block_number <chr> "15354508", "15354517", "15354519", "15354537", "1535…
#> $ timestamp <dttm> 2019-12-14 10:03:24, 2019-12-14 10:03:51, 2019-12-14…
#> $ contract_address <chr> "TKttnV3FSY1iEoAwB4N52WK2DxdV94KpSd", "TKttnV3FSY1iEo…
#> $ event_name <chr> "Transfer", "Transfer", "Transfer", "Transfer", "Tran…
#> $ event_data <list> ["0x1d0f4031f9e3eeeb727b10e462ab0e59ee06a2a6", "0x3c…