-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_helpers.R
73 lines (65 loc) · 1.47 KB
/
wallet_helpers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#' Wallet helper functions
#'
#' These helpers are simple getter functions for convenient access to specific wallet information.
#'
#' @param x a wallet list object. See \link{wallet}.
#' @name wallet_helpers
#'
#' @return vectorized results pertaining to each element in \code{x}. For \code{transactions}, a combined data frame with a wallet \code{address} column.
#'
#' @examples
#' \dontrun{
#' x <- wallet("1KennyH9grzif79WbaQDHpqgTnm25j4rRj")
#' hash(x)
#' address(x)
#' received(x)
#' sent(x)
#' balance(x)
#' txn(x)
#' transactions(x)
#' }
NULL
#' @export
#' @rdname wallet_helpers
hash <- function(x){
.stop_wallet(x)
purrr::map_chr(x, "hash160")
}
#' @export
#' @rdname wallet_helpers
address <- function(x){
.stop_wallet(x)
purrr::map_chr(x, "address")
}
#' @export
#' @rdname wallet_helpers
received <- function(x){
.stop_wallet(x)
purrr::map_dbl(x, "total_received")
}
#' @export
#' @rdname wallet_helpers
sent <- function(x){
.stop_wallet(x)
purrr::map_dbl(x, "total_sent")
}
#' @export
#' @rdname wallet_helpers
balance <- function(x){
.stop_wallet(x)
purrr::map_dbl(x, "final_balance")
}
#' @export
#' @rdname wallet_helpers
txn <- function(x){
.stop_wallet(x)
purrr::map_int(x, "n_tx")
}
#' @export
#' @rdname wallet_helpers
transactions <- function(x){
id <- address(x)
purrr::map2(x, id, ~dplyr::mutate(.x$txs, address = .y)) %>%
dplyr::bind_rows()
}
.stop_wallet <- function(x) if(!"wallet" %in% class(x)) stop("Input is not a `wallet` list.")