Method: CoinSync::Importers::KucoinAPI#import_balances

Defined in:
lib/coinsync/importers/kucoin_api.rb

#import_balancesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/coinsync/importers/kucoin_api.rb', line 72

def import_balances
  page = 1
  full_list = []

  loop do
    response = make_request('/account/balances', limit: 20, page: page)

    case response
    when Net::HTTPSuccess
      json = JSON.parse(response.body)

      if json['success'] != true || json['code'] != 'OK'
        raise "Kucoin importer: Invalid response: #{response.body}"
      end

      data = json['data']
      list = data && data['datas']

      if !list
        raise "Kucoin importer: No data returned: #{response.body}"
      end

      full_list.concat(list)

      page += 1
      break if page > data['pageNos']
    when Net::HTTPBadRequest
      raise "Kucoin importer: Bad request: #{response}"
    else
      raise "Kucoin importer: Bad response: #{response}"
    end
  end

  full_list.delete_if { |b| b['balance'] == 0.0 && b['freezeBalance'] == 0.0 }

  full_list.map do |b|
    Balance.new(
      CryptoCurrency.new(b['coinType']),
      available: BigDecimal.new(b['balanceStr']),
      locked: BigDecimal.new(b['freezeBalanceStr'])
    )
  end
end