Class: Cns::Beaconchain

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

Overview

classe para processar historicos da beaconchain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Beaconchain

Returns API beaconchain - processar historico saldos.

Parameters:

  • dad (Hash)

    todos os dados bigquery

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :v (Boolean) — default: false

    mostra saldos?

  • :t (Boolean) — default: false

    mostra todos saldos ou somente novos?



21
22
23
24
25
# File 'lib/cns/beaconchain.rb', line 21

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

Instance Attribute Details

#apiApibc (readonly)

Returns API blockchains.

Returns:

  • (Apibc)

    API blockchains



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

def ops
  @ops
end

Instance Method Details

#base_bc(abc) ⇒ Hash

Returns dados beaconchain - index, saldo & historico.

Examples:

{
  activationeligibilityepoch: 0,
  activationepoch: 0,
  balance: 32_489_497_108,
  effectivebalance: 32_000_000_000,
  exitepoch: 9_223_372_036_854_775_807,
  lastattestationslot: 265_446,
  name: '',
  pubkey: '0x93bf23a587f11f9eca329a12ef51296b8a9848af8c0fe61201524b14cb85b0c6fbd3e427501cdfa3b28719bd1ed96fff',
  slashed: false,
  status: 'active_online',
  validatorindex: 11_766,
  withdrawableepoch: 9_223_372_036_854_775_807,
  withdrawalcredentials: '0x004f11be01cb72187715c55d6348c67c5a3880687cd42692306fdbc91ac2da9b'
}

Parameters:

  • abc (Hash)

    account beaconchain

Returns:

  • (Hash)

    dados beaconchain - index, saldo & historico



78
79
80
81
82
83
84
85
# File 'lib/cns/beaconchain.rb', line 78

def base_bc(abc)
  acc = abc[:validatorindex]
  {
    ax: acc,
    sl: (abc[:balance].to_d / 10**9).round(10),
    bx: api.data_bc("/api/v1/validator/#{acc}/balancehistory")
  }
end

#bcdArray<Hash>

Returns todos os dados beaconchain - saldos & historico.

Returns:

  • (Array<Hash>)

    todos os dados beaconchain - saldos & historico



38
39
40
# File 'lib/cns/beaconchain.rb', line 38

def bcd
  @bcd ||= api.data_bc("/api/v1/validator/#{lax.join(',')}").map { |obj| base_bc(obj) }
end

#bq_bc(wbq, abc) ⇒ Hash

Returns dados juntos bigquery & beaconchain.

Parameters:

  • wbq (Hash)

    wallet bigquery

  • abc (Hash)

    account beaconchain

Returns:

  • (Hash)

    dados juntos bigquery & beaconchain



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

def bq_bc(wbq, abc)
  xbq = wbq[:id]
  {
    id: xbq,
    ax: wbq[:ax],
    bs: wbq[:sl],
    bb: bqd[:nb].select { |onb| onb[:iax] == xbq },
    es: abc[:sl],
    eb: abc[:bx]
  }
end

#dadosArray<Hash>

Returns todos os dados juntos bigquery & beaconchain.

Returns:

  • (Array<Hash>)

    todos os dados juntos bigquery & beaconchain



43
44
45
# File 'lib/cns/beaconchain.rb', line 43

def dados
  @dados ||= bqd[:wb].map { |obq| bq_bc(obq, bcd.select { |obc| obq[:id] == obc[:ax] }.first) }
end

#formata_endereco(add, max) ⇒ String

Returns pubkey formatada.

Examples:

pubkey inicio..fim

0x10f3a0cf0b534c..c033cf32e8a03586

Parameters:

  • add (String)

    chave publica validador

  • max (Integer)

    chars a mostrar

Returns:

  • (String)

    pubkey formatada



141
142
143
144
145
146
147
148
# File 'lib/cns/beaconchain.rb', line 141

def formata_endereco(add, max)
  return 'erro' if max < 7

  max -= 2
  ini = Integer(max / 2)
  inf = max % 2
  "#{add[0, ini - 3]}..#{add[-inf - ini - 3..]}"
end

#formata_saldos(hbh) ⇒ String

Returns texto formatado historico beaconchain.

Examples:

{
  balance: 32_489_497_108,
  effectivebalance: 32_000_000_000,
  epoch: 8296,
  validatorindex: 11_766,
  week: 5
}

Parameters:

  • hbh (Hash)

    historico beaconchain

Returns:

  • (String)

    texto formatado historico beaconchain



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cns/beaconchain.rb', line 160

def formata_saldos(hbh)
  idx = hbh[:validatorindex]
  epc = hbh[:epoch]
  format(
    '%<vi>5i %<vl>17.6f %<ep>6i %<id>9i',
    vi: idx,
    vl: (hbh[:balance].to_d / (10**9)).round(10),
    ep: epc,
    id: itx(epc, idx)
  )
end

#formata_validador(hjn) ⇒ String

Returns texto formatado dum validador.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & beaconchain

Returns:

  • (String)

    texto formatado dum validador



113
114
115
# File 'lib/cns/beaconchain.rb', line 113

def formata_validador(hjn)
  format('%<s1>-5.5s %<s2>-34.34s ', s1: hjn[:id], s2: formata_endereco(hjn[:ax], 34)) + formata_valores(hjn)
end

#formata_valores(hjn) ⇒ String

Returns texto formatado valores dum validador.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & beaconchain

Returns:

  • (String)

    texto formatado valores dum validador



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

def formata_valores(hjn)
  format(
    '%<v1>11.6f %<n1>3i %<v2>12.6f %<n2>6i %<ok>-3s',
    v1: hjn[:es],
    n1: hjn[:eb].count,
    v2: hjn[:bs],
    n2: hjn[:bb].count,
    ok: ok?(hjn) ? 'OK' : 'NOK'
  )
end

#idbArray<Integer>

Returns lista historicos novos.

Returns:

  • (Array<Integer>)

    lista historicos novos



48
49
50
51
# File 'lib/cns/beaconchain.rb', line 48

def idb
  @idb ||= bcd.map { |obc| obc[:bx].map { |obj| itx(obj[:epoch], obj[:validatorindex]) } }.flatten -
           (ops[:t] ? [] : bqd[:nb].map { |obq| obq[:itx] })
end

#itx(intum, intdois) ⇒ Integer

Returns szudzik pairing two integers.

Parameters:

  • intum (Integer)
  • intdois (Integer)

Returns:

  • (Integer)

    szudzik pairing two integers



56
57
58
# File 'lib/cns/beaconchain.rb', line 56

def itx(intum, intdois)
  intum >= intdois ? intum * intum + intum + intdois : intum + intdois * intdois
end

#laxArray<Integer>

Returns lista dos meus validators.

Returns:

  • (Array<Integer>)

    lista dos meus validators



33
34
35
# File 'lib/cns/beaconchain.rb', line 33

def lax
  @lax ||= bqd[:wb].map { |obj| obj[:id] }
end

#mostra_resumoString

Returns texto validadores & saldos historicos.

Returns:

  • (String)

    texto validadores & saldos historicos



103
104
105
106
107
108
109
# File 'lib/cns/beaconchain.rb', line 103

def mostra_resumo
  return unless dados.count.positive?

  puts("\nindex address                            beaconchain blh     bigquery    blh")
  dados.each { |obj| puts(formata_validador(obj)) }
  mostra_saldos
end

#mostra_saldosString

Returns texto historico saldos.

Returns:

  • (String)

    texto historico saldos



173
174
175
176
177
178
# File 'lib/cns/beaconchain.rb', line 173

def mostra_saldos
  return unless ops[:v] && nov.count.positive?

  puts("\nindex             saldo  epoch       itx")
  sorbx.each { |obj| puts(formata_saldos(obj)) }
end

#novArray<Hash>

Returns lista balancos novos.

Returns:

  • (Array<Hash>)

    lista balancos novos



28
29
30
# File 'lib/cns/beaconchain.rb', line 28

def nov
  @nov ||= bcd.map { |obc| obc[:bx].select { |obj| idb.include?(itx(obj[:epoch], obj[:validatorindex])) } }.flatten
end

#ok?(hjn) ⇒ Boolean

Returns validador tem historicos novos(sim=NOK, nao=OK)?.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & beaconchain

Returns:

  • (Boolean)

    validador tem historicos novos(sim=NOK, nao=OK)?



132
133
134
# File 'lib/cns/beaconchain.rb', line 132

def ok?(hjn)
  hjn[:bs] == hjn[:es]
end

#sorbxArray<Hash>

Returns lista ordenada historico saldos.

Returns:

  • (Array<Hash>)

    lista ordenada historico saldos



181
182
183
# File 'lib/cns/beaconchain.rb', line 181

def sorbx
  nov.sort { |ant, prx| ant[:itx] <=> prx[:itx] }
end