Class: Cns::Etherscan

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

Overview

classe para processar transacoes do etherscan

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Etherscan

Returns API etherscan - processar transacoes normais e tokens.

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 normais & tokens?



24
25
26
27
28
# File 'lib/cns/etherscan.rb', line 24

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

Instance Attribute Details

#apiApibc (readonly)

Returns API blockchains.

Returns:

  • (Apibc)

    API blockchains



13
14
15
# File 'lib/cns/etherscan.rb', line 13

def api
  @api
end

#bqdArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



15
16
17
# File 'lib/cns/etherscan.rb', line 15

def bqd
  @bqd
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



17
18
19
# File 'lib/cns/etherscan.rb', line 17

def ops
  @ops
end

Instance Method Details

#base_bc(abc) ⇒ Hash

Returns dados etherscan - address, saldo & transacoes.

Examples:

account_es

{
  status: '1',
  message: 'OK',
  result: [
    { account: '0x...', balance: '4000000000000000000' },
    { account: '0x...', balance: '87000000000000000000' }
  ]
}

Parameters:

  • abc (Hash)

    account etherscan

Returns:

  • (Hash)

    dados etherscan - address, saldo & transacoes



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

def base_bc(abc)
  acc = abc[:account].downcase
  {
    ax: acc,
    sl: (abc[:balance].to_d / 10**18),
    tx: filtrar_tx(acc, api.norml_es(acc)),
    ix: filtrar_tx(acc, api.inter_es(acc)),
    px: filtrar_px(acc, api.block_es(acc)),
    wx: filtrar_px(acc, api.withw_es(acc)),
    kx: filtrar_tx(acc, api.token_es(acc))
  }
end

#bcdArray<Hash>

Returns todos os dados etherscan - saldos & transacoes.

Returns:

  • (Array<Hash>)

    todos os dados etherscan - saldos & transacoes



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

def bcd
  @bcd ||= api.(lax).map { |obj| base_bc(obj) }
end

#bq_bc(wbq, hbc) ⇒ Hash

Returns dados juntos bigquery & etherscan.

Parameters:

  • wbq (Hash)

    wallet bigquery

  • hbc (Hash)

    dados etherscan - address, saldo & transacoes

Returns:

  • (Hash)

    dados juntos bigquery & etherscan



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cns/etherscan.rb', line 119

def bq_bc(wbq, hbc)
  {
    id: wbq[:id],
    ax: xbq = wbq[:ax],
    bs: wbq[:sl],
    bt: bqd[:nt].select { |ont| ont[:iax] == xbq },
    bi: bqd[:ni].select { |oni| oni[:iax] == xbq },
    bp: bqd[:np].select { |onp| onp[:iax] == xbq },
    bw: bqd[:nw].select { |onw| onw[:iax] == xbq },
    bk: bqd[:nk].select { |onk| onk[:iax] == xbq },
    es: hbc[:sl],
    et: hbc[:tx],
    ei: hbc[:ix],
    ep: hbc[:px],
    ew: hbc[:wx],
    ek: hbc[:kx]
  }
end

#dadosArray<Hash>

Returns todos os dados juntos bigquery & etherscan.

Returns:

  • (Array<Hash>)

    todos os dados juntos bigquery & etherscan



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

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

#filtrar_px(add, ary) ⇒ Array<Hash>

Returns lista blocks events filtrada.

Parameters:

  • ary (Array<Hash>)

    lista blocks events

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista blocks events filtrada



151
152
153
154
# File 'lib/cns/etherscan.rb', line 151

def filtrar_px(add, ary)
  # adiciona chave indice itx & adiciona identificador da carteira iax
  ary.map { |omp| omp.merge(itx: Integer(omp[:blockNumber]), iax: add) }
end

#filtrar_tx(add, ary) ⇒ Array<Hash>

Returns lista transacoes/token events filtrada.

Parameters:

  • ary (Array<Hash>)

    lista transacoes/token events

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista transacoes/token events filtrada



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

def filtrar_tx(add, ary)
  # elimina transferencia from: (lax) to: (add) - esta transferencia aparece em from: (add) to: (lax)
  # elimina chaves irrelevantes (DL) & adiciona chave indice itx & adiciona identificador da carteira iax
  ary.delete_if { |odl| add.casecmp?(odl[:to]) && lax.include?(odl[:from].downcase) }
     .map { |omp| omp.delete_if { |key, _| DL.include?(key) }.merge(itx: Integer(omp[:blockNumber]), iax: add) }
end

#formata_carteira(hjn) ⇒ String

Returns texto formatado duma carteira.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & etherscan

Returns:

  • (String)

    texto formatado duma carteira



202
203
204
205
206
207
208
# File 'lib/cns/etherscan.rb', line 202

def formata_carteira(hjn)
  format(
    '%<s1>-6.6s %<s2>-10.10s ',
    s1: hjn[:id],
    s2: formata_enderec1(hjn[:ax], 10)
  ) + formata_valores(hjn)
end

#formata_enderec1(add, max) ⇒ String

Returns endereco formatado.

Examples:

ether address inicio..fim

0x10f3a0cf0b534c..c033cf32e8a03586

Parameters:

  • max (Integer)

    chars a mostrar

  • add (String)

    endereco ETH

Returns:

  • (String)

    endereco formatado



242
243
244
245
246
247
248
249
# File 'lib/cns/etherscan.rb', line 242

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

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

#formata_enderec2(add, max) ⇒ String

Returns endereco formatado.

Examples:

ether address inicio..fim

me-app..4b437776403d

Parameters:

  • max (Integer)

    chars a mostrar

  • add (String)

    endereco ETH

Returns:

  • (String)

    endereco formatado



256
257
258
259
260
261
262
263
264
265
# File 'lib/cns/etherscan.rb', line 256

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

  max -= 2
  ini = Integer(max / 2)
  inf = max % 2
  hid = bqd[:wb].select { |obj| obj[:ax] == add }.first
  ndd = hid ? hid[:id] + '-' + add : add
  "#{ndd[0, ini - 3]}..#{ndd[-inf - ini - 3..]}"
end

#formata_transacao_block(htx) ⇒ String

Returns texto formatado transacao block etherscan.

Examples:

block_es

Parameters:

  • htx (Hash)

    transacao block etherscan

Returns:

  • (String)

    texto formatado transacao block etherscan



284
285
286
287
288
289
290
291
292
# File 'lib/cns/etherscan.rb', line 284

def formata_transacao_block(htx)
  format(
    '%<bn>9i %<fr>-41.41s %<dt>10.10s %<vl>17.6f',
    bn: htx[:blockNumber],
    fr: formata_enderec2(htx[:iax], 41),
    dt: Time.at(Integer(htx[:timeStamp])),
    vl: (htx[:blockReward].to_d / 10**18).round(10)
  )
end

#formata_transacao_norml(htx) ⇒ String

Returns texto formatado transacao normal etherscan.

Examples:

norml_es

{
  status: '1',
  message: 'OK',
  result: [
    {
      blockNumber: '4984535',
      timeStamp: '1517094794',
      hash: '0x...',
      nonce: '10',
      blockHash: '0x...',
      transactionIndex: '17',
      from: '0x...',
      to: '0x...',
      value: '52627271000000000000',
      gas: '21000',
      gasPrice: '19000000000',
      isError: '0',
      txreceipt_status: '1',
      input: '0x',
      contractAddress: '',
      gasUsed: '21000',
      cumulativeGasUsed: '566293',
      confirmations: '5848660'
    },
    {}
  ]
}

Parameters:

  • htx (Hash)

    transacao normal etherscan

Returns:

  • (String)

    texto formatado transacao normal etherscan



270
271
272
273
274
275
276
277
278
279
# File 'lib/cns/etherscan.rb', line 270

def formata_transacao_norml(htx)
  format(
    '%<bn>9i %<fr>-20.20s %<to>-20.20s %<dt>10.10s %<vl>17.6f',
    bn: htx[:blockNumber],
    fr: formata_enderec2(htx[:from], 20),
    to: formata_enderec2(htx[:to], 20),
    dt: Time.at(Integer(htx[:timeStamp])),
    vl: (htx[:value].to_d / 10**18).round(10)
  )
end

#formata_transacao_token(hkx) ⇒ String

Returns texto formatado transacao token etherscan.

Examples:

token_es

{
  status: '1',
  message: 'OK',
  result: [
    {
      blockNumber: '3967652',
      timeStamp: '1499081515',
      hash: '0x registo duplicado com todos os dados iguais',
      nonce: '3',
      blockHash: '0x00a49e999036dc13dc6c4244bb1d51d3146fe7f00bfb500a7624d82e299c7328',
      from: '0xd0a6e6c54dbc68db5db3a091b171a77407ff7ccf',
      contractAddress: '0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0',
      to: '0x...',
      value: '0',
      tokenName: 'EOS',
      tokenSymbol: 'EOS',
      tokenDecimal: '18',
      transactionIndex: '83',
      gas: '173399',
      gasPrice: '21000000000',
      gasUsed: '173398',
      input: 'deprecated',
      cumulativeGasUsed: '7484878',
      confirmations: '3442641'
    },
    {}
  ]
}

Parameters:

  • hkx (Hash)

    transacao token etherscan

Returns:

  • (String)

    texto formatado transacao token etherscan



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/cns/etherscan.rb', line 297

def formata_transacao_token(hkx)
  format(
    '%<bn>9i %<fr>-20.20s %<to>-20.20s %<dt>10.10s %<vl>11.3f %<sy>-5.5s',
    bn: hkx[:blockNumber],
    fr: formata_enderec2(hkx[:from], 20),
    to: formata_enderec2(hkx[:to], 20),
    dt: Time.at(Integer(hkx[:timeStamp])),
    vl: (hkx[:value].to_d / 10**18).round(10),
    sy: hkx[:tokenSymbol]
  )
end

#formata_transacao_withw(htx) ⇒ String

Returns texto formatado transacao withdrawals etherscan.

Examples:

block_es

Parameters:

  • htx (Hash)

    transacao withdrawals etherscan

Returns:

  • (String)

    texto formatado transacao withdrawals etherscan



312
313
314
315
316
317
318
319
320
# File 'lib/cns/etherscan.rb', line 312

def formata_transacao_withw(htx)
  format(
    '%<vi>9i %<bn>9i %<dt>10.10s %<vl>10.6f',
    vi: htx[:validatorIndex],
    bn: htx[:blockNumber],
    dt: Time.at(Integer(htx[:timestamp])),
    vl: (htx[:amount].to_d / 10**9).round(10)
  )
end

#formata_valores(hjn) ⇒ String

Returns texto formatado valores duma carteira.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & etherscan

Returns:

  • (String)

    texto formatado valores duma carteira



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cns/etherscan.rb', line 212

def formata_valores(hjn)
  format(
    '%<v1>11.4f %<n1>3i %<n2>2i %<n3>2i %<n4>2i %<w1>4i %<v2>11.4f %<n5>3i %<n6>2i %<n7>2i %<n8>2i %<w2>4i %<ok>-3s',
    v1: hjn[:es],
    n1: hjn[:et].count,
    n2: hjn[:ei].count,
    n3: hjn[:ep].count,
    n4: hjn[:ek].count,
    w1: hjn[:ew].count,
    v2: hjn[:bs],
    n5: hjn[:bt].count,
    n6: hjn[:bi].count,
    n7: hjn[:bp].count,
    n8: hjn[:bk].count,
    w2: hjn[:bw].count,
    ok: ok?(hjn) ? 'OK' : 'NOK'
  )
end

#idiArray<Integer>

Returns lista indices transacoes internas novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes internas novas



77
78
79
80
# File 'lib/cns/etherscan.rb', line 77

def idi
  @idi ||= bcd.map { |obc| obc[:ix].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:ni].map { |obq| obq[:itx] })
end

#idkArray<Integer>

Returns lista indices transacoes token novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes token novas



95
96
97
98
# File 'lib/cns/etherscan.rb', line 95

def idk
  @idk ||= bcd.map { |obc| obc[:kx].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:nk].map { |obq| obq[:itx] })
end

#idpArray<Integer>

Returns lista indices transacoes block novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes block novas



83
84
85
86
# File 'lib/cns/etherscan.rb', line 83

def idp
  @idp ||= bcd.map { |obc| obc[:px].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:np].map { |obq| obq[:itx] })
end

#idtArray<Integer>

Returns lista indices transacoes normais novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes normais novas



71
72
73
74
# File 'lib/cns/etherscan.rb', line 71

def idt
  @idt ||= bcd.map { |obc| obc[:tx].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:nt].map { |obq| obq[:itx] })
end

#idwArray<Integer>

Returns lista indices transacoes withdrawals novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes withdrawals novas



89
90
91
92
# File 'lib/cns/etherscan.rb', line 89

def idw
  @idw ||= bcd.map { |obc| obc[:wx].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:nw].map { |obq| obq[:itx] })
end

#laxArray<String>

Returns lista dos meus enderecos.

Returns:

  • (Array<String>)

    lista dos meus enderecos



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

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

#mostra_configuracao_ajuste_diasString

Returns texto configuracao ajuste dias das transacoes (normais & token).

Returns:

  • (String)

    texto configuracao ajuste dias das transacoes (normais & token)



363
364
365
366
367
# File 'lib/cns/etherscan.rb', line 363

def mostra_configuracao_ajuste_dias
  return unless (novtx.count + novkx.count).positive?

  puts("\nstring ajuste dias\n-h=#{sorax.map { |obj| "#{obj[:blockNumber]}:0" }.join(' ')}")
end

#mostra_resumoString

Returns texto carteiras & transacoes & ajuste dias.

Returns:

  • (String)

    texto carteiras & transacoes & ajuste dias



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cns/etherscan.rb', line 187

def mostra_resumo
  return unless dados.count.positive?

  puts("\nid     address      etherscan  tn ti tb tk   tw    bigquery  tn ti tb tk   tw")
  dados.each { |obj| puts(formata_carteira(obj)) }
  mostra_transacao_norml
  mostra_transacao_inter
  mostra_transacao_block
  mostra_transacao_token
  mostra_transacao_withw
  mostra_configuracao_ajuste_dias
end

#mostra_transacao_blockString

Returns texto transacoes block.

Returns:

  • (String)

    texto transacoes block



339
340
341
342
343
344
# File 'lib/cns/etherscan.rb', line 339

def mostra_transacao_block
  return unless ops[:v] && novpx.count.positive?

  puts("\ntx block  address                                   data                   valor")
  sorpx.each { |obj| puts(formata_transacao_block(obj)) }
end

#mostra_transacao_interString

Returns texto transacoes internas.

Returns:

  • (String)

    texto transacoes internas



331
332
333
334
335
336
# File 'lib/cns/etherscan.rb', line 331

def mostra_transacao_inter
  return unless ops[:v] && novix.count.positive?

  puts("\ntx intern from                 to                   data                   valor")
  sorix.each { |obj| puts(formata_transacao_norml(obj)) }
end

#mostra_transacao_normlString

Returns texto transacoes normais.

Returns:

  • (String)

    texto transacoes normais



323
324
325
326
327
328
# File 'lib/cns/etherscan.rb', line 323

def mostra_transacao_norml
  return unless ops[:v] && novtx.count.positive?

  puts("\ntx normal from                 to                   data                   valor")
  sortx.each { |obj| puts(formata_transacao_norml(obj)) }
end

#mostra_transacao_tokenString

Returns texto transacoes token.

Returns:

  • (String)

    texto transacoes token



347
348
349
350
351
352
# File 'lib/cns/etherscan.rb', line 347

def mostra_transacao_token
  return unless ops[:v] && novkx.count.positive?

  puts("\ntx token  from                 to                   data             valor")
  sorkx.each { |obj| puts(formata_transacao_token(obj)) }
end

#mostra_transacao_withwString

Returns texto transacoes withdrawals.

Returns:

  • (String)

    texto transacoes withdrawals



355
356
357
358
359
360
# File 'lib/cns/etherscan.rb', line 355

def mostra_transacao_withw
  return unless ops[:v] && novwx.count.positive?

  puts("\nvalidator     block data            valor")
  sorwx.each { |obj| puts(formata_transacao_withw(obj)) }
end

#novixArray<Hash>

Returns lista transacoes internas novas.

Returns:

  • (Array<Hash>)

    lista transacoes internas novas



36
37
38
# File 'lib/cns/etherscan.rb', line 36

def novix
  @novix ||= bcd.map { |obc| obc[:ix].select { |obj| idi.include?(obj[:itx]) } }.flatten
end

#novkxArray<Hash>

Returns lista transacoes token novas.

Returns:

  • (Array<Hash>)

    lista transacoes token novas



51
52
53
# File 'lib/cns/etherscan.rb', line 51

def novkx
  @novkx ||= bcd.map { |obc| obc[:kx].select { |obj| idk.include?(obj[:itx]) } }.flatten
end

#novpxArray<Hash>

Returns lista transacoes block novas.

Returns:

  • (Array<Hash>)

    lista transacoes block novas



41
42
43
# File 'lib/cns/etherscan.rb', line 41

def novpx
  @novpx ||= bcd.map { |obc| obc[:px].select { |obj| idp.include?(obj[:itx]) } }.flatten
end

#novtxArray<Hash>

Returns lista transacoes normais novas.

Returns:

  • (Array<Hash>)

    lista transacoes normais novas



31
32
33
# File 'lib/cns/etherscan.rb', line 31

def novtx
  @novtx ||= bcd.map { |obc| obc[:tx].select { |obj| idt.include?(obj[:itx]) } }.flatten
end

#novwxArray<Hash>

Returns lista transacoes withdrawals novas.

Returns:

  • (Array<Hash>)

    lista transacoes withdrawals novas



46
47
48
# File 'lib/cns/etherscan.rb', line 46

def novwx
  @novwx ||= bcd.map { |obc| obc[:wx].select { |obj| idw.include?(obj[:itx]) } }.flatten
end

#ok?(hjn) ⇒ Boolean

Returns carteira tem transacoes novas(sim=NOK, nao=OK)?.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & etherscan

Returns:

  • (Boolean)

    carteira tem transacoes novas(sim=NOK, nao=OK)?



233
234
235
# File 'lib/cns/etherscan.rb', line 233

def ok?(hjn)
  hjn[:es].round(4) == hjn[:bs].round(4) && hjn[:bt].count == hjn[:et].count && hjn[:bi].count == hjn[:ei].count && hjn[:bp].count == hjn[:ep].count && hjn[:bk].count == hjn[:ek].count && hjn[:bw].count == hjn[:ew].count
end

#soraxArray<Hash>

Returns lista ordenada transacoes (normais & token) novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes (normais & token) novas



182
183
184
# File 'lib/cns/etherscan.rb', line 182

def sorax
  (novtx + novkx).sort { |ant, prx| ant[:itx] <=> prx[:itx] }
end

#sorixArray<Hash>

Returns lista ordenada transacoes internas novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes internas novas



162
163
164
# File 'lib/cns/etherscan.rb', line 162

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

#sorkxArray<Hash>

Returns lista ordenada transacoes token novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes token novas



177
178
179
# File 'lib/cns/etherscan.rb', line 177

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

#sorpxArray<Hash>

Returns lista ordenada transacoes block novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes block novas



167
168
169
# File 'lib/cns/etherscan.rb', line 167

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

#sortxArray<Hash>

Returns lista ordenada transacoes normais novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes normais novas



157
158
159
# File 'lib/cns/etherscan.rb', line 157

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

#sorwxArray<Hash>

Returns lista ordenada transacoes withdrawals novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes withdrawals novas



172
173
174
# File 'lib/cns/etherscan.rb', line 172

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