class Etht::Carteiras
classe para processar carteiras & transacoes normais e tokens
(see Carteiras
)
Attributes
@return [Etherscan] API etherscan
@return [Array<Hash>] todos os dados bigquery
@return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
Public Class Methods
@param [Hash] dad todos os dados bigquery @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho @option pop [Hash] :h ({}) configuracao dias ajuste reposicionamento temporal @option pop [Boolean] :v (false) mostra dados transacoes normais & tokens? @return [Carteiras] API etherscan - processar transacoes normais e tokens
# File lib/etht/carteiras.rb, line 21 def initialize(dad, pop) @api = Etherscan.new @dbq = dad @ops = pop end
Public Instance Methods
@param [Hash] hes dados etherscan @return [Hash] dados etherscan - address, saldo & transacoes
# File lib/etht/carteiras.rb, line 76 def base_etherscan(hes) { ax: hes['account'], sl: (hes['balance'].to_d / 10**18).round(10), tx: etherscan_tx(hes['account']), kx: etherscan_kx(hes['account']) } end
@param [Hash] hbq dados bigquery @param [Hash] hes dados etherscan @return [Hash] dados juntos bigquery & etherscan
# File lib/etht/carteiras.rb, line 88 def bigquery_etherscan(hbq, hes) { id: hbq[:id], ax: hbq[:ax], bs: hbq[:sl], bt: dbq[:nt].select { |t| t[:iax] == hbq[:ax] }, bk: dbq[:nk].select { |t| t[:iax] == hbq[:ax] }, es: hes[:sl], et: hes[:tx], ek: hes[:kx] } end
@return [Array<Integer>] lista blocknumbers de transacoes token
# File lib/etht/carteiras.rb, line 44 def bnk @bnk ||= (des.map { |e| e[:kx].map { |n| Integer(n['blockNumber']) } }.flatten - (ops[:t] ? [] : dbq[:nk].map { |t| t[:blocknumber] })).uniq end
@return [Array<Integer>] lista blocknumbers de transacoes normais
# File lib/etht/carteiras.rb, line 38 def bnt @bnt ||= (des.map { |e| e[:tx].map { |n| Integer(n['blockNumber']) } }.flatten - (ops[:t] ? [] : dbq[:nt].map { |t| t[:blocknumber] })).uniq end
@return [Array<Hash>] todos os dados etherscan - saldos & transacoes
# File lib/etht/carteiras.rb, line 28 def des @des ||= api.multi_address_balance(dbq[:wb].map { |c| c[:ax] }).map { |e| base_etherscan(e) } end
@return [Array<Hash>] todos os dados juntos bigquery & etherscan
# File lib/etht/carteiras.rb, line 33 def djn @djn ||= dbq[:wb].map { |b| bigquery_etherscan(b, des.select { |s| b[:ax] == s[:ax] }.first) } end
@param add (see formata_endereco
) @return [Array<Hash>] lista transacoes token ligadas a uma carteira - sem elementos irrelevantes
# File lib/etht/carteiras.rb, line 113 def etherscan_kx(add) api.token_tx(add).map do |e| e.delete('cumulativeGasUsed') e.delete('confirmations') e end end
@param add (see formata_endereco
) @return [Array<Hash>] lista transacoes normais ligadas a uma carteira - sem elementos irrelevantes
# File lib/etht/carteiras.rb, line 103 def etherscan_tx(add) api.normal_tx(add).map do |e| e.delete('cumulativeGasUsed') e.delete('confirmations') e end end
@parm [Hash] hjn dados juntos bigquery & etherscan @return [String] texto formatado duma carteira
# File lib/etht/formatar.rb, line 20 def formata_carteira(hjn) format( '%<s1>-6.6s %<s2>-34.34s ', s1: hjn[:id], s2: formata_endereco(hjn[:ax], 34) ) + formata_valores(hjn) end
@param [String] add endereco carteira ether @param [Integer] max chars a mostrar @return [String] endereco ether formatado @example ether address inicio..fim
0x10f3a0cf0b534c..c033cf32e8a03586
# File lib/etht/formatar.rb, line 12 def formata_endereco(add, max) i = Integer((max - 2) / 2) e = (max <= 20 ? dbq[:wb].select { |s| s[:ax] == add }.first : nil) || { id: add } max < 7 ? 'erro' : "#{e[:id][0, i - 3]}..#{add[-i - 3..]}" end
@parm [Hash] htx transacao normal @return [String] texto formatado transacao normal
# File lib/etht/formatar.rb, line 51 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_endereco(htx['from'], 20), to: formata_endereco(htx['to'], 20), dt: Time.at(Integer(htx['timeStamp'])), vl: (htx['value'].to_d / 10**18).round(10) ) end
@parm [Hash] hkx transacao token @return [String] texto formatado transacao token
# File lib/etht/formatar.rb, line 64 def formata_transacao_token(hkx) format( '%<bn>9i %<fr>-20.20s %<to>-20.20s %<dt>10.10s %<sy>-5.5s %<vl>11.3f', bn: hkx['blockNumber'], fr: formata_endereco(hkx['from'], 20), to: formata_endereco(hkx['to'], 20), dt: Time.at(Integer(hkx['timeStamp'])), vl: (hkx['value'].to_d / 10**18).round(10), sy: hkx['tokenSymbol'] ) end
@parm (see formata_carteira
) @return [String] texto formatado valores duma carteira
# File lib/etht/formatar.rb, line 36 def formata_valores(hjn) format( '%<v1>10.6f %<n1>2i %<n3>2i %<v2>11.6f %<n2>2i %<n4>2i %<ok>-3s', v1: hjn[:bs], n1: hjn[:bt].count, n3: hjn[:bk].count, v2: hjn[:es], n2: hjn[:et].count, n4: hjn[:ek].count, ok: ok?(hjn) ? 'OK' : 'NOK' ) end
@return [String] texto configuracao ajuste dias das transacoes (normais & tokan)
# File lib/etht/formatar.rb, line 104 def mostra_configuracao_ajuste_dias return unless (norml.count + token.count).positive? puts("\nstring ajuste dias\n-h=#{todas_sort.map { |e| "#{e['blockNumber']}:0" }.join(' ')}") end
@return [String] texto carteiras & transacoes & ajuste dias
# File lib/etht/formatar.rb, line 77 def mostra_resumo return unless djn.count.positive? puts("\nid address ----bigquery---- ----etherscan----") djn.each { |e| puts(formata_carteira(e)) } mostra_transacao_norml mostra_transacao_token mostra_configuracao_ajuste_dias end
@return [String] texto transacoes normais
# File lib/etht/formatar.rb, line 88 def mostra_transacao_norml return unless ops[:v] && norml.count.positive? puts("\ntx normal address from address to ---data--- ------valor------") norml_sort.each { |e| puts(formata_transacao_norml(e)) } end
@return [String] texto transacoes token
# File lib/etht/formatar.rb, line 96 def mostra_transacao_token return unless ops[:v] && token.count.positive? puts("\ntx token address from address to ---data--- token ---valor---") token_sort.each { |e| puts(formata_transacao_token(e)) } end
@return [Array<Hash>] lista transacoes normais
# File lib/etht/carteiras.rb, line 50 def norml @norml ||= des.map { |e| e[:tx].select { |s| bnt.include?(Integer(s['blockNumber'])) } }.flatten.uniq end
@return [Array<Hash>] lista ordenada transacoes normais
# File lib/etht/carteiras.rb, line 60 def norml_sort norml.sort { |a, b| Integer(a['blockNumber']) <=> Integer(b['blockNumber']) } end
@parm (see formata_carteira
) @return [Boolean] carteira tem transacoes novas(sim=NOK, nao=OK)?
# File lib/etht/formatar.rb, line 30 def ok?(hjn) hjn[:bs] == hjn[:es] && hjn[:bt].count == hjn[:et].count && hjn[:bk].count == hjn[:ek].count end
@return [Array<Hash>] lista ordenada todas as transacoes (normais & tokan)
# File lib/etht/carteiras.rb, line 70 def todas_sort (norml + token).sort { |a, b| Integer(a['blockNumber']) <=> Integer(b['blockNumber']) } end
@return [Array<Hash>] lista transacoes tokan
# File lib/etht/carteiras.rb, line 55 def token @token ||= des.map { |e| e[:kx].select { |s| bnk.include?(Integer(s['blockNumber'])) } }.flatten.uniq end
@return [Array<Hash>] lista ordenada transacoes tokan
# File lib/etht/carteiras.rb, line 65 def token_sort token.sort { |a, b| Integer(a['blockNumber']) <=> Integer(b['blockNumber']) } end