Class: Etht::Bigquery

Inherits:
Object
  • Object
show all
Defined in:
lib/etht/bigquery.rb

Overview

classe para processar etherscan & bigquery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pop) ⇒ Bigquery

Returns API bigquery & API etherscan.

Parameters:

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :h (Hash) — default: {}

    configuracao ajuste reposicionamento temporal

  • :v (Boolean) — default: false

    mostra transacoes normais & token?

  • :t (Boolean) — default: false

    mostra transacoes todas ou somente novas?



26
27
28
29
30
31
# File 'lib/etht/bigquery.rb', line 26

def initialize(pop)
  # usa env GOOGLE_APPLICATION_CREDENTIALS para obter credentials
  # @see https://cloud.google.com/bigquery/docs/authentication/getting-started
  @api = Google::Cloud::Bigquery.new
  @ops = pop
end

Instance Attribute Details

#apiGoogle::Cloud::Bigquery (readonly)

Returns API bigquery.

Returns:

  • (Google::Cloud::Bigquery)

    API bigquery



13
14
15
# File 'lib/etht/bigquery.rb', line 13

def api
  @api
end

#jobGoogle::Cloud::Bigquery::QueryJob (readonly)

Returns job bigquery.

Returns:

  • (Google::Cloud::Bigquery::QueryJob)

    job bigquery



15
16
17
# File 'lib/etht/bigquery.rb', line 15

def job
  @job
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



17
18
19
# File 'lib/etht/bigquery.rb', line 17

def ops
  @ops
end

#sqrGoogle::Cloud::Bigquery::Data (readonly)

Returns resultado do SQL.

Returns:

  • (Google::Cloud::Bigquery::Data)

    resultado do SQL



19
20
21
# File 'lib/etht/bigquery.rb', line 19

def sqr
  @sqr
end

Instance Method Details

#carteirasCarteiras

Returns API etherscan - processar carteiras & transacoes normais e tokens.

Returns:

  • (Carteiras)

    API etherscan - processar carteiras & transacoes normais e tokens



47
48
49
# File 'lib/etht/bigquery.rb', line 47

def carteiras
  transacoes
end

#dml(cmd) ⇒ Integer

cria Data Manipulation Language (DML) job bigquery

Parameters:

  • cmd (String)

    comando SQL a executar

Returns:

  • (Integer)

    numero linhas afetadas



158
159
160
# File 'lib/etht/bigquery.rb', line 158

def dml(cmd)
  job?(cmd) ? 0 : job.num_dml_affected_rows
end

#ethk_insString

Returns comando insert SQL formatado ethk (trx token).

Returns:

  • (String)

    comando insert SQL formatado ethk (trx token)



65
66
67
68
69
# File 'lib/etht/bigquery.rb', line 65

def ethk_ins
  "insert #{BD}.ethk(blocknumber,timestamp,txhash,nonce,blockhash,axfrom,contractaddress,axto,value,tokenname," \
  'tokensymbol,tokendecimal,transactionindex,gas,gasprice,gasused,cumulativegasused,input,confirmations,dias' \
  ") VALUES#{transacoes.token.map { |e| ethk_val1(e) }.join(',')}"
end

#ethk_val1(hes) ⇒ String

Returns valores formatados ethk (trx token parte1).

Returns:

  • (String)

    valores formatados ethk (trx token parte1)



103
104
105
106
107
108
109
110
111
# File 'lib/etht/bigquery.rb', line 103

def ethk_val1(hes)
  "(#{Integer(hes['blockNumber'])}," \
  "#{Integer(hes['timeStamp'])}," \
  "'#{hes['hash']}'," \
  "#{Integer(hes['nonce'])}," \
  "'#{hes['blockHash']}'," \
  "'#{hes['from']}'," \
  "#{ethk_val2(hes)}"
end

#ethk_val2(hes) ⇒ String

Returns valores formatados ethk (trx token parte2).

Returns:

  • (String)

    valores formatados ethk (trx token parte2)



114
115
116
117
118
119
120
121
122
123
# File 'lib/etht/bigquery.rb', line 114

def ethk_val2(hes)
  "#{hes['contractAddress'].length.zero? ? 'null' : "'#{hes['contractAddress']}'"}," \
  "'#{hes['to']}'," \
  "cast('#{hes['value']}' as numeric)," \
  "'#{hes['tokenName']}'," \
  "'#{hes['tokenSymbol']}'," \
  "#{Integer(hes['tokenDecimal'])}," \
  "#{Integer(hes['transactionIndex'])}," \
  "#{ethk_val3(hes)}"
end

#ethk_val3(hes) ⇒ String

Returns valores formatados ethk (trx token parte3).

Returns:

  • (String)

    valores formatados ethk (trx token parte3)



126
127
128
129
130
131
132
# File 'lib/etht/bigquery.rb', line 126

def ethk_val3(hes)
  "cast('#{hes['gas']}' as numeric)," \
  "cast('#{hes['gasPrice']}' as numeric)," \
  "cast('#{hes['gasUsed']}' as numeric),0," \
  "#{hes['input'].length.zero? ? 'null' : "'#{hes['input']}'"},0," \
  "#{Integer(ops[:h][hes['blockNumber']] || 0)})"
end

#etht_insString

Returns comando insert SQL formatado etht (trx normais).

Returns:

  • (String)

    comando insert SQL formatado etht (trx normais)



58
59
60
61
62
# File 'lib/etht/bigquery.rb', line 58

def etht_ins
  "insert #{BD}.etht(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,value,gas," \
  'gasprice,iserror,txreceipt_status,input,contractaddress,cumulativegasused,gasused,confirmations,dias' \
  ") VALUES#{transacoes.norml.map { |e| etht_val1(e) }.join(',')}"
end

#etht_val1(hes) ⇒ String

Returns valores formatados etht (trx normais parte1).

Returns:

  • (String)

    valores formatados etht (trx normais parte1)



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/etht/bigquery.rb', line 72

def etht_val1(hes)
  "(#{Integer(hes['blockNumber'])}," \
  "#{Integer(hes['timeStamp'])}," \
  "'#{hes['hash']}'," \
  "#{Integer(hes['nonce'])}," \
  "'#{hes['blockHash']}'," \
  "#{Integer(hes['transactionIndex'])}," \
  "'#{hes['from']}'," \
  "'#{hes['to']}'," \
  "#{etht_val2(hes)}"
end

#etht_val2(hes) ⇒ String

Returns valores formatados etht (trx normais parte2).

Returns:

  • (String)

    valores formatados etht (trx normais parte2)



85
86
87
88
89
90
91
92
93
# File 'lib/etht/bigquery.rb', line 85

def etht_val2(hes)
  "cast('#{hes['value']}' as numeric)," \
  "cast('#{hes['gas']}' as numeric)," \
  "cast('#{hes['gasPrice']}' as numeric)," \
  "#{Integer(hes['isError'])}," \
  "#{hes['txreceipt_status'].length.zero? ? 'null' : hes['txreceipt_status']}," \
  "#{hes['input'].length.zero? ? 'null' : "'#{hes['input']}'"}," \
  "#{etht_val3(hes)}"
end

#etht_val3(hes) ⇒ String

Returns valores formatados etht (trx normais parte3).

Returns:

  • (String)

    valores formatados etht (trx normais parte3)



96
97
98
99
100
# File 'lib/etht/bigquery.rb', line 96

def etht_val3(hes)
  "#{hes['contractAddress'].length.zero? ? 'null' : "'#{hes['contractAddress']}'"}," \
  "0,cast('#{hes['gasUsed']}' as numeric),0," \
  "#{Integer(ops[:h][hes['blockNumber']] || 0)})"
end

#job?(cmd) ⇒ Boolean

cria job bigquery & verifica execucao

Parameters:

  • cmd (String)

    comando SQL a executar

Returns:

  • (Boolean)

    job ok?



138
139
140
141
142
143
# File 'lib/etht/bigquery.rb', line 138

def job?(cmd)
  @job = api.query_job(cmd)
  @job.wait_until_done!
  puts(@job.error['message']) if @job.failed?
  @job.failed?
end

#processaObject

insere transacoes novas nas tabelas etht (trx normais), ethk (trx token)



52
53
54
55
# File 'lib/etht/bigquery.rb', line 52

def processa
  puts(format("%<n>2i LINHAS INSERIDAS #{BD}.etht", n: transacoes.norml.count.positive? ? dml(etht_ins) : 0))
  puts(format("%<n>2i LINHAS INSERIDAS #{BD}.ethk", n: transacoes.token.count.positive? ? dml(ethk_ins) : 0))
end

#sql(cmd, red = []) ⇒ Google::Cloud::Bigquery::Data

cria Structured Query Language (SQL) job bigquery

Parameters:

  • cmd (String)

    comando SQL a executar

  • red (String) (defaults to: [])

    resultado quando SQL tem erro

Returns:

  • (Google::Cloud::Bigquery::Data)

    resultado do SQL



150
151
152
# File 'lib/etht/bigquery.rb', line 150

def sql(cmd, red = [])
  @sqr = job?(cmd) ? red : job.data
end

#transacoesCarteiras

Returns API etherscan - processar transacoes normais e tokens.

Returns:

  • (Carteiras)

    API etherscan - processar transacoes normais e tokens



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/etht/bigquery.rb', line 34

def transacoes
  @transacoes ||= Carteiras.new(
    {
      wb: sql("select * from #{BD}.walletEth order by 2")
               .map { |e| { id: e[:id], ax: e[:address], sl: e[:saldo].to_d.round(10) } },
      nt: sql("select blocknumber,iax from #{BD}.ethtx"),
      nk: sql("select blocknumber,iax from #{BD}.ethkx")
    },
    ops
  )
end