A detailed description of how blocks are produced on the TRON blockchain can be found in the official documentation. At a high level, blocks are logical units that combine transactions that took place on the blockchain within a certain period of time. On the TRON network this time is set to 3 seconds.
There are two main functions in the tronr
package to make block-specific queries. The first one, get_block_info()
, retrieves a set of attributes either for the latest generated block or for a specific block referred to by its number. In both cases, this function will return a nested tibble - see help file for definitions of the respective columns (?get_block_info
).
library(tronr)
library(dplyr)
library(tidyr)
# Get data for the latest block (argument `latest = TRUE`):
r1 <- get_block_info(latest = TRUE)
glimpse(r1)
#> Rows: 1
#> Columns: 11
#> $ request_time <dttm> 2021-07-12 22:04:36
#> $ block_number <chr> "31884282"
#> $ timestamp <dttm> 2021-07-12 22:03:30
#> $ hash <chr> "0000000001e683faae0da0b7731c68f9425f067eb9e292f9d880…
#> $ parent_hash <chr> "0000000001e683f9499ada64c41608d0d4d69fe0a52d10fd149d8…
#> $ tx_trie_root <chr> "Pyz3DYHk7aoMugwVrg93mEbMxZBpcyT5eU9vcBFuvXdjhYpXE"
#> $ confirmed <lgl> TRUE
#> $ size <int> 38656
#> $ witness_address <chr> "TJBtdYunmQkeK5KninwgcjuK1RPDhyUWBZ"
#> $ tx_count <int> 128
#> $ tx <list> [<tbl_df[128 x 4]>]
# Get data for a specific block:
r2 <- get_block_info(latest = FALSE, block_number = "26810333")
glimpse(r2)
#> Rows: 1
#> Columns: 11
#> $ request_time <dttm> 2021-07-12 22:04:37
#> $ block_number <chr> "26810333"
#> $ timestamp <dttm> 2021-01-16 15:15:03
#> $ hash <chr> "00000000019917ddec79f24f784de288030d6277e93628e94a3f…
#> $ parent_hash <chr> "00000000019917dc0d7c15284a73c99337e7a18b3177aae9b8d25…
#> $ tx_trie_root <chr> "2SHS8nZSw62MGx6BLJ4NY5DuxsBP23MzkrV6M5dCyHaTaRwHmW"
#> $ confirmed <lgl> TRUE
#> $ size <int> 23826
#> $ witness_address <chr> "TN2W4cc7a4dsYyTLiLMWa9m7jVpdLjGvYs"
#> $ tx_count <int> 92
#> $ tx <list> [<tbl_df[92 x 4]>]
One of the most important columns in the returned tibble is tx
- a list with one element, which contains a tibble with basic info on transactions associated with the queried block:
r1$tx[[1]] %>% glimpse()
#> Rows: 128
#> Columns: 4
#> $ tx_id <chr> "fec78d485c08b4a999eb10fea6a1f8bf4840778512bab7cd5d1229d…
#> $ contract_type <chr> "TransferAssetContract", "TransferAssetContract", "Trans…
#> $ from_address <chr> "TDyx416rT7Wn7CTwkJpJk7haPgxd8YJuT7", "TGHhUj67RRtdtuEt3…
#> $ to_address <chr> "TWwV71US9KXNKRHyECt691D3MZ1KkR7VFJ", "TFs1CxvVDychokzne…
Another function, get_blocks_for_time_range()
, retrieves attributes of the blocks produced within a user-specified period of time. This period is definied with the min_timestamp
and max_timestamp
arguments, both of which expect Unix timestamps (including milliseconds):
# Use utility function `to_unix_timestamp()` to convert POSIXct
# datetime values to Unix timestamps
r <- get_blocks_for_time_range(
min_timestamp = "1551715200000",
max_timestamp = "1551715210000"
)
glimpse(r)
#> Rows: 4
#> Columns: 13
#> $ block_number <chr> "7203852", "7203851", "7203850", "7203849"
#> $ timestamp <dttm> 2019-03-04 16:00:09, 2019-03-04 16:00:06, 2019-03-04 1…
#> $ hash <chr> "00000000006dec0cd76d5eee4bea21802b0e73dd67460d3407a4…
#> $ parent_hash <chr> "00000000006dec0b36bce19a101eef08f3c7b8a16223481a37dea…
#> $ tx_trie_root <chr> "2Y3SaM5CxAEM1LAKw9F2sCHKU4ThodSuiqi4zftgHvZRpdVWph", …
#> $ confirmed <lgl> TRUE, TRUE, TRUE, TRUE
#> $ revert <lgl> FALSE, FALSE, FALSE, FALSE
#> $ size <int> 22632, 20701, 21046, 19363
#> $ witness_address <chr> "TJ2aDMgeipmoZRuUEru2ri8t7TGkxnm6qY", "TVFKwzE8qeETLaZ…
#> $ witness_name <chr> "Huobi_pool", "BlockchainOrg", "lianjinshu", "callmeSR"
#> $ tx_count <int> 83, 72, 75, 61
#> $ net_usage <int> 36919, 24628, 34877, 22663
#> $ energy_usage <int> 1996195, 1488224, 1307323, 1919043
In contrast to the get_block_info()
function, get_blocks_for_time_range()
does not return data on transactions associated with the respective blocks - this is to speed up the query and processing time, which can be substantial for longer periods of time. However, if transaction data are required, users can apply get_block_info()
to the list of blocks returned by get_blocks_for_time_range()
:
r_tx <- sapply(r$block_number,
function(x) {get_block_info(latest = FALSE,
block_number = x)
}, simplify = FALSE) %>%
bind_rows()
# Overview of the resultant nested tibble:
glimpse(r_tx)
#> Rows: 4
#> Columns: 11
#> $ request_time <dttm> 2021-07-12 22:04:38, 2021-07-12 22:04:41, 2021-07-12 …
#> $ block_number <chr> "7203852", "7203851", "7203850", "7203849"
#> $ timestamp <dttm> 2019-03-04 16:00:09, 2019-03-04 16:00:06, 2019-03-04 1…
#> $ hash <chr> "00000000006dec0cd76d5eee4bea21802b0e73dd67460d3407a4…
#> $ parent_hash <chr> "00000000006dec0b36bce19a101eef08f3c7b8a16223481a37dea…
#> $ tx_trie_root <chr> "2Y3SaM5CxAEM1LAKw9F2sCHKU4ThodSuiqi4zftgHvZRpdVWph", …
#> $ confirmed <lgl> TRUE, TRUE, TRUE, TRUE
#> $ size <int> 22632, 20701, 21046, 19363
#> $ witness_address <chr> "TJ2aDMgeipmoZRuUEru2ri8t7TGkxnm6qY", "TVFKwzE8qeETLaZ…
#> $ tx_count <int> 83, 72, 75, 61
#> $ tx <list> [<tbl_df[83 x 4]>], [<tbl_df[72 x 4]>], [<tbl_df[75 x …
# Unnest transaction data:
r_tx %>%
select(block_number, tx) %>%
unnest(cols = tx) %>%
glimpse()
#> Rows: 291
#> Columns: 5
#> $ block_number <chr> "7203852", "7203852", "7203852", "7203852", "7203852", "…
#> $ tx_id <chr> "e0bb731fa4a5e9d0c8d88f9dfeb896d4c26e11260a854d7c53fe5d2…
#> $ contract_type <chr> "TriggerSmartContract", "TriggerSmartContract", "Trigger…
#> $ from_address <chr> "TYo75v3FXdE3m7N3qLZLauqsGedQibVRBY", "TNNc1HGDUrRkowQxd…
#> $ to_address <chr> "TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3", "TCuAmDG9EfPQu6Ysy…