Class: Cns::Bitcoinde

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

Overview

classe para processar transacoes trades/ledger do bitcoinde

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Bitcoinde

Returns API bitcoinde - 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/bitcoinde.rb', line 22

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

Instance Attribute Details

#apiApius (readonly)

Returns API bitcoinde.

Returns:

  • (Apius)

    API bitcoinde



10
11
12
# File 'lib/cns/bitcoinde.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/bitcoinde.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/bitcoinde.rb', line 14

def ops
  @ops
end

Instance Method Details

#exdHash

Returns dados exchange bitcoinde - saldos & trades & deposits & withdrawals.

Returns:

  • (Hash)

    dados exchange bitcoinde - saldos & trades & deposits & withdrawals



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

def exd
  @exd ||= {
    sl: api.,
    tt: api.trades_de,
    tl: api.deposits_de + api.withdrawals_de
  }
end

#formata_ledger(hlx) ⇒ String

Returns texto formatado ledger.

Parameters:

  • hlx (Hash)

    ledger (deposits + withdrawals) bitcoinde

Returns:

  • (String)

    texto formatado ledger



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

def formata_ledger(hlx)
  format(
    '%<ky>6i %<dt>19.19s %<ty>-10.10s %<mo>-3.3s %<pr>19.8f %<vl>18.8f',
    ky: hlx[:txid],
    dt: hlx[:time],
    ty: hlx[:tp],
    mo: hlx[:moe].upcase,
    pr: hlx[:qt].to_d,
    vl: hlx[:fee].to_d
  )
end

#formata_saldos(moe, hsx) ⇒ String

Returns texto formatado saldos.

Examples:

account_de

{
  data: {
    balances: {
      btc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
      bch: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
      btg: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
      eth: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
      bsv: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
      ltc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' }
    },
    encrypted_information: { uid: '0y...', bic_short: '0y...', bic_full: '0y...' }
  },
  errors: [],
  credits: 23
}

Parameters:

  • moe (String)

    codigo bitcoinde da moeda

  • hsx (Hash)

    saldo bitcoinde da moeda

Returns:

  • (String)

    texto formatado saldos



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

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

#formata_trades(htx) ⇒ String

Returns texto formatado trade.

Examples:

trades_de

{
  trades: [{
    trade_id: 'XUWWD3',
    trading_pair: 'btceur',
    is_external_wallet_trade: false,
    type: 'sell',
    amount_currency_to_trade: '0.1',
    price: 430,
    volume_currency_to_pay: 43,
    volume_currency_to_pay_after_fee: 42.79,
    amount_currency_to_trade_after_fee: 0.099,
    fee_currency_to_pay: 0.22,
    fee_currency_to_trade: '0.00100000',
    created_at: '2014-03-22T08:14:48+01:00',
    successfully_finished_at: '2014-03-25T14:03:22+01:00',
    state: 1,
    is_trade_marked_as_paid: true,
    trade_marked_as_paid_at: '2014-03-22T08:20:01+01:00',
    payment_method: 1,
    my_rating_for_trading_partner: 'positive',
    trading_partner_information: {
      username: 'emax2000',
      is_kyc_full: false,
      trust_level: 'bronze',
      amount_trades: 4,
      rating: 100,
      bank_name: 'CASSA DI RISPARMIO DI CIVITAVECCHIA SPA',
      bic: 'CRFIIT2CXXX',
      seat_of_bank: 'IT'
    }
  }, {}],
  page: { current: 1, last: 2 },
  errors: [],
  credits: 22
}

Returns:

  • (String)

    texto formatado trade



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

def formata_trades(htx)
  format(
    '%<ky>-6.6s %<dt>19.19s %<dp>10.10s %<ty>-5.5s %<mo>-8.8s %<vl>18.8f %<co>8.2f',
    ky: htx[:trade_id],
    dt: Time.parse(htx[:successfully_finished_at]),
    dp: Time.parse(htx[:trade_marked_as_paid_at]),
    ty: htx[:type],
    mo: htx[:trading_pair].upcase,
    vl: htx[:amount_currency_to_trade].to_d,
    co: htx[:volume_currency_to_pay].to_d
  )
end

#kylArray<Integer>

Returns lista txid dos ledger novos.

Returns:

  • (Array<Integer>)

    lista txid dos ledger novos



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

def kyl
  @kyl ||= exd[:tl].map { |oex| oex[:txid] }.flatten - (ops[:t] ? [] : bqd[:nl].map { |obq| obq[:txid] })
end

#kytArray<String>

Returns lista txid dos trades novos.

Returns:

  • (Array<String>)

    lista txid dos trades novos



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

def kyt
  @kyt ||= exd[:tt].map { |oex| oex[:trade_id] }.flatten - (ops[:t] ? [] : bqd[:nt].map { |obq| obq[:txid] })
end

#ledgerArray<Hash>

Returns lista ledger (deposits + withdrawals) bitcoinde novos.

Returns:

  • (Array<Hash>)

    lista ledger (deposits + withdrawals) bitcoinde novos



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

def ledger
  @ledger ||= exd[:tl].select { |obj| kyl.include?(obj[:txid]) }
end

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



140
141
142
143
144
145
# File 'lib/cns/bitcoinde.rb', line 140

def mostra_ledger
  return unless ops[:v] && !ledger.empty?

  puts("\nledger data       hora     tipo       moe          quantidade              custo")
  ledger.sort { |ant, prx| prx[:time] <=> ant[:time] }.each { |obj| puts(formata_ledger(obj)) }
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/bitcoinde.rb', line 39

def mostra_resumo
  puts("\nBITCOINDE\ntipo              bitcoinde              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 numero de transacoes.

Returns:

  • (String)

    texto numero de transacoes



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

def mostra_totais
  vtt = exd[:tt].count
  vnt = bqd[:nt].count
  vtl = exd[:tl].count
  vnl = bqd[:nl].count

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

#mostra_tradesString

Returns texto transacoes trades.

Returns:

  • (String)

    texto transacoes trades



130
131
132
133
134
135
136
137
# File 'lib/cns/bitcoinde.rb', line 130

def mostra_trades
  return unless ops[:v] && !trades.empty?

  puts("\ntrades data       hora     dt criacao tipo  par                     qtd      eur")
  trades
    .sort { |ant, prx| Time.parse(prx[:successfully_finished_at]) <=> Time.parse(ant[:successfully_finished_at]) }
    .each { |obj| puts(formata_trades(obj)) }
end

#tradesArray<Hash>

Returns lista trades bitcoinde novos.

Returns:

  • (Array<Hash>)

    lista trades bitcoinde novos



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

def trades
  @trades ||= exd[:tt].select { |obj| kyt.include?(obj[:trade_id]) }
end