Class: Honeymaker::Clients::Kraken

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

Constant Summary collapse

URL =
"https://api.kraken.com"
RATE_LIMITS =
{ default: 1000, orders: 1000 }.freeze
ASSET_MAP =
{
  "ZUSD" => "USD", "ZEUR" => "EUR", "ZGBP" => "GBP",
  "ZJPY" => "JPY", "ZCHF" => "CHF", "ZCAD" => "CAD",
  "ZAUD" => "AUD", "XXBT" => "XBT", "XETH" => "ETH",
  "XXDG" => "XDG"
}.freeze

Constants inherited from Honeymaker::Client

Honeymaker::Client::OPTIONS

Instance Attribute Summary

Attributes inherited from Honeymaker::Client

#api_key, #api_secret

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Honeymaker::Client

#initialize, rate_limits, #validate

Constructor Details

This class inherits a constructor from Honeymaker::Client

Class Method Details

.reset_nonce_state!Object



313
314
315
# File 'lib/honeymaker/clients/kraken.rb', line 313

def self.reset_nonce_state!
  @@nonce_mutex.synchronize { @@last_nonces.clear }
end

Instance Method Details

#add_order(ordertype:, type:, volume:, pair:, userref: nil, cl_ord_id: nil, displayvol: nil, price: nil, price2: nil, trigger: nil, leverage: nil, reduce_only: nil, stptype: nil, oflags: [], timeinforce: nil, starttm: nil, expiretm: nil, close: nil, close_price: nil, close_price2: nil, deadline: nil, validate: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/honeymaker/clients/kraken.rb', line 37

def add_order(ordertype:, type:, volume:, pair:, userref: nil, cl_ord_id: nil,
              displayvol: nil, price: nil, price2: nil, trigger: nil, leverage: nil,
              reduce_only: nil, stptype: nil, oflags: [], timeinforce: nil,
              starttm: nil, expiretm: nil, close: nil, close_price: nil,
              close_price2: nil, deadline: nil, validate: nil)
  result = post_private("/0/private/AddOrder", {
    "nonce" => nonce, "ordertype" => ordertype, "type" => type,
    "volume" => volume, "pair" => pair, "userref" => userref,
    "cl_ord_id" => cl_ord_id, "displayvol" => displayvol,
    "price" => price, "price2" => price2, "trigger" => trigger,
    "leverage" => leverage, "reduce_only" => reduce_only,
    "stptype" => stptype,
    "oflags" => oflags.any? ? oflags.join(",") : nil,
    "timeinforce" => timeinforce, "starttm" => starttm,
    "expiretm" => expiretm, "close[ordertype]" => close,
    "close[price]" => close_price, "close[price2]" => close_price2,
    "deadline" => deadline, "validate" => validate
  })
  return result if result.failure?

  errors = result.data["error"]
  return Result::Failure.new(*errors) if errors.is_a?(Array) && errors.any?

  txid = (result.data.dig("result", "txid") || []).first
  Result::Success.new({ order_id: txid, raw: result.data })
end

#cancel_order(txid: nil, cl_ord_id: nil) ⇒ Object



64
65
66
# File 'lib/honeymaker/clients/kraken.rb', line 64

def cancel_order(txid: nil, cl_ord_id: nil)
  post_private("/0/private/CancelOrder", { nonce: nonce, txid: txid, cl_ord_id: cl_ord_id })
end

#closed_orders_from_trades(order_ids:, start: nil, max_pages: 20) ⇒ Object

Reconcile orders that QueryOrders has already dropped (Kraken removes terminal orders within hours) against TradesHistory, which is authoritative for executions and effectively unbounded in retention. Returns ONLY orders that actually executed, keyed by ordertxid, each as a per-order executed aggregate. Orders with no trades (never filled / truly gone) are simply absent from the result.

‘start` should be a unix timestamp bounding the lookback (e.g. the order’s creation time) so we don’t page the user’s entire trade history.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/honeymaker/clients/kraken.rb', line 132

def closed_orders_from_trades(order_ids:, start: nil, max_pages: 20)
  wanted = Array(order_ids)
  return Result::Success.new({}) if wanted.empty?

  by_order = Hash.new { |h, k| h[k] = [] }
  offset = 0
  pages = 0
  loop do
    result = get_trades_history(start: start, ofs: offset)
    return result if result.failure?

    errors = result.data["error"]
    return Result::Failure.new(*errors) if errors.is_a?(Array) && errors.any?

    trades = result.data.dig("result", "trades") || {}
    break if trades.empty?

    trades.each_value do |t|
      otxid = t["ordertxid"]
      by_order[otxid] << t if wanted.include?(otxid)
    end

    # Do NOT early-exit when each id has been *seen* — a partial fill can have trades on
    # later pages. Page the whole [start, now] window (bounded by `start` + max_pages).
    pages += 1
    offset += trades.size
    count = result.data.dig("result", "count").to_i
    break if offset >= count || pages >= max_pages
  end

  Result::Success.new(by_order.transform_values { |trades| aggregate_trades(trades) })
end

#get_asset_info(assets: nil, aclass: nil) ⇒ Object



74
75
76
# File 'lib/honeymaker/clients/kraken.rb', line 74

def get_asset_info(assets: nil, aclass: nil)
  get_public("/0/public/Assets", { asset: assets ? assets.join(",") : nil, aclass: aclass })
end

#get_balancesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/honeymaker/clients/kraken.rb', line 86

def get_balances
  result = get_extended_balance
  return result if result.failure?

  errors = result.data["error"]
  return Result::Failure.new(*errors) if errors.is_a?(Array) && errors.any?

  balances = {}
  (result.data["result"] || {}).each do |symbol, balance|
    mapped_symbol = ASSET_MAP[symbol.split(".").first] || symbol.split(".").first
    total = BigDecimal(balance["balance"].to_s)
    locked = BigDecimal((balance["hold_trade"] || "0").to_s)
    free = total - locked
    next if free.zero? && locked.zero?
    balances[mapped_symbol] = { free: free, locked: locked }
  end

  Result::Success.new(balances)
end

#get_earn_allocations(ascending: nil, converted_asset: nil, hide_zero_allocations: nil) ⇒ Object

— Earn —



179
180
181
182
183
184
185
# File 'lib/honeymaker/clients/kraken.rb', line 179

def get_earn_allocations(ascending: nil, converted_asset: nil, hide_zero_allocations: nil)
  post_private("/0/private/Earn/Allocations", {
    nonce: nonce, ascending: ascending,
    converted_asset: converted_asset,
    hide_zero_allocations: hide_zero_allocations
  })
end

#get_extended_balanceObject



82
83
84
# File 'lib/honeymaker/clients/kraken.rb', line 82

def get_extended_balance
  post_private("/0/private/BalanceEx", { nonce: nonce })
end

#get_ledgers(asset: nil, type: nil, start: nil, end_time: nil, ofs: nil) ⇒ Object



117
118
119
120
121
122
# File 'lib/honeymaker/clients/kraken.rb', line 117

def get_ledgers(asset: nil, type: nil, start: nil, end_time: nil, ofs: nil)
  post_private("/0/private/Ledgers", {
    nonce: nonce, asset: asset, type: type,
    start: start, end: end_time, ofs: ofs
  })
end

#get_ohlc_data(pair:, interval: nil, since: nil) ⇒ Object



106
107
108
# File 'lib/honeymaker/clients/kraken.rb', line 106

def get_ohlc_data(pair:, interval: nil, since: nil)
  get_public("/0/public/OHLC", { pair: pair, interval: interval, since: since })
end

#get_ticker_information(pair: nil) ⇒ Object



78
79
80
# File 'lib/honeymaker/clients/kraken.rb', line 78

def get_ticker_information(pair: nil)
  get_public("/0/public/Ticker", { pair: pair })
end

#get_tradable_asset_pairs(pairs: nil, info: nil, country_code: nil) ⇒ Object



68
69
70
71
72
# File 'lib/honeymaker/clients/kraken.rb', line 68

def get_tradable_asset_pairs(pairs: nil, info: nil, country_code: nil)
  get_public("/0/public/AssetPairs", {
    pair: pairs ? pairs.join(",") : nil, info: info, country_code: country_code
  })
end

#get_trades_history(type: nil, trades: nil, start: nil, end_time: nil, ofs: nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/honeymaker/clients/kraken.rb', line 110

def get_trades_history(type: nil, trades: nil, start: nil, end_time: nil, ofs: nil)
  post_private("/0/private/TradesHistory", {
    nonce: nonce, type: type, trades: trades,
    start: start, end: end_time, ofs: ofs
  })
end

#get_withdraw_addresses(asset: nil, method: nil) ⇒ Object



165
166
167
# File 'lib/honeymaker/clients/kraken.rb', line 165

def get_withdraw_addresses(asset: nil, method: nil)
  post_private("/0/private/WithdrawAddresses", { nonce: nonce, asset: asset, method: method })
end

#get_withdraw_methods(asset: nil) ⇒ Object



169
170
171
# File 'lib/honeymaker/clients/kraken.rb', line 169

def get_withdraw_methods(asset: nil)
  post_private("/0/private/WithdrawMethods", { nonce: nonce, asset: asset })
end

#query_orders_info(txid:, trades: nil, userref: nil, consolidate_taker: true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/honeymaker/clients/kraken.rb', line 20

def query_orders_info(txid:, trades: nil, userref: nil, consolidate_taker: true)
  result = post_private("/0/private/QueryOrders", {
    nonce: nonce, trades: trades, userref: userref,
    txid: txid, consolidate_taker: consolidate_taker
  })
  return result if result.failure?

  errors = result.data["error"]
  return Result::Failure.new(*errors) if errors.is_a?(Array) && errors.any?

  orders = {}
  (result.data["result"] || {}).each do |order_id, raw|
    orders[order_id] = normalize_order(order_id, raw)
  end
  Result::Success.new(orders)
end

#withdraw(asset:, key:, amount:, address: nil) ⇒ Object



173
174
175
# File 'lib/honeymaker/clients/kraken.rb', line 173

def withdraw(asset:, key:, amount:, address: nil)
  post_private("/0/private/Withdraw", { nonce: nonce, asset: asset, key: key, amount: amount, address: address })
end