Class: Honeymaker::Clients::Kucoin

Inherits:
Honeymaker::Client show all
Defined in:
lib/honeymaker/clients/kucoin.rb

Constant Summary collapse

URL =
"https://api.kucoin.com"
RATE_LIMITS =
{ default: 100, orders: 200 }.freeze

Constants inherited from Honeymaker::Client

Honeymaker::Client::OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Honeymaker::Client

#api_key, #api_secret

Instance Method Summary collapse

Methods inherited from Honeymaker::Client

rate_limits, #validate

Constructor Details

#initialize(api_key: nil, api_secret: nil, passphrase: nil, proxy: nil, logger: nil) ⇒ Kucoin

Returns a new instance of Kucoin.



11
12
13
14
# File 'lib/honeymaker/clients/kucoin.rb', line 11

def initialize(api_key: nil, api_secret: nil, passphrase: nil, proxy: nil, logger: nil)
  super(api_key: api_key, api_secret: api_secret, proxy: proxy, logger: logger)
  @passphrase = passphrase
end

Instance Attribute Details

#passphraseObject (readonly)

Returns the value of attribute passphrase.



9
10
11
# File 'lib/honeymaker/clients/kucoin.rb', line 9

def passphrase
  @passphrase
end

Instance Method Details

#cancel_order(order_id:) ⇒ Object



81
82
83
# File 'lib/honeymaker/clients/kucoin.rb', line 81

def cancel_order(order_id:)
  delete_signed("/api/v1/orders/#{order_id}")
end

#futures_funding_history(symbol:, start_at: nil, end_at: nil, reverse: nil, offset: nil, forward: nil, max_count: nil) ⇒ Object

— Futures —



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/honeymaker/clients/kucoin.rb', line 160

def futures_funding_history(symbol:, start_at: nil, end_at: nil, reverse: nil, offset: nil, forward: nil, max_count: nil)
  with_rescue do
    params = {
      symbol: symbol, startAt: start_at, endAt: end_at,
      reverse: reverse, offset: offset, forward: forward, maxCount: max_count
    }.compact
    query_string = params.empty? ? "" : "?#{Faraday::Utils.build_query(params)}"
    path = "/api/v1/funding-history"
    ts = timestamp_ms.to_s
    pre_sign = "#{ts}GET#{path}#{query_string}"

    response = futures_connection.get do |req|
      req.url path
      req.headers = signed_headers(ts, pre_sign)
      req.params = params
    end
    response.body
  end
end

#get_accounts(currency: nil, type: nil) ⇒ Object



34
35
36
# File 'lib/honeymaker/clients/kucoin.rb', line 34

def get_accounts(currency: nil, type: nil)
  get_signed("/api/v1/accounts", { currency: currency, type: type })
end

#get_all_tickersObject



24
25
26
# File 'lib/honeymaker/clients/kucoin.rb', line 24

def get_all_tickers
  get_public("/api/v1/market/allTickers")
end

#get_balances(type: "trade") ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/honeymaker/clients/kucoin.rb', line 38

def get_balances(type: "trade")
  result = get_accounts(type: type)
  return result if result.failure?

  return Result::Failure.new("KuCoin API error") unless result.data["code"] == "200000"

  balances = {}
  (result.data["data"] || []).each do ||
    symbol = ["currency"]
    free = BigDecimal((["available"] || "0").to_s)
    locked = BigDecimal((["holds"] || "0").to_s)
    next if free.zero? && locked.zero?
    balances[symbol] = { free: free, locked: locked }
  end

  Result::Success.new(balances)
end

#get_currenciesObject



89
90
91
# File 'lib/honeymaker/clients/kucoin.rb', line 89

def get_currencies
  get_public("/api/v3/currencies")
end

#get_deposits(currency: nil, start_at: nil, end_at: nil, status: nil, current_page: nil, page_size: nil) ⇒ Object



114
115
116
117
118
119
# File 'lib/honeymaker/clients/kucoin.rb', line 114

def get_deposits(currency: nil, start_at: nil, end_at: nil, status: nil, current_page: nil, page_size: nil)
  get_signed("/api/v1/deposits", {
    currency: currency, startAt: start_at, endAt: end_at,
    status: status, currentPage: current_page, pageSize: page_size
  })
end

#get_fills(order_id: nil, symbol: nil, side: nil, type: nil, start_at: nil, end_at: nil, trade_type: nil, limit: nil, current_page: nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/honeymaker/clients/kucoin.rb', line 97

def get_fills(order_id: nil, symbol: nil, side: nil, type: nil, start_at: nil, end_at: nil,
              trade_type: nil, limit: nil, current_page: nil)
  get_signed("/api/v1/fills", {
    orderId: order_id, symbol: symbol, side: side, type: type,
    startAt: start_at, endAt: end_at, tradeType: trade_type,
    pageSize: limit, currentPage: current_page
  })
end

#get_historical_orders(symbol: nil, side: nil, start_at: nil, end_at: nil, current_page: nil, page_size: nil) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/honeymaker/clients/kucoin.rb', line 106

def get_historical_orders(symbol: nil, side: nil, start_at: nil, end_at: nil, current_page: nil, page_size: nil)
  get_signed("/api/v1/hist-orders", {
    symbol: symbol, side: side,
    startAt: start_at, endAt: end_at,
    currentPage: current_page, pageSize: page_size
  })
end

#get_klines(symbol:, type:, start_at: nil, end_at: nil) ⇒ Object



28
29
30
31
32
# File 'lib/honeymaker/clients/kucoin.rb', line 28

def get_klines(symbol:, type:, start_at: nil, end_at: nil)
  get_public("/api/v1/market/candles", {
    symbol: symbol, type: type, startAt: start_at, endAt: end_at
  })
end

#get_order(order_id:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/honeymaker/clients/kucoin.rb', line 70

def get_order(order_id:)
  result = get_signed("/api/v1/orders/#{order_id}")
  return result if result.failure?
  return Result::Failure.new("KuCoin API error") unless result.data["code"] == "200000"

  raw = result.data["data"]
  return Result::Failure.new("Order not found") unless raw

  Result::Success.new(normalize_order(order_id, raw))
end

#get_orderbook(symbol:, limit: 20) ⇒ Object



85
86
87
# File 'lib/honeymaker/clients/kucoin.rb', line 85

def get_orderbook(symbol:, limit: 20)
  get_public("/api/v1/market/orderbook/level2_#{limit}", { symbol: symbol })
end

#get_symbolsObject



16
17
18
# File 'lib/honeymaker/clients/kucoin.rb', line 16

def get_symbols
  get_public("/api/v2/symbols")
end

#get_ticker(symbol:) ⇒ Object



20
21
22
# File 'lib/honeymaker/clients/kucoin.rb', line 20

def get_ticker(symbol:)
  get_public("/api/v1/market/orderbook/level1", { symbol: symbol })
end

#get_withdrawal_quotas(currency:, chain: nil) ⇒ Object



93
94
95
# File 'lib/honeymaker/clients/kucoin.rb', line 93

def get_withdrawal_quotas(currency:, chain: nil)
  get_signed("/api/v1/withdrawals/quotas", { currency: currency, chain: chain })
end

#get_withdrawals(currency: nil, start_at: nil, end_at: nil, status: nil, current_page: nil, page_size: nil) ⇒ Object



121
122
123
124
125
126
# File 'lib/honeymaker/clients/kucoin.rb', line 121

def get_withdrawals(currency: nil, start_at: nil, end_at: nil, status: nil, current_page: nil, page_size: nil)
  get_signed("/api/v1/withdrawals", {
    currency: currency, startAt: start_at, endAt: end_at,
    status: status, currentPage: current_page, pageSize: page_size
  })
end

#margin_borrow_history(currency: nil, is_isolated: nil, symbol: nil, order_no: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil) ⇒ Object

— Margin —



137
138
139
140
141
142
# File 'lib/honeymaker/clients/kucoin.rb', line 137

def margin_borrow_history(currency: nil, is_isolated: nil, symbol: nil, order_no: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil)
  get_signed("/api/v3/margin/borrow", {
    currency: currency, isIsolated: is_isolated, symbol: symbol, orderNo: order_no,
    startTime: start_time, endTime: end_time, currentPage: current_page, pageSize: page_size
  })
end

#margin_interest_history(currency: nil, is_isolated: nil, symbol: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil) ⇒ Object



151
152
153
154
155
156
# File 'lib/honeymaker/clients/kucoin.rb', line 151

def margin_interest_history(currency: nil, is_isolated: nil, symbol: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil)
  get_signed("/api/v3/margin/interest", {
    currency: currency, isIsolated: is_isolated, symbol: symbol,
    startTime: start_time, endTime: end_time, currentPage: current_page, pageSize: page_size
  })
end

#margin_repay_history(currency: nil, is_isolated: nil, symbol: nil, order_no: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil) ⇒ Object



144
145
146
147
148
149
# File 'lib/honeymaker/clients/kucoin.rb', line 144

def margin_repay_history(currency: nil, is_isolated: nil, symbol: nil, order_no: nil, start_time: nil, end_time: nil, current_page: nil, page_size: nil)
  get_signed("/api/v3/margin/repay", {
    currency: currency, isIsolated: is_isolated, symbol: symbol, orderNo: order_no,
    startTime: start_time, endTime: end_time, currentPage: current_page, pageSize: page_size
  })
end

#place_order(client_oid:, side:, symbol:, type:, size: nil, funds: nil, price: nil, time_in_force: nil, stp: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/honeymaker/clients/kucoin.rb', line 56

def place_order(client_oid:, side:, symbol:, type:, size: nil, funds: nil, price: nil,
                time_in_force: nil, stp: nil)
  result = post_signed("/api/v1/orders", {
    clientOid: client_oid, side: side, symbol: symbol, type: type,
    size: size, funds: funds, price: price,
    timeInForce: time_in_force, stp: stp
  })
  return result if result.failure?
  return Result::Failure.new("KuCoin API error") unless result.data["code"] == "200000"

  order_id = result.data.dig("data", "orderId")
  Result::Success.new({ order_id: order_id, raw: result.data })
end

#withdraw(currency:, address:, amount:, chain: nil, memo: nil) ⇒ Object



128
129
130
131
132
133
# File 'lib/honeymaker/clients/kucoin.rb', line 128

def withdraw(currency:, address:, amount:, chain: nil, memo: nil)
  post_signed("/api/v1/withdrawals", {
    currency: currency, address: address, amount: amount,
    chain: chain, memo: memo
  })
end