class Etht::Bigquery

classe para processar etherscan & bigquery

Attributes

api[R]

@return [Google::Cloud::Bigquery] API bigquery

job[R]

@return [Google::Cloud::Bigquery::QueryJob] job bigquery

ops[R]

@return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho

sqr[R]

@return (see sql)

Public Class Methods

new(pop) click to toggle source

@param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho @option pop [Hash] :h ({}) configuracao ajuste reposicionamento temporal @option pop [Boolean] :v (false) mostra transacoes normais & token? @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas? @return [Bigquery] API bigquery & API etherscan

# 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

Public Instance Methods

carteiras() click to toggle source

@return [Carteiras] API etherscan - processar carteiras & transacoes normais e tokens

# File lib/etht/bigquery.rb, line 47
def carteiras
  transacoes
end
dml(cmd) click to toggle source

cria Data Manipulation Language (DML) job bigquery

@param cmd (see sql) @return [Integer] numero linhas afetadas

# File lib/etht/bigquery.rb, line 158
def dml(cmd)
  job?(cmd) ? 0 : job.num_dml_affected_rows
end
ethk_ins() click to toggle source

@return [String] comando insert SQL formatado ethk (trx token)

# 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) click to toggle source

@return [String] valores formatados ethk (trx token parte1)

# 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) click to toggle source

@return [String] valores formatados ethk (trx token parte2)

# 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) click to toggle source

@return [String] valores formatados ethk (trx token parte3)

# 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() click to toggle source

@return [String] comando insert SQL formatado etht (trx normais)

# 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) click to toggle source

@return [String] valores formatados etht (trx normais parte1)

# 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) click to toggle source

@return [String] valores formatados etht (trx normais parte2)

# 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) click to toggle source

@return [String] valores formatados etht (trx normais parte3)

# 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) click to toggle source

cria job bigquery & verifica execucao

@param cmd (see sql) @return [Boolean] job ok?

# 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() click to toggle source

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

# 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 = []) click to toggle source

cria Structured Query Language (SQL) job bigquery

@param [String] cmd comando SQL a executar @param [String] red resultado quando SQL tem erro @return [Google::Cloud::Bigquery::Data] resultado do SQL

# File lib/etht/bigquery.rb, line 150
def sql(cmd, red = [])
  @sqr = job?(cmd) ? red : job.data
end
transacoes() click to toggle source

@return [Carteiras] API etherscan - processar transacoes normais e tokens

# 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