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

Instance Method Summary collapse

Methods inherited from Honeymaker::Client

#initialize, rate_limits, #validate

Constructor Details

This class inherits a constructor from Honeymaker::Client

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

#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 —



138
139
140
141
142
143
144
# File 'lib/honeymaker/clients/kraken.rb', line 138

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



124
125
126
# File 'lib/honeymaker/clients/kraken.rb', line 124

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



128
129
130
# File 'lib/honeymaker/clients/kraken.rb', line 128

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



132
133
134
# File 'lib/honeymaker/clients/kraken.rb', line 132

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