Class: Krkt::Kraken

Inherits:
Object
  • Object
show all
Defined in:
lib/krkt/kraken.rb

Overview

classe para processar saldos & transacoes trades e ledger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Kraken

Returns API kraken - obter saldos & transacoes trades e ledger.

Parameters:

  • dad (Hash)

    todos os dados bigquery

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :h (Hash) — default: {}

    configuracao dias ajuste reposicionamento temporal

  • :v (Boolean) — default: false

    mostra dados transacoes trades & ledger?

  • :t (Boolean) — default: false

    mostra transacoes todas ou somente novas?



22
23
24
25
26
27
# File 'lib/krkt/kraken.rb', line 22

def initialize(dad, pop)
  # API kraken base
  @api = Client.new
  @dbq = dad
  @ops = pop
end

Instance Attribute Details

#apiClient (readonly)

Returns API kraken.

Returns:



10
11
12
# File 'lib/krkt/kraken.rb', line 10

def api
  @api
end

#dbqArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



12
13
14
# File 'lib/krkt/kraken.rb', line 12

def dbq
  @dbq
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



14
15
16
# File 'lib/krkt/kraken.rb', line 14

def ops
  @ops
end

Instance Method Details

#dkrHash

Returns dados kraken - saldos & transacoes trades e ledger.

Returns:

  • (Hash)

    dados kraken - saldos & transacoes trades e ledger



30
31
32
33
34
35
36
# File 'lib/krkt/kraken.rb', line 30

def dkr
  @dkr ||= {
    sl: api.balance,
    kt: api.trades_history,
    kl: api.ledger
  }
end

#formata_saldos(moe, sal) ⇒ String

Returns texto formatado saldos (kraken/bigquery) & iguais/ok/nok?.

Returns:

  • (String)

    texto formatado saldos (kraken/bigquery) & iguais/ok/nok?



61
62
63
64
65
66
67
68
69
70
# File 'lib/krkt/kraken.rb', line 61

def formata_saldos(moe, sal)
  t = dbq[:sl][moe.downcase.to_sym].to_d
  format(
    '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
    mo: moe,
    kr: sal,
    bq: t,
    ok: t == sal ? 'OK' : 'NOK'
  )
end

#formata_transacao_ledger(idx, hlx) ⇒ String

Returns texto formatado transacao ledger.

Returns:

  • (String)

    texto formatado transacao ledger



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/krkt/kraken.rb', line 91

def formata_transacao_ledger(idx, hlx)
  format(
    '%<ky>-6.6s %<dt>19.19s %<ty>-10.10s %<mo>-4.4s %<pr>18.7f %<vl>18.7f',
    ky: idx,
    dt: Time.at(hlx['time']),
    ty: hlx['type'],
    mo: hlx['asset'],
    pr: hlx['amount'].to_d,
    vl: hlx['fee'].to_d
  )
end

#formata_transacao_trades(idx, htx) ⇒ String

Returns texto formatado transacao trade.

Returns:

  • (String)

    texto formatado transacao trade



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/krkt/kraken.rb', line 75

def formata_transacao_trades(idx, htx)
  format(
    '%<ky>-6.6s %<dt>19.19s %<ty>-10.10s %<mo>-8.8s %<pr>8.2f %<vl>15.7f %<co>8.2f',
    ky: idx,
    dt: Time.at(htx['time']),
    ty: "#{htx['type']}/#{htx['ordertype']}",
    mo: htx['pair'],
    pr: htx['price'].to_d,
    vl: htx['vol'].to_d,
    co: htx['cost'].to_d
  )
end

#kylArray<String>

Returns lista txid de transacoes ledger.

Returns:

  • (Array<String>)

    lista txid de transacoes ledger



44
45
46
# File 'lib/krkt/kraken.rb', line 44

def kyl
  @kyl ||= dkr[:kl]['ledger'].keys - (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
end

#kytArray<String>

Returns lista txid de transacoes trades.

Returns:

  • (Array<String>)

    lista txid de transacoes trades



39
40
41
# File 'lib/krkt/kraken.rb', line 39

def kyt
  @kyt ||= dkr[:kt]['trades'].keys - (ops[:t] ? [] : dbq[:nt].map { |e| e[:txid] })
end

#ledgerHash

Returns transacoes ledger.

Returns:

  • (Hash)

    transacoes ledger



54
55
56
# File 'lib/krkt/kraken.rb', line 54

def ledger
  @ledger ||= dkr[:kl]['ledger'].select { |k, _| kyl.include?(k) }
end

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



124
125
126
127
128
129
# File 'lib/krkt/kraken.rb', line 124

def mostra_ledger
  return unless ops[:v] && ledger.count.positive?

  puts("\nledger data       hora     tipo       moeda -------quantidade -------------custo")
  ledger.each { |k, v| puts(formata_transacao_ledger(k, v)) }
end

#mostra_resumoString

Returns texto saldos & transacoes & ajuste dias.

Returns:

  • (String)

    texto saldos & transacoes & ajuste dias



104
105
106
107
108
109
110
111
112
113
# File 'lib/krkt/kraken.rb', line 104

def mostra_resumo
  puts("\nmoeda          saldo kraken        saldo bigquery")
  dkr[:sl].each { |k, v| puts(formata_saldos(k, v.to_d)) }

  mostra_trades
  mostra_ledger
  return unless trades.count.positive?

  puts("\nstring ajuste dias dos trades\n-h=#{kyt.map { |e| "#{e}:0" }.join(' ')}")
end

#mostra_tradesString

Returns texto transacoes trades.

Returns:

  • (String)

    texto transacoes trades



116
117
118
119
120
121
# File 'lib/krkt/kraken.rb', line 116

def mostra_trades
  return unless ops[:v] && trades.count.positive?

  puts("\ntrade  data       hora     tipo       par      ---preco ---------volume ---custo")
  trades.each { |k, v| puts(formata_transacao_trades(k, v)) }
end

#tradesHash

Returns transacoes trades.

Returns:

  • (Hash)

    transacoes trades



49
50
51
# File 'lib/krkt/kraken.rb', line 49

def trades
  @trades ||= dkr[:kt]['trades'].select { |k, _| kyt.include?(k) }
end