Class: Cns::Kraken

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

Overview

classe para processar transacoes trades/ledger do kraken

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
# File 'lib/cns/kraken.rb', line 22

def initialize(dad, pop)
  @api = Apice.new
  @bqd = dad
  @ops = pop
end

Instance Attribute Details

#apiApius (readonly)

Returns API kraken.

Returns:

  • (Apius)

    API kraken



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

def api
  @api
end

#bqdArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



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

def bqd
  @bqd
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



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

def ops
  @ops
end

Instance Method Details

#exdHash

Returns dados exchange kraken - saldos & transacoes trades e ledger.

Returns:

  • (Hash)

    dados exchange kraken - saldos & transacoes trades e ledger



52
53
54
55
56
57
58
# File 'lib/cns/kraken.rb', line 52

def exd
  @exd ||= {
    sl: api.,
    kt: api.trades_us,
    kl: api.ledger_us
  }
end

#formata_ledger(idx, hlx) ⇒ String

Returns texto formatado ledger.

Examples:

ledger_us

{
  error: [],
  result: {
    ledger: {
      "LXXURB-ITI7S-CXVERS": {
        refid: 'ACCHF3A-RIBBMO-VYBESY',
        time: 1_543_278_716.2775,
        type: 'withdrawal',
        subtype: '',
        aclass: 'currency',
        asset: 'ZEUR',
        amount: '-15369.6200',
        fee: '0.0900',
        balance: '0.0062'
      },
      "OUTRO-LEDGER-ID": {}
    },
    count: 376
  }
}

Parameters:

  • hlx (Hash)

    ledger kraken

  • idx (String)

    identificador transacao

Returns:

  • (String)

    texto formatado ledger



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cns/kraken.rb', line 105

def formata_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].upcase,
    pr: hlx[:amount].to_d,
    vl: hlx[:fee].to_d
  )
end

#formata_saldos(moe, sal) ⇒ String

Returns texto formatado saldos.

Examples:

account_us

{
 error: [],
 result: {
   ZEUR: '0.0038',
   XXBT: '0.0000000000',
   XETH: '1.0000000000',
   XETC: '0.0000000000',
   EOS: '0.0000001700',
   BCH: '0.0000000000'
 }
}

Parameters:

  • moe (String)

    codigo kraken da moeda

  • sal (BigDecimal)

    saldo kraken da moeda

Returns:

  • (String)

    texto formatado saldos



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

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

#formata_trades(idx, htx) ⇒ String

Returns texto formatado trade.

Examples:

trades_us

{
  error: [],
  result: {
    trades: {
      "TVINF5-TIOUB-YFNGKE": {
        ordertxid: 'ORPSUW-YKP4F-UJZOC6',
        pair: 'XETHXXBT',
        time: 1_463_435_684.8387,
        type: 'buy',
        ordertype: 'market',
        price: '0.024989',
        cost: '1.193973',
        fee: '0.003104',
        vol: '47.77994129',
        margin: '0.000000',
        misc: ''
      },
      "OUTRO-TRADE-ID": {}
    },
    count: 157
  }
}

Returns:

  • (String)

    texto formatado trade



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

def formata_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].upcase,
    pr: htx[:price].to_d,
    vl: htx[:vol].to_d,
    co: htx[:cost].to_d
  )
end

#kylArray<String>

Returns lista txid dos ledger novos.

Returns:

  • (Array<String>)

    lista txid dos ledger novos



66
67
68
# File 'lib/cns/kraken.rb', line 66

def kyl
  @kyl ||= exd[:kl].keys - (ops[:t] ? [] : bqd[:nl].map { |obj| obj[:txid].to_sym })
end

#kytArray<String>

Returns lista txid dos trades novos.

Returns:

  • (Array<String>)

    lista txid dos trades novos



61
62
63
# File 'lib/cns/kraken.rb', line 61

def kyt
  @kyt ||= exd[:kt].keys - (ops[:t] ? [] : bqd[:nt].map { |obj| obj[:txid].to_sym })
end

#ledgerHash

Returns ledger kraken novos.

Returns:

  • (Hash)

    ledger kraken novos



34
35
36
# File 'lib/cns/kraken.rb', line 34

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

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



137
138
139
140
141
142
# File 'lib/cns/kraken.rb', line 137

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

  puts("\nledger data       hora     tipo       moeda        quantidade              custo")
  ledger.sort { |ant, prx| prx[1][:time] <=> ant[1][:time] }.each { |key, val| puts(formata_ledger(key, val)) }
end

#mostra_resumoString

Returns texto saldos & transacoes & ajuste dias.

Returns:

  • (String)

    texto saldos & transacoes & ajuste dias



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cns/kraken.rb', line 39

def mostra_resumo
  puts("\nKRAKEN\ntipo                 kraken              bigquery")
  exd[:sl].sort { |ant, prx| ant <=> prx }.each { |key, val| puts(formata_saldos(key, val)) }
  mostra_totais

  mostra_trades
  mostra_ledger
  return if trades.empty?

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

#mostra_totaisString

Returns texto totais numero de transacoes.

Returns:

  • (String)

    texto totais numero de transacoes



118
119
120
121
122
123
124
125
126
# File 'lib/cns/kraken.rb', line 118

def mostra_totais
  vkt = exd[:kt].count
  vnt = bqd[:nt].count
  vkl = exd[:kl].count
  vnl = bqd[:nl].count

  puts("TRADES #{format('%<a>20i %<b>21i %<o>3.3s', a: vkt, b: vnt, o: vkt == vnt ? 'OK' : 'NOK')}")
  puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: vkl, d: vnl, o: vkl == vnl ? 'OK' : 'NOK')}")
end

#mostra_tradesString

Returns texto transacoes trades.

Returns:

  • (String)

    texto transacoes trades



129
130
131
132
133
134
# File 'lib/cns/kraken.rb', line 129

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

  puts("\ntrade  data       hora     tipo       par         preco          volume    custo")
  trades.sort { |ant, prx| prx[1][:time] <=> ant[1][:time] }.each { |key, val| puts(formata_trades(key, val)) }
end

#tradesHash

Returns trades kraken novos.

Returns:

  • (Hash)

    trades kraken novos



29
30
31
# File 'lib/cns/kraken.rb', line 29

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