There are two functions in tronr
that can be used to query the current balance of a TRON account. Note that the term “account” here refers to both wallet-like accounts and smart contracts.
The get_account_trx_balance()
function retrieves the current amount of Tronix (TRX) held by an account. The main argument of this function is address
, which (similar to all tronr
functions that take address
as one of their inputs) accepts account addresses in either base58check
(start with T
) or hex
format (start with 41
).
library(tronr)
library(dplyr)
library(tidyr)
(bal <- get_account_trx_balance(address = "TQjaZ9FD473QBTdUzMLmSyoGB6Yz1CGpux"))
#> # A tibble: 1 x 3
#> request_time address trx_balance
#> <dttm> <chr> <dbl>
#> 1 2021-07-12 22:04:31 TQjaZ9FD473QBTdUzMLmSyoGB6Yz1CGpux 3927692.
The obtained TRX balance can be converted to a fiat currency, such as USD, using the get_current_trx_price()
function (this function makes a call to the CoinGecko API to obtain the current TRX price in the respective currency):
price <- get_current_trx_price(vs_currencies = c("usd"))
bal$trx_balance * price$trx_price
#> [1] 236973.4
A more comprehensive view of an account’s balance can be obtained using the get_account_balance()
function, which returns a nested tibble:
r <- get_account_balance(address = "TQjaZ9FD473QBTdUzMLmSyoGB6Yz1CGpux")
glimpse(r)
#> Rows: 1
#> Columns: 10
#> $ request_time <dttm> 2021-07-12 22:04:32
#> $ address <chr> "TQjaZ9FD473QBTdUzMLmSyoGB6Yz1CGpux"
#> $ name <chr> "SunTRXV3Pool"
#> $ total_tx <int> 69237
#> $ bandwidth <list> [<tbl_df[1 x 20]>]
#> $ trx_balance <dbl> 3927692
#> $ n_trc20 <int> 16
#> $ trc20 <list> [<tbl_df[16 x 7]>]
#> $ n_trc10 <int> 19
#> $ trc10 <list> [<tbl_df[19 x 8]>]
The nested structure of this tibble can be seen in columns bandwidth
(account’s current energy and bandwidth resources), trc20
(TRC-20 tokens held by the account), and trc10
(TRC-10 tokens held by the account). Each of these columns contains a list with a single element - another tibble with the corresponding data. These nested data can be retrieved either by using the standard R indexing or by applying the tidyr::unnest()
function:
# Retrieve data on the TRC-20 tokens held by the account,
# using the standard R indexing:
r$trc20[[1]]
#> # A tibble: 16 x 7
#> token_contract_ad… token_name token_abbr token_type balance token_price_in_…
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 TKkeiboTkxXKJpbmV… SUNOLD SUNOLD trc20 2.39e+1 319.
#> 2 TASrsr1Qsz8eUSaji… HYPE Token HYPE trc20 1.25e-1 NA
#> 3 TQXhxDPNKVsgxYbJa… GoldenEggs GLDE trc20 2 e-6 NA
#> 4 TRZPqchWTRUzXNGNW… AISwap AIS trc20 1 e+0 NA
#> 5 TDndaG9V79f3dVuVQ… ANB Finance ANB trc20 1.12e-7 NA
#> 6 THSjCwmYVeK1oLR3T… EAB Finance EAB trc20 1.11e-3 NA
#> 7 TCwsQqyVfTNwNm5Go… MAVRONA CO… MAVRONA trc20 2 e+0 NA
#> 8 TM6oGQHC9Fq6d9two… NEXA Token NEXA trc20 4.12e-1 NA
#> 9 TY2p1N32AEBtpqTqQ… GOLF Token GOLF trc20 1 e-2 NA
#> 10 TLvdAAh7hxDWvvhGw… StakeAnbFi… SKE trc20 1 e-6 NA
#> 11 TU7uTrWbnt9RVQJgX… CLAM HOLDI… CLAM trc20 1 e-6 NA
#> 12 THJPvg68wH4wbAkjJ… O K E X _ … OKU trc20 1 e-6 NA
#> 13 TV2DYBjPY2SJuHQmu… AAA DeFi T… AAA trc20 1 e+0 NA
#> 14 TUAbNGTPe7uCMcduG… SNDCOIN SNDC trc20 1 e-2 NA
#> 15 TVogNDwsyGYuEcAZd… 42 Defense 42D trc20 1 e-1 NA
#> 16 TMqgAJSuMEq5EQWBs… EAB Finance EAB trc20 1 e-4 NA
#> # … with 1 more variable: vip <lgl>
# Retrieve the same data by unnesting the tibble:
r %>% select(trc20) %>% unnest(cols = trc20)
#> # A tibble: 16 x 7
#> token_contract_ad… token_name token_abbr token_type balance token_price_in_…
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 TKkeiboTkxXKJpbmV… SUNOLD SUNOLD trc20 2.39e+1 319.
#> 2 TASrsr1Qsz8eUSaji… HYPE Token HYPE trc20 1.25e-1 NA
#> 3 TQXhxDPNKVsgxYbJa… GoldenEggs GLDE trc20 2 e-6 NA
#> 4 TRZPqchWTRUzXNGNW… AISwap AIS trc20 1 e+0 NA
#> 5 TDndaG9V79f3dVuVQ… ANB Finance ANB trc20 1.12e-7 NA
#> 6 THSjCwmYVeK1oLR3T… EAB Finance EAB trc20 1.11e-3 NA
#> 7 TCwsQqyVfTNwNm5Go… MAVRONA CO… MAVRONA trc20 2 e+0 NA
#> 8 TM6oGQHC9Fq6d9two… NEXA Token NEXA trc20 4.12e-1 NA
#> 9 TY2p1N32AEBtpqTqQ… GOLF Token GOLF trc20 1 e-2 NA
#> 10 TLvdAAh7hxDWvvhGw… StakeAnbFi… SKE trc20 1 e-6 NA
#> 11 TU7uTrWbnt9RVQJgX… CLAM HOLDI… CLAM trc20 1 e-6 NA
#> 12 THJPvg68wH4wbAkjJ… O K E X _ … OKU trc20 1 e-6 NA
#> 13 TV2DYBjPY2SJuHQmu… AAA DeFi T… AAA trc20 1 e+0 NA
#> 14 TUAbNGTPe7uCMcduG… SNDCOIN SNDC trc20 1 e-2 NA
#> 15 TVogNDwsyGYuEcAZd… 42 Defense 42D trc20 1 e-1 NA
#> 16 TMqgAJSuMEq5EQWBs… EAB Finance EAB trc20 1 e-4 NA
#> # … with 1 more variable: vip <lgl>