cns 0.1.4 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @author Hernani Rodrigues Vaz
4
+ module Cns
5
+ # classe para processar historicos da beaconchain
6
+ class Beaconchain
7
+ # @return [String] texto validadores & saldos historicos
8
+ def mostra_resumo
9
+ return unless dados.count.positive?
10
+
11
+ puts("\nindex address beaconchain blh bigquery blh")
12
+ dados.each { |obj| puts(formata_validador(obj)) }
13
+ mostra_saldos
14
+ end
15
+
16
+ # @param [Hash] hjn dados juntos bigquery & beaconchain
17
+ # @return [String] texto formatado dum validador
18
+ def formata_validador(hjn)
19
+ format(
20
+ '%<s1>-5.5s %<s2>-34.34s ',
21
+ s1: hjn[:id],
22
+ s2: formata_endereco(hjn[:ax], 34)
23
+ ) + formata_valores(hjn)
24
+ end
25
+
26
+ # @param (see formata_validador)
27
+ # @return [String] texto formatado valores dum validador
28
+ def formata_valores(hjn)
29
+ format(
30
+ '%<v1>11.6f %<n1>3i %<v2>12.6f %<n2>6i %<ok>-3s',
31
+ v1: hjn[:es],
32
+ n1: hjn[:eb].count,
33
+ v2: hjn[:bs],
34
+ n2: hjn[:bb].count,
35
+ ok: ok?(hjn) ? 'OK' : 'NOK'
36
+ )
37
+ end
38
+
39
+ # @param (see formata_validador)
40
+ # @return [Boolean] validador tem historicos novos(sim=NOK, nao=OK)?
41
+ def ok?(hjn)
42
+ hjn[:bs] == hjn[:es]
43
+ end
44
+
45
+ # @example pubkey inicio..fim
46
+ # 0x10f3a0cf0b534c..c033cf32e8a03586
47
+ # @param [String] add chave publica validador
48
+ # @param [Integer] max chars a mostrar
49
+ # @return [String] pubkey formatada
50
+ def formata_endereco(add, max)
51
+ int = Integer((max - 2) / 2)
52
+ max < 7 ? 'erro' : "#{add[0, int - 3]}..#{add[-int - 3..]}"
53
+ end
54
+
55
+ # @example
56
+ # {
57
+ # balance: 32_489_497_108,
58
+ # effectivebalance: 32_000_000_000,
59
+ # epoch: 8296,
60
+ # validatorindex: 11_766,
61
+ # week: 5
62
+ # }
63
+ # @param [Hash] hbh historico beaconchain
64
+ # @return [String] texto formatado historico beaconchain
65
+ def formata_saldos(hbh)
66
+ idx = hbh[:validatorindex]
67
+ epc = hbh[:epoch]
68
+ format(
69
+ '%<vi>5i %<vl>17.6f %<ep>6i %<id>9i',
70
+ vi: idx,
71
+ vl: (hbh[:balance].to_d / 10**9).round(10),
72
+ ep: epc,
73
+ id: itx(epc, idx)
74
+ )
75
+ end
76
+
77
+ # @return [String] texto historico saldos
78
+ def mostra_saldos
79
+ return unless ops[:v] && nov.count.positive?
80
+
81
+ puts("\nindex saldo epoch itx")
82
+ sorbx.each { |obj| puts(formata_saldos(obj)) }
83
+ end
84
+
85
+ # @return [Array<Hash>] lista ordenada historico saldos
86
+ def sorbx
87
+ nov.sort { |ant, prx| ant[:itx] <=> prx[:itx] }
88
+ end
89
+ end
90
+ end
@@ -8,7 +8,7 @@ module Cns
8
8
  BD = 'hernanirvaz.coins'
9
9
 
10
10
  # (see Bigquery)
11
- class Cns::Bigquery
11
+ class Bigquery
12
12
  # @return [Google::Cloud::Bigquery] API bigquery
13
13
  attr_reader :api
14
14
  # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
@@ -30,7 +30,7 @@ module Cns
30
30
  @ops = pop
31
31
  end
32
32
 
33
- # mostra situacao completa entre kraken/bitcoinde/paymium/therock/etherscan/greymass & bigquery
33
+ # mostra situacao completa entre kraken/bitcoinde/paymium/therock/etherscan/greymass/beaconchain & bigquery
34
34
  def mostra_tudo
35
35
  apius.mostra_resumo
36
36
  apide.mostra_resumo
@@ -38,51 +38,55 @@ module Cns
38
38
  apimt.mostra_resumo
39
39
  apies.mostra_resumo
40
40
  apigm.mostra_resumo
41
+ apibc.mostra_resumo
41
42
  end
42
43
 
43
- # insere (caso existam) transacoes novas kraken/bitcoinde/paymium/therock/etherscan/greymass no bigquery
44
+ # insere (caso existam) dados novos kraken/bitcoinde/paymium/therock/etherscan/greymass/beaconchain no bigquery
44
45
  def processa_tudo
45
46
  processa_us
46
47
  processa_de
47
- processa_fr
48
- processa_mt
48
+ processa_frmt
49
49
  processa_eth
50
50
  processa_eos
51
+ processa_bc
51
52
  end
52
53
 
53
54
  private
54
55
 
55
56
  # insere transacoes exchange kraken novas nas tabelas ust (trades), usl (ledger)
56
57
  def processa_us
57
- puts(format("%<n>2i TRADES KRAKEN INSERIDAS #{BD}.ust", n: apius.trades.empty? ? 0 : dml(ust_ins)))
58
- puts(format("%<n>2i LEDGER KRAKEN INSERIDAS #{BD}.usl", n: apius.ledger.empty? ? 0 : dml(usl_ins)))
58
+ puts(format("%<n>4i TRADES\tKRAKEN\t\tINSERIDAS ust", n: apius.trades.empty? ? 0 : dml(ust_ins)))
59
+ puts(format("%<n>4i LEDGER\tKRAKEN\t\tINSERIDAS usl", n: apius.ledger.empty? ? 0 : dml(usl_ins)))
59
60
  end
60
61
 
61
62
  # insere transacoes exchange bitcoinde novas nas tabelas det (trades), del (ledger)
62
63
  def processa_de
63
- puts(format("%<n>2i TRADES BITCOINDE INSERIDAS #{BD}.det", n: apide.trades.empty? ? 0 : dml(det_ins)))
64
- puts(format("%<n>2i LEDGER BITCOINDE INSERIDAS #{BD}.del", n: apide.ledger.empty? ? 0 : dml(del_ins)))
64
+ puts(format("%<n>4i TRADES\tBITCOINDE\tINSERIDAS det", n: apide.trades.empty? ? 0 : dml(det_ins)))
65
+ puts(format("%<n>4i LEDGER\tBITCOINDE\tINSERIDAS del", n: apide.ledger.empty? ? 0 : dml(del_ins)))
65
66
  end
66
67
 
67
- # insere transacoes exchange paymium novas na tabela fr (ledger)
68
- def processa_fr
69
- puts(format("%<n>2i LEDGER PAYMIUM INSERIDAS #{BD}.fr", n: apifr.ledger.empty? ? 0 : dml(frl_ins)))
70
- end
71
-
72
- # insere transacoes exchange therock novas na tabela mt (ledger)
73
- def processa_mt
74
- puts(format("%<n>2i LEDGER THEROCK INSERIDAS #{BD}.mt", n: apimt.ledger.empty? ? 0 : dml(mtl_ins)))
68
+ # insere transacoes exchange paymium/therock novas na tabela fr/mt (ledger)
69
+ def processa_frmt
70
+ puts(format("%<n>4i LEDGER\tPAYMIUM\t\tINSERIDAS fr", n: apifr.ledger.empty? ? 0 : dml(frl_ins)))
71
+ puts(format("%<n>4i LEDGER\tTHEROCK\t\tINSERIDAS mt", n: apimt.ledger.empty? ? 0 : dml(mtl_ins)))
75
72
  end
76
73
 
77
74
  # insere transacoes blockchain novas nas tabelas etht (norml), ethk (token)
78
75
  def processa_eth
79
- puts(format("%<n>2i TRANSACOES ETH INSERIDAS #{BD}.etht", n: apies.novtx.empty? ? 0 : dml(etht_ins)))
80
- puts(format("%<n>2i TOKEN EVENTS ETH INSERIDAS #{BD}.ethk", n: apies.novkx.empty? ? 0 : dml(ethk_ins)))
76
+ puts(format("%<n>4i TRANSACOES\tETH\t\tINSERIDAS etht", n: apies.novtx.empty? ? 0 : dml(etht_ins)))
77
+ puts(format("%<n>4i TOKENS\tETH\t\tINSERIDAS ethk", n: apies.novkx.empty? ? 0 : dml(ethk_ins)))
81
78
  end
82
79
 
83
80
  # insere transacoes blockchain novas na tabela eos
84
81
  def processa_eos
85
- puts(format("%<n>2i TRANSACOES EOS INSERIDAS #{BD}.eos ", n: apigm.novax.empty? ? 0 : dml(eost_ins)))
82
+ puts(format("%<n>4i TRANSACOES\tEOS\t\tINSERIDAS eos ", n: apigm.novax.empty? ? 0 : dml(eost_ins)))
83
+ end
84
+
85
+ # insere historico sados novos na tabela eth2bh
86
+ def processa_bc
87
+ # puts(format("%<n>4i ATTESTATIONS INSERIDAS #{BD}.eth2at", n: apibc.novtx.empty? ? 0 : dml(eth2at_ins)))
88
+ # puts(format("%<n>4i PROPOSALS INSERIDAS #{BD}.eth2pr", n: apibc.novkx.empty? ? 0 : dml(eth2pr_ins)))
89
+ puts(format("%<n>4i BALANCES\tETH2\t\tINSERIDOS eth2bh", n: apibc.nov.empty? ? 0 : dml(eth2bh_ins)))
86
90
  end
87
91
 
88
92
  # cria job bigquery & verifica execucao
@@ -91,9 +95,10 @@ module Cns
91
95
  # @return [Boolean] job ok?
92
96
  def job?(cmd)
93
97
  @job = api.query_job(cmd)
94
- @job.wait_until_done!
95
- puts(@job.error['message']) if @job.failed?
96
- @job.failed?
98
+ job.wait_until_done!
99
+ fld = job.failed?
100
+ puts(job.error['message']) if fld
101
+ fld
97
102
  end
98
103
 
99
104
  # cria Structured Query Language (SQL) job bigquery
@@ -6,6 +6,18 @@ module Cns
6
6
  class Bigquery
7
7
  private
8
8
 
9
+ # @return [String] comando insert SQL formatado fr (ledger)
10
+ def mtl_ins
11
+ "insert #{BD}.mt(id,time,type,valor,moe,pair,note,trade_id,dias) " \
12
+ "VALUES#{apimt.ledger.map { |obj| mtl_1val(obj) }.join(',')}"
13
+ end
14
+
15
+ # @return [String] comando insert SQL formatado eth2bh
16
+ def eth2bh_ins
17
+ "insert #{BD}.eth2bh(balance,effectivebalance,epoch,validatorindex" \
18
+ ") VALUES#{apibc.nov[0..1000].map { |obj| eth2bh_1val(obj) }.join(',')}"
19
+ end
20
+
9
21
  # @return [Etherscan] API blockchain ETH
10
22
  def apies
11
23
  @apies ||= Etherscan.new(
@@ -29,6 +41,17 @@ module Cns
29
41
  )
30
42
  end
31
43
 
44
+ # @return [Beaconchain] API blockchain ETH2
45
+ def apibc
46
+ @apibc ||= Beaconchain.new(
47
+ {
48
+ wb: sql("select * from #{BD}.walletEth2 order by 1"),
49
+ nb: sql("select itx,iax from #{BD}.eth2bhx")
50
+ },
51
+ ops
52
+ )
53
+ end
54
+
32
55
  # @return [Kraken] API exchange kraken
33
56
  def apius
34
57
  @apius ||= Kraken.new(
@@ -13,13 +13,36 @@ module Cns
13
13
  def etht_ins
14
14
  "insert #{BD}.etht(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \
15
15
  'value,gas,gasprice,gasused,iserror,txreceipt_status,input,contractaddress,dias' \
16
- ") VALUES#{apies.novtx.map { |e| etht_val1(e) }.join(',')}"
16
+ ") VALUES#{apies.novtx.map { |obj| etht_1val(obj) }.join(',')}"
17
+ end
18
+
19
+ # @return [String] comando insert SQL formatado ethk (token)
20
+ def ethk_ins
21
+ "insert #{BD}.ethk(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \
22
+ 'value,tokenname,tokensymbol,tokendecimal,gas,gasprice,gasused,input,contractaddress,dias' \
23
+ ") VALUES#{apies.novkx.map { |obj| ethk_1val(obj) }.join(',')}"
24
+ end
25
+
26
+ # @return [String] comando insert SQL formatado eos
27
+ def eost_ins
28
+ "insert #{BD}.eos(gseq,aseq,bnum,time,contract,action,acfrom,acto,iax,amount,moeda,memo,dias" \
29
+ ") VALUES#{apigm.novax.map { |obj| eost_1val(obj) }.join(',')}"
30
+ end
31
+
32
+ # @example (see Beaconchain#formata_saldos)
33
+ # @param (see Beaconchain#formata_saldos)
34
+ # @return [String] valores formatados etht (norml parte1)
35
+ def eth2bh_1val(htb)
36
+ "(#{Integer(htb[:balance])}," \
37
+ "#{Integer(htb[:effectivebalance])}," \
38
+ "#{Integer(htb[:epoch])}," \
39
+ "#{Integer(htb[:validatorindex])})"
17
40
  end
18
41
 
19
42
  # @example (see Apibc#norml_es)
20
43
  # @param [Hash] htx transacao norml etherscan
21
44
  # @return [String] valores formatados etht (norml parte1)
22
- def etht_val1(htx)
45
+ def etht_1val(htx)
23
46
  "(#{Integer(htx[:blockNumber])}," \
24
47
  "#{Integer(htx[:timeStamp])}," \
25
48
  "'#{htx[:hash]}'," \
@@ -29,40 +52,36 @@ module Cns
29
52
  "'#{htx[:from]}'," \
30
53
  "'#{htx[:to]}'," \
31
54
  "'#{htx[:iax]}'," \
32
- "#{etht_val2(htx)}"
55
+ "#{etht_2val(htx)}"
33
56
  end
34
57
 
35
- # @param (see etht_val1)
58
+ # @param (see etht_1val)
36
59
  # @return [String] valores formatados etht (norml parte2)
37
- def etht_val2(htx)
60
+ def etht_2val(htx)
61
+ txr = htx[:txreceipt_status]
38
62
  "cast('#{htx[:value]}' as numeric)," \
39
63
  "cast('#{htx[:gas]}' as numeric)," \
40
64
  "cast('#{htx[:gasPrice]}' as numeric)," \
41
65
  "cast('#{htx[:gasUsed]}' as numeric)," \
42
66
  "#{Integer(htx[:isError])}," \
43
- "#{htx[:txreceipt_status].length.zero? ? 'null' : htx[:txreceipt_status]}," \
44
- "#{etht_val3(htx)}"
67
+ "#{txr.length.zero? ? 'null' : txr}," \
68
+ "#{etht_3val(htx)}"
45
69
  end
46
70
 
47
- # @param (see etht_val1)
71
+ # @param (see etht_1val)
48
72
  # @return [String] valores formatados etht (norml parte3)
49
- def etht_val3(htx)
50
- "#{htx[:input].length.zero? ? 'null' : "'#{htx[:input]}'"}," \
51
- "#{htx[:contractAddress].length.zero? ? 'null' : "'#{htx[:contractAddress]}'"}," \
73
+ def etht_3val(htx)
74
+ cta = htx[:contractAddress]
75
+ inp = htx[:input]
76
+ "#{inp.length.zero? ? 'null' : "'#{inp}'"}," \
77
+ "#{cta.length.zero? ? 'null' : "'#{cta}'"}," \
52
78
  "#{Integer(ops[:h][htx[:blockNumber]] || 0)})"
53
79
  end
54
80
 
55
- # @return [String] comando insert SQL formatado ethk (token)
56
- def ethk_ins
57
- "insert #{BD}.ethk(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \
58
- 'value,tokenname,tokensymbol,tokendecimal,gas,gasprice,gasused,input,contractaddress,dias' \
59
- ") VALUES#{apies.novkx.map { |e| ethk_val1(e) }.join(',')}"
60
- end
61
-
62
81
  # @example (see Apibc#token_es)
63
82
  # @param [Hash] hkx token event etherscan
64
83
  # @return [String] valores formatados ethk (token parte1)
65
- def ethk_val1(hkx)
84
+ def ethk_1val(hkx)
66
85
  "(#{Integer(hkx[:blockNumber])}," \
67
86
  "#{Integer(hkx[:timeStamp])}," \
68
87
  "'#{hkx[:hash]}'," \
@@ -72,12 +91,12 @@ module Cns
72
91
  "'#{hkx[:from]}'," \
73
92
  "'#{hkx[:to]}'," \
74
93
  "'#{hkx[:iax]}'," \
75
- "#{ethk_val2(hkx)}"
94
+ "#{ethk_2val(hkx)}"
76
95
  end
77
96
 
78
- # @param (see ethk_val1)
97
+ # @param (see ethk_1val)
79
98
  # @return [String] valores formatados ethk (token parte2)
80
- def ethk_val2(hkx)
99
+ def ethk_2val(hkx)
81
100
  "cast('#{hkx[:value]}' as numeric)," \
82
101
  "'#{hkx[:tokenName]}'," \
83
102
  "'#{hkx[:tokenSymbol]}'," \
@@ -85,50 +104,93 @@ module Cns
85
104
  "cast('#{hkx[:gas]}' as numeric)," \
86
105
  "cast('#{hkx[:gasPrice]}' as numeric)," \
87
106
  "cast('#{hkx[:gasUsed]}' as numeric)," \
88
- "#{ethk_val3(hkx)}"
107
+ "#{ethk_3val(hkx)}"
89
108
  end
90
109
 
91
- # @param (see ethk_val1)
110
+ # @param (see ethk_1val)
92
111
  # @return [String] valores formatados ethk (token parte3)
93
- def ethk_val3(hkx)
94
- "#{hkx[:input].length.zero? ? 'null' : "'#{hkx[:input]}'"}," \
95
- "#{hkx[:contractAddress].length.zero? ? 'null' : "'#{hkx[:contractAddress]}'"}," \
112
+ def ethk_3val(hkx)
113
+ cta = hkx[:contractAddress]
114
+ inp = hkx[:input]
115
+ "#{inp.length.zero? ? 'null' : "'#{inp}'"}," \
116
+ "#{cta.length.zero? ? 'null' : "'#{cta}'"}," \
96
117
  "#{Integer(ops[:h][hkx[:blockNumber]] || 0)})"
97
118
  end
98
119
 
99
- # @return [String] comando insert SQL formatado eos
100
- def eost_ins
101
- "insert #{BD}.eos(gseq,aseq,bnum,time,contract,action,acfrom,acto,iax,amount,moeda,memo,dias" \
102
- ") VALUES#{apigm.novax.map { |e| eost_val1(e) }.join(',')}"
103
- end
104
-
105
120
  # @example (see Apibc#ledger_gm)
106
121
  # @param [Hash] hlx ledger greymass
107
122
  # @return [String] valores formatados para insert eos (parte1)
108
- def eost_val1(hlx)
109
- a = hlx[:action_trace][:act]
123
+ def eost_1val(hlx)
124
+ act = hlx[:action_trace][:act]
110
125
  "(#{hlx[:global_action_seq]}," \
111
126
  "#{hlx[:account_action_seq]}," \
112
127
  "#{hlx[:block_num]}," \
113
128
  "DATETIME(TIMESTAMP('#{hlx[:block_time]}'))," \
114
- "'#{a[:account]}'," \
115
- "'#{a[:name]}'," \
116
- "#{eost_val2(hlx, a)}"
129
+ "'#{act[:account]}'," \
130
+ "'#{act[:name]}'," \
131
+ "#{eost_2val(hlx, act)}"
117
132
  end
118
133
 
119
- # @param (see eost_val1)
134
+ # @param (see eost_1val)
120
135
  # @param [Hash] act dados da acao
121
136
  # @return [String] valores formatados para insert eos (parte2)
122
- def eost_val2(hlx, act)
123
- d = act[:data]
124
- q = d[:quantity].to_s
125
- s = d[:memo].inspect
126
- "'#{d[:from]}'," \
127
- "'#{d[:to]}'," \
137
+ def eost_2val(hlx, act)
138
+ dat = act[:data]
139
+ qtd = dat[:quantity].to_s
140
+ str = dat[:memo].inspect
141
+ "'#{dat[:from]}'," \
142
+ "'#{dat[:to]}'," \
128
143
  "'#{hlx[:iax]}'," \
129
- "#{q.to_d},'#{q[/[[:upper:]]+/]}'," \
130
- "nullif('#{s.gsub(/['"]/, '')}','nil')," \
144
+ "#{qtd.to_d},'#{qtd[/[[:upper:]]+/]}'," \
145
+ "nullif('#{str.gsub(/['"]/, '')}','nil')," \
131
146
  "#{ops[:h][String(hlx[:itx])] || 0})"
132
147
  end
148
+
149
+ # def eth2at_ins
150
+ # "insert #{BD}.eth2at(attesterslot,committeeindex,epoch,inclusionslot,status,validatorindex" \
151
+ # ") VALUES#{apibc.novtx.map { |obj| eth2at_1val(obj) }.join(',')}"
152
+ # end
153
+ # def eth2pr_ins
154
+ # "insert #{BD}.eth2pr(attestationscount,attesterslashingscount,blockroot,depositscount,epoch," \
155
+ # 'eth1data_blockhash,eth1data_depositcount,eth1data_depositroot,graffiti,graffiti_text,parentroot,' \
156
+ # 'proposer,proposerslashingscount,randaoreveal,signature,slot,stateroot,status,voluntaryexitscount' \
157
+ # ") VALUES#{apibc.novkx.map { |obj| eth2pr_1val(obj) }.join(',')}"
158
+ # end
159
+ # def eth2at_1val(htx)
160
+ # "(#{Integer(htx[:attesterslot])}," \
161
+ # "#{Integer(htx[:committeeindex])}," \
162
+ # "#{Integer(htx[:epoch])}," \
163
+ # "#{Integer(htx[:inclusionslot])}," \
164
+ # "#{Integer(htx[:status])}," \
165
+ # "#{Integer(htx[:validatorindex])})"
166
+ # end
167
+ # def eth2pr_1val(htx)
168
+ # "(#{Integer(htx[:attestationscount])}," \
169
+ # "#{Integer(htx[:attesterslashingscount])}," \
170
+ # "'#{htx[:blockroot]}'," \
171
+ # "#{Integer(htx[:depositscount])}," \
172
+ # "#{Integer(htx[:epoch])}," \
173
+ # "'#{htx[:eth1data_blockhash]}'," \
174
+ # "#{eth2pr_2val(htx)}"
175
+ # end
176
+ # def eth2pr_2val(htx)
177
+ # grf = htx[:graffiti_text]
178
+ # "#{Integer(htx[:eth1data_depositcount])}," \
179
+ # "'#{htx[:eth1data_depositroot]}'," \
180
+ # "'#{htx[:graffiti]}'," \
181
+ # "#{grf.length.zero? ? 'null' : "'#{grf}'"}," \
182
+ # "'#{htx[:parentroot]}'," \
183
+ # "#{Integer(htx[:proposer])}," \
184
+ # "#{eth2pr_3val(htx)}"
185
+ # end
186
+ # def eth2pr_3val(htx)
187
+ # "#{Integer(htx[:proposerslashingscount])}," \
188
+ # "'#{htx[:randaoreveal]}'," \
189
+ # "'#{htx[:signature]}'," \
190
+ # "#{Integer(htx[:slot])}," \
191
+ # "'#{htx[:stateroot]}'," \
192
+ # "#{Integer(htx[:status])}," \
193
+ # "#{Integer(htx[:voluntaryexitscount])})"
194
+ # end
133
195
  end
134
196
  end