Class: Cex::Kraken

Inherits:
Object
  • Object
show all
Defined in:
lib/cex/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/cex/kraken.rb', line 22

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

Instance Attribute Details

#apiApius (readonly)

Returns API kraken.

Returns:



10
11
12
# File 'lib/cex/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/cex/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/cex/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



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

def exd
  @exd ||= {
    sl: api.,
    kt: api.trades,
    kl: api.ledger
  }
end

#formata_ledger(idx, hlx) ⇒ String

Returns texto formatado transacao ledger.

Examples:

{
  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)

    transacao ledger apius

  • idx (String)

    identificador transacao

Returns:

  • (String)

    texto formatado transacao ledger



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

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 (kraken/bigquery) & iguais/ok/nok?.

Examples:

{
 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 (kraken/bigquery) & iguais/ok/nok?



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

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.upcase,
    kr: sal,
    bq: t,
    ok: t == sal ? 'OK' : 'NOK'
  )
end

#formata_trades(idx, htx) ⇒ String

Returns texto formatado transacao trade.

Examples:

{
  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
  }
}

Parameters:

  • idx (String)

    identificador transacao

  • htx (Hash)

    transacao trade apius

Returns:

  • (String)

    texto formatado transacao trade



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

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 de transacoes ledger.

Returns:

  • (Array<String>)

    lista txid de transacoes ledger



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

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

#kytArray<String>

Returns lista txid de transacoes trades.

Returns:

  • (Array<String>)

    lista txid de transacoes trades



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

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

#ledgerHash

Returns transacoes ledger.

Returns:

  • (Hash)

    transacoes ledger



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

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

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



125
126
127
128
129
130
# File 'lib/cex/kraken.rb', line 125

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

  puts("\nledger data       hora     tipo       moeda -------quantidade -------------custo")
  ledger.sort { |a, b| b[1][:time] <=> a[1][:time] }.each { |k, v| puts(formata_ledger(k, v)) }
end

#mostra_resumoString

Returns texto saldos & transacoes & ajuste dias.

Returns:

  • (String)

    texto saldos & transacoes & ajuste dias



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

def mostra_resumo
  puts("\nKRAKEN\nmoeda          saldo kraken        saldo bigquery")
  exd[: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



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

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

  puts("\ntrade  data       hora     tipo       par      ---preco ---------volume ---custo")
  trades.sort { |a, b| b[1][:time] <=> a[1][:time] }.each { |k, v| puts(formata_trades(k, v)) }
end

#tradesHash

Returns transacoes trades.

Returns:

  • (Hash)

    transacoes trades



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

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