Skip to content

Commit 1547749

Browse files
Merge pull request #119 from next-game-solutions/dev-tx-functions-optimisation
Closes #119
2 parents 78bf079 + c18aa88 commit 1547749

File tree

6 files changed

+58
-52
lines changed

6 files changed

+58
-52
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# tronr 0.1.2
2+
3+
## Minor improvements
4+
5+
* Functions `parse_tx_info()` (non-public utility function), `get_block_info()` and `get_blocks_for_time_range()` now work a little bit faster by doing less work (e.g., by not converting the API-defined variable names to snake case).
6+
17
# tronr 0.1.1
28

39
## Minor improvements and bug fixes

R/get_block_info.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ get_block_info <- function(latest = TRUE,
7070
attr(request_time, "tzone") <- "UTC"
7171

7272
r <- api_request(url = url, max_attempts = max_attempts)
73-
7473
names(r) <- snakecase::to_snake_case(names(r))
7574

7675
result <- tibble::as_tibble(r) %>%
@@ -113,7 +112,7 @@ get_block_info <- function(latest = TRUE,
113112
params = list(
114113
sort = "-timestamp",
115114
count = tolower(TRUE),
116-
limit = 25,
115+
limit = 50,
117116
block = result$block_number
118117
)
119118
)

R/get_blocks_for_time_range.R

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,39 @@ get_blocks_for_time_range <- function(min_timestamp,
4545
path = c("api", "block"),
4646
params = list(
4747
sort = "-number",
48-
limit = 25,
48+
limit = 50,
4949
start_timestamp = min_timestamp,
5050
end_timestamp = max_timestamp
5151
),
5252
max_attempts = max_attempts
5353
)
5454

5555
result <- lapply(data, function(x) {
56-
names(x) <- snakecase::to_snake_case(names(x))
57-
5856
tibble::as_tibble(x) %>%
5957
dplyr::mutate(
6058
number = as.character(.data$number),
6159
timestamp = from_unix_timestamp(.data$timestamp)
6260
) %>%
6361
dplyr::rename(
64-
tx_count = .data$nr_of_trx,
62+
tx_count = .data$nrOfTrx,
6563
block_number = .data$number
6664
)
6765
}) %>%
6866
dplyr::bind_rows() %>%
6967
dplyr::select(
70-
.data$block_number,
71-
.data$timestamp,
72-
.data$hash,
73-
.data$parent_hash,
74-
.data$tx_trie_root,
75-
.data$confirmed,
76-
.data$revert,
77-
.data$size,
78-
.data$witness_address,
79-
.data$witness_name,
80-
.data$tx_count,
81-
.data$net_usage,
82-
.data$energy_usage
68+
block_number = .data$block_number,
69+
timestamp = .data$timestamp,
70+
hash = .data$hash,
71+
parent_hash = .data$parentHash,
72+
tx_trie_root = .data$txTrieRoot,
73+
confirmed = .data$confirmed,
74+
revert = .data$revert,
75+
size = .data$size,
76+
witness_address = .data$witnessAddress,
77+
witness_name = .data$witnessName,
78+
tx_count = .data$tx_count,
79+
net_usage = .data$netUsage,
80+
energy_usage = .data$energyUsage
8381
)
8482

8583
return(result)

R/get_tx_for_time_range.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ get_tx_for_time_range <- function(min_timestamp = 0,
4343
path = c("api", "transaction"),
4444
params = list(
4545
sort = "timestamp",
46-
limit = 25,
46+
limit = 50,
4747
count = tolower(TRUE),
4848
start_timestamp = min_timestamp,
4949
end_timestamp = max_timestamp

R/parse_tx_info.R

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ parse_tx_info <- function(info) {
2323
ifelse(!is.null(x), x, NA)
2424
}
2525

26-
names(info) <- snakecase::to_snake_case(names(info))
27-
28-
contract_data_names <-
29-
names(info$contract_data) <-
30-
snakecase::to_snake_case(names(info$contract_data))
31-
26+
info_names <- names(info)
3227

3328
# Core info:
3429
tx_id <- info$hash
@@ -37,28 +32,28 @@ parse_tx_info <- function(info) {
3732

3833
timestamp <- from_unix_timestamp(info$timestamp)
3934

40-
contract_result <- info$contract_ret
35+
contract_result <- info$contractRet
4136

4237
confirmed <- info$confirmed
4338

4439
confirmations_count <- info$confirmations
4540

46-
contract_type <- convert_contract_type_id(info$contract_type)
41+
contract_type <- convert_contract_type_id(info$contractType)
4742

48-
from_address <- info$owner_address
43+
from_address <- info$ownerAddress
4944

50-
to_address <- ifelse(nchar(info$to_address) != 0, info$to_address, NA)
45+
to_address <- ifelse(nchar(info$toAddress) != 0, info$toAddress, NA)
5146

52-
is_contract_from_address <- unlist(info$contract_map[info$owner_address])
47+
is_contract_from_address <- unlist(info$contract_map[info$ownerAddress])
5348

5449
is_contract_to_address <- ifelse(is.na(to_address),
5550
to_address,
56-
unlist(info$contract_map[info$to_address])
51+
unlist(info$contract_map[info$toAddress])
5752
)
5853

5954
costs <- list(tibble::as_tibble(info$cost))
6055

61-
sr_confirm_list <- lapply(info$sr_confirm_list, function(x) {
56+
sr_confirm_list <- lapply(info$srConfirmList, function(x) {
6257
tibble::as_tibble(x) %>%
6358
dplyr::mutate(block = as.character(.data$block)) %>%
6459
dplyr::rename(block_number = .data$block) %>%
@@ -71,21 +66,19 @@ parse_tx_info <- function(info) {
7166
if (contract_type == "TransferAssetContract") {
7267
trx_transfer <- 0
7368

74-
if ("token_info" %in% contract_data_names &
75-
is.list(info$contract_data$token_info) &
76-
length(info$contract_data$token_info) != 0) {
77-
token_info <- info$contract_data$token_info
78-
79-
names(token_info) <- snakecase::to_snake_case(names(token_info))
69+
if ("tokenInfo" %in% names(info$contractData) &
70+
is.list(info$contractData$tokenInfo) &
71+
length(info$contractData$tokenInfo) != 0) {
72+
token_info <- info$contractData$tokenInfo
8073

8174
trc10_transfer <- tibble::tibble(
82-
token_id = as.character(info$contract_data$asset_name),
83-
token_name = null_checker(token_info$token_name),
84-
token_abbr = null_checker(token_info$token_abbr),
75+
token_id = as.character(info$contractData$asset_name),
76+
token_name = null_checker(token_info$tokenName),
77+
token_abbr = null_checker(token_info$tokenAbbr),
8578
vip = null_checker(token_info$vip),
8679
amount = apply_decimal(
87-
info$contract_data$amount,
88-
token_info$token_decimal
80+
info$contractData$amount,
81+
token_info$tokenDecimal
8982
)
9083
)
9184

@@ -98,17 +91,17 @@ parse_tx_info <- function(info) {
9891

9992
# TransferContract - used only to transfer TRX:
10093
if (contract_type == "TransferContract") {
101-
trx_transfer <- apply_decimal(as.numeric(info$contract_data$amount), 6)
94+
trx_transfer <- apply_decimal(as.numeric(info$contractData$amount), 6)
10295
trc10_transfer <- NA
10396
}
10497

10598

10699
# TriggerSmartContract:
107100
if (contract_type == "TriggerSmartContract") {
108-
trx_transfer <- ifelse(!"call_value" %in% names(info$contract_data) ||
109-
nchar(info$contract_data$call_value) == 0,
101+
trx_transfer <- ifelse(!"call_value" %in% names(info$contractData) ||
102+
nchar(info$contractData$call_value) == 0,
110103
0,
111-
apply_decimal(as.numeric(info$contract_data$call_value), 6)
104+
apply_decimal(as.numeric(info$contractData$call_value), 6)
112105
)
113106

114107
trc10_transfer <- NA
@@ -126,10 +119,10 @@ parse_tx_info <- function(info) {
126119
}
127120

128121

129-
if ("trc_20_transfer_info" %in% names(info) &
130-
is.list(info$trc_20_transfer_info) &
131-
length(info$trc_20_transfer_info) != 0) {
132-
trc20_transfer <- lapply(info$trc_20_transfer_info, function(x) {
122+
if ("trc20TransferInfo" %in% names(info) &
123+
is.list(info$trc20TransferInfo) &
124+
length(info$trc20TransferInfo) != 0) {
125+
trc20_transfer <- lapply(info$trc20TransferInfo, function(x) {
133126
tibble::tibble(
134127
token_name = x$name,
135128
token_abbr = x$symbol,

R/run_paginated_tronscan_query.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ run_paginated_tronscan_query <- function(base_url,
5252
)
5353

5454
r <- api_request(url = url, max_attempts = max_attempts)
55+
56+
if ("message" %in% names(r)) {
57+
rlang::abort("Some API parameters are invalid or out of range")
58+
}
59+
5560
names(r) <- snakecase::to_snake_case(names(r))
5661

5762
previous_data_length <- length(data)
@@ -85,6 +90,11 @@ run_paginated_tronscan_query <- function(base_url,
8590
)
8691

8792
r <- api_request(url = url, max_attempts = max_attempts)
93+
94+
if ("message" %in% names(r)) {
95+
rlang::abort("Some API parameters are invalid or out of range")
96+
}
97+
8898
names(r) <- snakecase::to_snake_case(names(r))
8999

90100
previous_data_length <- length(data)

0 commit comments

Comments
 (0)