R/account.R

Defines functions coin_value_wallets coin_wallets_lite coin_wallets coin_assets coin_fiat_withdraw coin_api_permission coin_fiat_deposit get_trade_fee coin_account account_headers api_secret api_key register_account

#' @export
register_account <- function(api_key, api_secret, keep_registered = F){
  if(keep_registered){
    set_renv("BIN_API" = api_key)
    set_renv("BIN_SK" = api_secret)
  } else {
    Sys.setenv("BIN_API" = api_key)
    Sys.setenv("BIN_SK" = api_secret)
  }
}

#' @export
api_key <- function() Sys.getenv("BIN_API")
#' @export
api_secret <- function() Sys.getenv("BIN_SK")

#' @export
account_headers <- function(){
  c(accept = "application/json",
    `User-Agent` = 'binance/python',
    `X-MBX-APIKEY` = api_key())
}

#' @export
coin_account <- function(){
  bi_get(type = "api", signed = T, path = "account") %>%
    purrr::imap_dfc(~{
      if(length(.x) == 0) return(NULL)
      if(.y == "balances") return(tibble::tibble(balances = list(.x %>% purrr::map_dfr(tibble::as_tibble))))

      tibble::tibble(a = .x) %>% purrr::set_names(.y)
    }) %>%
    janitor::clean_names()
}

#' @export
get_trade_fee <- function(){
  bi_get(type = "sapi", signed = T, version = bi_params$secure_api_version, path = "asset/tradeFee") %>%
    purrr::map_dfr(tibble::as_tibble)
}

#' @export
coin_fiat_deposit <- function(){
  bi_get(type = "margin", signed = T, path = "fiat/orders", data = tibble::tibble(transactionType = "0", beginTime = 0)) %>%
    purrr::imap_dfc(~{
      if(length(.x) == 0) return(NULL)
      if(.y == "data") return(.x %>% purrr::map_dfr(tibble::as_tibble))
      return(NULL)
    }) %>%
    janitor::clean_names()
}

#' @export
coin_api_permission <- function(){
  binancer:::bi_get(type = "margin", signed = T, path = "account/apiRestrictions") %>%
    tibble::as_tibble() %>%
    janitor::clean_names()
}

#' @export
coin_fiat_withdraw <- function(){
  bi_get(type = "margin", signed = T, path = "fiat/orders", data = tibble::tibble(transactionType = "1", beginTime = 0)) %>%
    purrr::imap_dfc(~{
      if(length(.x) == 0) return(NULL)
      if(.y == "data") return(.x %>% purrr::map_dfr(tibble::as_tibble))
      return(NULL)
    }) %>%
    janitor::clean_names()
}

#' @export
coin_assets <- function(assets = NULL){
  tmp <- coin_account()[["balances"]][[1]]
  if(is.null(tmp)) stop("Wallet could not be retrieved")

  out <- tmp %>%
    dplyr::mutate(free = as.numeric(free),
                  locked = as.numeric(locked)) %>%
    dplyr::filter(as.numeric(free) > 0 | as.numeric(locked) > 0)

  if(!is.null(assets)){
    out <- out %>% filter(asset %in% assets)
  }

  out

}

#' @export
coin_wallets <- function(){

  prices <- coin_prices() %>%
    dplyr::bind_rows(tibble::tibble(symbol = "USDTUSDT", price = 1))

  assets <- coin_assets()

  dplyr::bind_rows(
    assets %>%
      dplyr::mutate(symbol = paste0(asset, "USDT")) %>%
      dplyr::inner_join(prices, "symbol") %>%
      dplyr::filter(!is.na(price)),
    assets %>%
      dplyr::mutate(symbol = paste0("USDT", asset)) %>%
      dplyr::inner_join(prices, "symbol") %>%
      dplyr::mutate(price = 1/price) %>%
      dplyr::filter(!is.na(price))
  ) %>%
    dplyr::distinct(symbol, .keep_all = T) %>%
    dplyr::mutate(value = free * price + locked * price) %>%
    dplyr::select(-symbol)

}

#' @export
coin_wallets_lite <- function(){

  assets <- coin_assets()

  list_assets <-  intersect(c(paste0(assets$asset, "USDT"), paste0(assets$asset, "USDT")), symbols()$symbol) %>%
    paste0('"', ., '"', collapse = ',') %>%
    paste0("[", ., "]")

  prices <- bi_get(path = "ticker/price", data = list(symbols = list_assets))

  if(!is.null(prices$code) && prices$code == "-1121"){
    stop("A symbol was not found when calling coin_wallets_lite")
  }

  prices <- prices %>%
    purrr::map_dfr(tibble::as_tibble) %>%
    dplyr::mutate(price = as.numeric(price)) %>%
    dplyr::bind_rows(tibble::tibble(symbol = "USDTUSDT", price = 1))

  dplyr::bind_rows(
    assets %>%
      dplyr::mutate(symbol = paste0(asset, "USDT")) %>%
      dplyr::inner_join(prices, "symbol") %>%
      dplyr::filter(!is.na(price)),
    assets %>%
      dplyr::mutate(symbol = paste0("USDT", asset)) %>%
      dplyr::inner_join(prices, "symbol") %>%
      dplyr::mutate(price = 1/price) %>%
      dplyr::filter(!is.na(price))
  ) %>%
    dplyr::distinct(symbol, .keep_all = T) %>%
    dplyr::mutate(value = free * price + locked * price) %>%
    dplyr::select(-symbol)

}

#' @export
coin_value_wallets <- function(account){

  coin_wallets_lite() %>%
    dplyr::summarise(value = sum(value)) %>%
    dplyr::pull(value)
}
benjaminguinaudeau/binancer documentation built on Aug. 8, 2022, 12:05 a.m.