Class: Etht::Bigquery
- Inherits:
-
Object
- Object
- Etht::Bigquery
- Defined in:
- lib/etht/bigquery.rb
Overview
classe para processar etherscan & bigquery
Instance Attribute Summary collapse
-
#api ⇒ Google::Cloud::Bigquery
readonly
API bigquery.
-
#job ⇒ Google::Cloud::Bigquery::QueryJob
readonly
Job bigquery.
-
#ops ⇒ Thor::CoreExt::HashWithIndifferentAccess
readonly
Opcoes trabalho.
-
#sqr ⇒ Google::Cloud::Bigquery::Data
readonly
Resultado do SQL.
Instance Method Summary collapse
-
#carteiras ⇒ Carteiras
API etherscan - processar carteiras & transacoes normais e tokens.
-
#dml(cmd) ⇒ Integer
cria Data Manipulation Language (DML) job bigquery.
-
#ethk_ins ⇒ String
Comando insert SQL formatado ethk (trx token).
-
#ethk_val1(hes) ⇒ String
Valores formatados ethk (trx token parte1).
-
#ethk_val2(hes) ⇒ String
Valores formatados ethk (trx token parte2).
-
#ethk_val3(hes) ⇒ String
Valores formatados ethk (trx token parte3).
-
#etht_ins ⇒ String
Comando insert SQL formatado etht (trx normais).
-
#etht_val1(hes) ⇒ String
Valores formatados etht (trx normais parte1).
-
#etht_val2(hes) ⇒ String
Valores formatados etht (trx normais parte2).
-
#etht_val3(hes) ⇒ String
Valores formatados etht (trx normais parte3).
-
#initialize(pop) ⇒ Bigquery
constructor
API bigquery & API etherscan.
-
#job?(cmd) ⇒ Boolean
cria job bigquery & verifica execucao.
-
#processa ⇒ Object
insere transacoes novas nas tabelas etht (trx normais), ethk (trx token).
-
#sql(cmd, red = []) ⇒ Google::Cloud::Bigquery::Data
cria Structured Query Language (SQL) job bigquery.
-
#transacoes ⇒ Carteiras
API etherscan - processar transacoes normais e tokens.
Constructor Details
#initialize(pop) ⇒ Bigquery
Returns API bigquery & API etherscan.
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
#api ⇒ Google::Cloud::Bigquery (readonly)
Returns API bigquery.
13 14 15 |
# File 'lib/etht/bigquery.rb', line 13 def api @api end |
#job ⇒ Google::Cloud::Bigquery::QueryJob (readonly)
Returns job bigquery.
15 16 17 |
# File 'lib/etht/bigquery.rb', line 15 def job @job end |
#ops ⇒ Thor::CoreExt::HashWithIndifferentAccess (readonly)
Returns opcoes trabalho.
17 18 19 |
# File 'lib/etht/bigquery.rb', line 17 def ops @ops end |
#sqr ⇒ Google::Cloud::Bigquery::Data (readonly)
Returns resultado do SQL.
19 20 21 |
# File 'lib/etht/bigquery.rb', line 19 def sqr @sqr end |
Instance Method Details
#carteiras ⇒ Carteiras
Returns 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
158 159 160 |
# File 'lib/etht/bigquery.rb', line 158 def dml(cmd) job?(cmd) ? 0 : job.num_dml_affected_rows end |
#ethk_ins ⇒ String
Returns 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).
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).
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).
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_ins ⇒ String
Returns 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).
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).
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).
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
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 |
#processa ⇒ Object
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
150 151 152 |
# File 'lib/etht/bigquery.rb', line 150 def sql(cmd, red = []) @sqr = job?(cmd) ? red : job.data end |
#transacoes ⇒ Carteiras
Returns 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 |