Class: CoinSync::Importers::KrakenAPI

Inherits:
Base
  • Object
show all
Includes:
CoinSync::Importers::Kraken::Common
Defined in:
lib/coinsync/importers/kraken_api.rb

Constant Summary collapse

BASE_URL =
"https://api.kraken.com"
API_RENEWAL_INTERVAL =
3.0

Instance Method Summary collapse

Methods included from CoinSync::Importers::Kraken::Common

#build_transaction_list

Methods inherited from Base

#can_build?, register_commands, register_importer, registered_commands

Constructor Details

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

Returns a new instance of KrakenAPI.



24
25
26
27
28
29
# File 'lib/coinsync/importers/kraken_api.rb', line 24

def initialize(config, params = {})
  super
  @api_key = params['api_key']
  @secret_api_key = params['private_key']
  @decoded_secret = Base64.decode64(@secret_api_key) if @secret_api_key
end

Instance Method Details

#can_import?(type) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/coinsync/importers/kraken_api.rb', line 31

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

#import_balancesObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/coinsync/importers/kraken_api.rb', line 81

def import_balances
  response = make_request('/0/private/Balance')

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

    if !json['error'].empty? || !json['result']
      raise "Kraken importer: Invalid response: #{response.body}"
    end

    return json['result'].map { |k, v|
      [Kraken::LedgerEntry.parse_currency(k), BigDecimal.new(v)]
    }.select { |currency, amount|
      amount > 0 && currency.crypto?
    }.map { |currency, amount|
      Balance.new(currency, available: amount)
    }
  when Net::HTTPBadRequest
    raise "Kraken importer: Bad request: #{response.body}"
  else
    raise "Kraken importer: Bad response: #{response.body}"
  end
end

#import_transactions(filename) ⇒ Object



35
36
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/coinsync/importers/kraken_api.rb', line 35

def import_transactions(filename)
  offset = 0
  entries = []
  slowdown = false

  loop do
    response = make_request('/0/private/Ledgers', ofs: offset)
    print slowdown ? '-' : '.'
    sleep(2 * API_RENEWAL_INTERVAL) if slowdown   # rate limiting

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

      if json['result'].nil? || json['error'].length > 0
        if json['error'].first == 'EAPI:Rate limit exceeded'
          slowdown = true
          print '!'
          sleep(4 * API_RENEWAL_INTERVAL)
          next
        else
          raise "Kraken importer: Invalid response: #{response.body}"
        end
      end

      data = json['result']
      list = data && data['ledger']

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

      break if list.empty?

      entries.concat(list.values)
      offset += list.length
    when Net::HTTPBadRequest
      raise "Kraken importer: Bad request: #{response.body}"
    else
      raise "Kraken importer: Bad response: #{response.body}"
    end
  end

  File.write(filename, JSON.pretty_generate(entries) + "\n")
end

#read_transaction_list(source) ⇒ Object



106
107
108
109
110
# File 'lib/coinsync/importers/kraken_api.rb', line 106

def read_transaction_list(source)
  json = JSON.parse(source.read)

  build_transaction_list(json.map { |hash| Kraken::LedgerEntry.from_json(hash) })
end