module Tradecow::Account

Public Class Methods

balances(target) click to toggle source
# File lib/tradecow/account.rb, line 27
def self.balances target
  # spot
  # otc
  # point
  # margin
  account_id = ids.select{|r| r["type"] == target}.first["id"]
  path = "/v1/account/accounts/#{account_id}/balance"
  req_url = Tradecow::Network.url('GET', path)
  i = 0
  begin
    HTTParty.get(req_url).parsed_response.dig("data", "list").sort_by{|x| x["balance"]}.reverse
  rescue Exception => e
    puts "#{e}"
    i += 1
    if i <= 3
      puts 'Retrying'
      retry
    end
  end
end
ids(target=nil) click to toggle source
# File lib/tradecow/account.rb, line 5
def self.ids target=nil
  path = "/v1/account/accounts"
  req_url = Tradecow::Network.url('GET', path)
  i = 0
  account_list =
    begin
      HTTParty.get(req_url).parsed_response.dig("data")
    rescue Exception => e
      puts "#{e}"
      i += 1
      if i <= 3
        puts 'Retrying'
        retry
      end
    end
  if target.nil?
    account_list
  else
    account_list.select{|x| x["type"] == target}.first
  end
end
transfer(trans, amount, symbol) click to toggle source
# File lib/tradecow/account.rb, line 48
def self.transfer trans, amount, symbol
  # spot to future: "pro-to-futures"
  # future to spot: "futures-to-pro"
  path = "/v1/futures/transfer"
  options = {
    currency: symbol.upcase,
    amount: amount.to_d.truncate(8).to_s,
    type: trans
  }
  req_url = Tradecow::Network.url('POST', path, options)
  begin
    r = HTTParty.post(req_url,
      body: options.to_json,
      headers: {"Content-Type" => "application/json"}).parsed_response
    if r.dig('status') == 'error'
      puts "Failed, msg: #{r.dig('err-msg')}"
    elsif r.dig('status') == 'ok'
      puts 'Done'
    else
      puts 'Unknown response: '
      puts r
    end
    r
  rescue Exception => e
    puts "#{e}"
  end
end