Class: CoinSync::Importers::KucoinAPI

Inherits:
Base
  • Object
show all
Defined in:
lib/coinsync/importers/kucoin_api.rb

Defined Under Namespace

Classes: HistoryEntry

Constant Summary collapse

BASE_URL =
"https://api.kucoin.com"

Instance Method Summary collapse

Methods inherited from Base

#can_build?, register_commands, register_importer, registered_commands

Constructor Details

#initialize(config, params = {}) ⇒ KucoinAPI

Returns a new instance of KucoinAPI.



35
36
37
38
39
# File 'lib/coinsync/importers/kucoin_api.rb', line 35

def initialize(config, params = {})
  super
  @api_key = params['api_key']
  @api_secret = params['api_secret']
end

Instance Method Details

#can_import?(type) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/coinsync/importers/kucoin_api.rb', line 41

def can_import?(type)
  @api_key && @api_secret && [:balances, :transactions].include?(type)
end

#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

#import_transactions(filename) ⇒ Object



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
# File 'lib/coinsync/importers/kucoin_api.rb', line 45

def import_transactions(filename)
  # TODO: what if there's more than 100? (looks like we might need to switch to loading each market separately…)
  response = make_request('/order/dealt', limit: 100)

  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

    File.write(filename, JSON.pretty_generate(list) + "\n")
  when Net::HTTPBadRequest
    raise "Kucoin importer: Bad request: #{response}"
  else
    raise "Kucoin importer: Bad response: #{response}"
  end
end

#read_transaction_list(source) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/coinsync/importers/kucoin_api.rb', line 116

def read_transaction_list(source)
  json = JSON.parse(source.read)
  transactions = []

  json.each do |hash|
    entry = HistoryEntry.new(hash)

    if entry.direction == 'BUY'
      transactions << Transaction.new(
        exchange: 'Kucoin',
        time: entry.created_at,
        bought_amount: entry.amount - entry.fee,
        bought_currency: entry.coin_type,
        sold_amount: entry.deal_value,
        sold_currency: entry.coin_type_pair
      )
    elsif entry.direction == 'SELL'
      transactions << Transaction.new(
        exchange: 'Kucoin',
        time: entry.created_at,
        sold_amount: entry.amount,
        sold_currency: entry.coin_type,
        bought_amount: entry.deal_value - entry.fee,
        bought_currency: entry.coin_type_pair
      )
    else
      raise "Kucoin importer error: unexpected entry direction '#{entry.direction}'"
    end
  end

  transactions.reverse
end