cns 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,620 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('google/cloud/bigquery')
4
+ require('bigdecimal/util')
5
+
6
+ # @author Hernani Rodrigues Vaz
7
+ module Cns
8
+ BD = 'hernanirvaz.coins'
9
+
10
+ # classe para processar bigquery
11
+ class Bigquery
12
+ # @return [Google::Cloud::Bigquery] API bigquery
13
+ attr_reader :api
14
+ # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
15
+ attr_reader :job
16
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
17
+ attr_reader :ops
18
+ # @return (see sql)
19
+ attr_reader :sqr
20
+
21
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
22
+ # @option pop [Hash] :h ({}) configuracao ajuste reposicionamento temporal
23
+ # @option pop [Boolean] :v (false) mostra transacoes trades & ledger?
24
+ # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
25
+ # @return [Bigquery] API bigquery
26
+ def initialize(pop)
27
+ # usa env GOOGLE_APPLICATION_CREDENTIALS para obter credentials
28
+ # @see https://cloud.google.com/bigquery/docs/authentication/getting-started
29
+ @api = Google::Cloud::Bigquery.new
30
+ @ops = pop
31
+ end
32
+
33
+ # mostra situacao completa entre kraken/bitcoinde/paymium/therock/etherscan/greymass/beaconchain & bigquery
34
+ def mostra_tudo
35
+ apius.mostra_resumo
36
+ apide.mostra_resumo
37
+ #apifr.mostra_resumo
38
+ #apimt.mostra_resumo
39
+ apies.mostra_resumo
40
+ apigm.mostra_resumo
41
+ #apibc.mostra_resumo
42
+ end
43
+
44
+ # insere (caso existam) dados novos kraken/bitcoinde/paymium/therock/etherscan/greymass/beaconchain no bigquery
45
+ def processa_tudo
46
+ processa_us
47
+ processa_de
48
+ #processa_frmt
49
+ processa_eth
50
+ processa_eos
51
+ #processa_bc
52
+ end
53
+
54
+ # insere transacoes blockchain novas nas tabelas etht (norml), ethi (internas), ethp (block), ethk (token)
55
+ def processa_eth
56
+ puts(format("%<n>4i TRANSACOES ETH NORMAIS\tINSERIDAS etht", n: apies.novtx.empty? ? 0 : dml(etht_ins)))
57
+ puts(format("%<n>4i TRANSACOES ETH INTERNAS\tINSERIDAS ethi", n: apies.novix.empty? ? 0 : dml(ethi_ins)))
58
+ puts(format("%<n>4i TRANSACOES ETH BLOCK\tINSERIDAS ethp", n: apies.novpx.empty? ? 0 : dml(ethp_ins)))
59
+ puts(format("%<n>4i TRANSACOES ETH WITHDRAWALS\tINSERIDAS ethw", n: apies.novwx.empty? ? 0 : dml(ethw_ins)))
60
+ puts(format("%<n>4i TOKENS\tETH\t\tINSERIDAS ethk", n: apies.novkx.empty? ? 0 : dml(ethk_ins)))
61
+ end
62
+
63
+ private
64
+
65
+ # insere transacoes exchange kraken novas nas tabelas ust (trades), usl (ledger)
66
+ def processa_us
67
+ puts(format("%<n>4i TRADES\tKRAKEN\t\tINSERIDAS ust", n: apius.trades.empty? ? 0 : dml(ust_ins)))
68
+ puts(format("%<n>4i LEDGER\tKRAKEN\t\tINSERIDAS usl", n: apius.ledger.empty? ? 0 : dml(usl_ins)))
69
+ end
70
+
71
+ # insere transacoes exchange bitcoinde novas nas tabelas det (trades), del (ledger)
72
+ def processa_de
73
+ puts(format("%<n>4i TRADES\tBITCOINDE\tINSERIDAS det", n: apide.trades.empty? ? 0 : dml(det_ins)))
74
+ puts(format("%<n>4i LEDGER\tBITCOINDE\tINSERIDAS del", n: apide.ledger.empty? ? 0 : dml(del_ins)))
75
+ end
76
+
77
+ # insere transacoes exchange paymium/therock novas na tabela fr/mt (ledger)
78
+ def processa_frmt
79
+ puts(format("%<n>4i LEDGER\tPAYMIUM\t\tINSERIDAS fr", n: apifr.ledger.empty? ? 0 : dml(frl_ins)))
80
+ puts(format("%<n>4i LEDGER\tTHEROCK\t\tINSERIDAS mt", n: apimt.ledger.empty? ? 0 : dml(mtl_ins)))
81
+ end
82
+
83
+ # insere transacoes blockchain novas na tabela eos
84
+ def processa_eos
85
+ puts(format("%<n>4i TRANSACOES\tEOS\t\tINSERIDAS eos ", n: apigm.novax.empty? ? 0 : dml(eost_ins)))
86
+ end
87
+
88
+ # insere historico sados novos na tabela eth2bh
89
+ def processa_bc
90
+ # puts(format("%<n>4i ATTESTATIONS INSERIDAS eth2at", n: apibc.novtx.empty? ? 0 : dml(eth2at_ins)))
91
+ # puts(format("%<n>4i PROPOSALS INSERIDAS eth2pr", n: apibc.novkx.empty? ? 0 : dml(eth2pr_ins)))
92
+ puts(format("%<n>4i BALANCES\tETH2\t\tINSERIDOS eth2bh", n: apibc.nov.empty? ? 0 : dml(eth2bh_ins)))
93
+ end
94
+
95
+ # cria job bigquery & verifica execucao
96
+ #
97
+ # @param cmd (see sql)
98
+ # @return [Boolean] job ok?
99
+ def job?(cmd)
100
+ @job = api.query_job(cmd)
101
+ job.wait_until_done!
102
+ fld = job.failed?
103
+ puts(job.error['message']) if fld
104
+ fld
105
+ end
106
+
107
+ # cria Structured Query Language (SQL) job bigquery
108
+ #
109
+ # @param [String] cmd comando SQL a executar
110
+ # @param [String] res resultado quando SQL tem erro
111
+ # @return [Google::Cloud::Bigquery::Data] resultado do SQL
112
+ def sql(cmd, res = [])
113
+ @sqr = job?(cmd) ? res : job.data
114
+ end
115
+
116
+ # cria Data Manipulation Language (DML) job bigquery
117
+ #
118
+ # @param cmd (see sql)
119
+ # @return [Integer] numero linhas afetadas
120
+ def dml(cmd)
121
+ job?(cmd) ? 0 : job.num_dml_affected_rows
122
+ end
123
+
124
+ # @return [String] comando insert SQL formatado fr (ledger)
125
+ def mtl_ins
126
+ "insert #{BD}.mt(id,time,type,valor,moe,pair,note,trade_id,dias) " \
127
+ "VALUES#{apimt.ledger.map { |obj| mtl_1val(obj) }.join(',')}"
128
+ end
129
+
130
+ # @return [String] comando insert SQL formatado eth2bh
131
+ def eth2bh_ins
132
+ "insert #{BD}.eth2bh(balance,effectivebalance,epoch,validatorindex" \
133
+ ") VALUES#{apibc.nov[0..1000].map { |obj| eth2bh_1val(obj) }.join(',')}"
134
+ end
135
+
136
+ # @return [Etherscan] API blockchain ETH
137
+ def apies
138
+ @apies ||= Etherscan.new(
139
+ {
140
+ wb: sql("select * from #{BD}.walletEth order by 2"),
141
+ nt: sql("select itx,iax from #{BD}.ethtx"),
142
+ ni: sql("select itx,iax from #{BD}.ethix"),
143
+ np: sql("select itx,iax from #{BD}.ethpx"),
144
+ nw: sql("select itx,iax from #{BD}.ethwx"),
145
+ nk: sql("select itx,iax from #{BD}.ethkx")
146
+ },
147
+ ops
148
+ )
149
+ end
150
+
151
+ # @return [Greymass] API blockchain EOS
152
+ def apigm
153
+ @apigm ||= Greymass.new(
154
+ {
155
+ wb: sql("select * from #{BD}.walletEos order by 2"),
156
+ nt: sql("select itx,iax from #{BD}.eostx")
157
+ },
158
+ ops
159
+ )
160
+ end
161
+
162
+ # @return [Beaconchain] API blockchain ETH2
163
+ def apibc
164
+ @apibc ||= Beaconchain.new(
165
+ {
166
+ wb: sql("select * from #{BD}.walletEth2 order by 1"),
167
+ nb: sql("select itx,iax from #{BD}.eth2bhx")
168
+ },
169
+ ops
170
+ )
171
+ end
172
+
173
+ # @return [Kraken] API exchange kraken
174
+ def apius
175
+ @apius ||= Kraken.new(
176
+ {
177
+ sl: sql("select sum(btc) xxbt,sum(eth) xeth,sum(eos) eos,sum(eur) zeur from #{BD}.ussl")[0],
178
+ nt: sql("select * from #{BD}.ustx order by time,txid"),
179
+ nl: sql("select * from #{BD}.uslx order by time,txid")
180
+ },
181
+ ops
182
+ )
183
+ end
184
+
185
+ # @return [Bitcoinde] API exchange bitcoinde
186
+ def apide
187
+ @apide ||= Bitcoinde.new(
188
+ {
189
+ sl: sql("select sum(btc) btc from #{BD}.desl")[0],
190
+ nt: sql("select * from #{BD}.detx order by time,txid"),
191
+ nl: sql("select * from #{BD}.delx order by time,txid")
192
+ },
193
+ ops
194
+ )
195
+ end
196
+
197
+ # @return [Paymium] API exchange paymium
198
+ def apifr
199
+ @apifr ||= Paymium.new(
200
+ {
201
+ sl: sql("select sum(btc) btc,sum(eur) eur from #{BD}.frsl")[0],
202
+ nl: sql("select * from #{BD}.frlx order by time,txid")
203
+ },
204
+ ops
205
+ )
206
+ end
207
+
208
+ # @return [TheRock] API exchange therock
209
+ def apimt
210
+ @apimt ||= TheRock.new(
211
+ {
212
+ sl: sql("select sum(btc) btc,sum(eur) eur from #{BD}.mtsl")[0],
213
+ nl: sql("select * from #{BD}.mtlx order by time,txid")
214
+ },
215
+ ops
216
+ )
217
+ end
218
+
219
+ # @return [String] comando insert SQL formatado etht (norml)
220
+ def etht_ins
221
+ "insert #{BD}.etht(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \
222
+ 'value,gas,gasprice,gasused,iserror,txreceipt_status,input,contractaddress,dias' \
223
+ ") VALUES#{apies.novtx.map { |obj| etht_1val(obj) }.join(',')}"
224
+ end
225
+
226
+ # @return [String] comando insert SQL formatado ethi (internas)
227
+ def ethi_ins
228
+ "insert #{BD}.ethi(blocknumber,timestamp,txhash,axfrom,axto,iax," \
229
+ 'value,contractaddress,input,type,gas,gasused,traceid,iserror,errcode' \
230
+ ") VALUES#{apies.novix.map { |obj| ethi_1val(obj) }.join(',')}"
231
+ end
232
+
233
+ # @return [String] comando insert SQL formatado ethp (block)
234
+ def ethp_ins
235
+ "insert #{BD}.ethp(blocknumber,timestamp,blockreward,iax" \
236
+ ") VALUES#{apies.novpx.map { |obj| ethp_1val(obj) }.join(',')}"
237
+ end
238
+
239
+ # @return [String] comando insert SQL formatado ethw (withdrawals)
240
+ def ethw_ins
241
+ "insert #{BD}.ethw(withdrawalindex,validatorindex,address,amount,blocknumber,timestamp" \
242
+ ") VALUES#{apies.novwx.map { |obj| ethw_1val(obj) }.join(',')}"
243
+ end
244
+
245
+ # @return [String] comando insert SQL formatado ethk (token)
246
+ def ethk_ins
247
+ "insert #{BD}.ethk(blocknumber,timestamp,txhash,nonce,blockhash,transactionindex,axfrom,axto,iax," \
248
+ 'value,tokenname,tokensymbol,tokendecimal,gas,gasprice,gasused,input,contractaddress,dias' \
249
+ ") VALUES#{apies.novkx.map { |obj| ethk_1val(obj) }.join(',')}"
250
+ end
251
+
252
+ # @return [String] comando insert SQL formatado eos
253
+ def eost_ins
254
+ "insert #{BD}.eos(gseq,aseq,bnum,time,contract,action,acfrom,acto,iax,amount,moeda,memo,dias" \
255
+ ") VALUES#{apigm.novax.map { |obj| eost_1val(obj) }.join(',')}"
256
+ end
257
+
258
+ # @return [String] comando insert SQL formatado det (trades)
259
+ def det_ins
260
+ "insert #{BD}.det(txid,time,tp,user,btc,eur,dtc,dias) VALUES#{apide.trades.map { |obj| det_1val(obj) }.join(',')}"
261
+ end
262
+
263
+ # @return [String] comando insert SQL formatado del (ledger)
264
+ def del_ins
265
+ "insert #{BD}.del(txid,time,tp,add,moe,qt,fee) VALUES#{apide.ledger.map { |obj| del_val(obj) }.join(',')}"
266
+ end
267
+
268
+ # @return [String] comando insert SQL formatado ust (trades)
269
+ def ust_ins
270
+ "insert #{BD}.ust(txid,ordertxid,pair,time,type,ordertype,price,cost,fee,vol,margin,misc,ledgers,dias) " \
271
+ "VALUES#{apius.trades.map { |key, val| ust_1val(key, val) }.join(',')}"
272
+ end
273
+
274
+ # @return [String] comando insert SQL formatado usl (ledger)
275
+ def usl_ins
276
+ "insert #{BD}.usl(txid,refid,time,type,aclass,asset,amount,fee) " \
277
+ "VALUES#{apius.ledger.map { |key, val| usl_val(key, val) }.join(',')}"
278
+ end
279
+
280
+ # @return [String] comando insert SQL formatado fr (ledger)
281
+ def frl_ins
282
+ "insert #{BD}.fr(uuid,tipo,valor,moe,time,dias) VALUES#{apifr.ledger.map { |obj| frl_val(obj) }.join(',')}"
283
+ end
284
+
285
+ # @example (see Beaconchain#formata_saldos)
286
+ # @param (see Beaconchain#formata_saldos)
287
+ # @return [String] valores formatados etht (norml parte1)
288
+ def eth2bh_1val(htb)
289
+ "(#{Integer(htb[:balance])}," \
290
+ "#{Integer(htb[:effectivebalance])}," \
291
+ "#{Integer(htb[:epoch])}," \
292
+ "#{Integer(htb[:validatorindex])})"
293
+ end
294
+
295
+ # @example (see Apibc#norml_es)
296
+ # @param [Hash] htx transacao norml etherscan
297
+ # @return [String] valores formatados etht (norml parte1)
298
+ def etht_1val(htx)
299
+ "(#{Integer(htx[:blockNumber])}," \
300
+ "#{Integer(htx[:timeStamp])}," \
301
+ "'#{htx[:hash]}'," \
302
+ "#{Integer(htx[:nonce])}," \
303
+ "'#{htx[:blockHash]}'," \
304
+ "#{Integer(htx[:transactionIndex])}," \
305
+ "'#{htx[:from]}'," \
306
+ "'#{htx[:to]}'," \
307
+ "'#{htx[:iax]}'," \
308
+ "#{etht_2val(htx)}"
309
+ end
310
+
311
+ # @param (see etht_1val)
312
+ # @return [String] valores formatados etht (norml parte2)
313
+ def etht_2val(htx)
314
+ txr = htx[:txreceipt_status]
315
+ "cast('#{htx[:value]}' as numeric)," \
316
+ "cast('#{htx[:gas]}' as numeric)," \
317
+ "cast('#{htx[:gasPrice]}' as numeric)," \
318
+ "cast('#{htx[:gasUsed]}' as numeric)," \
319
+ "#{Integer(htx[:isError])}," \
320
+ "#{txr.length.zero? ? 'null' : txr}," \
321
+ "#{etht_3val(htx)}"
322
+ end
323
+
324
+ # @param (see etht_1val)
325
+ # @return [String] valores formatados etht (norml parte3)
326
+ def etht_3val(htx)
327
+ cta = htx[:contractAddress]
328
+ inp = htx[:input]
329
+ "#{inp.length.zero? ? 'null' : "'#{inp}'"}," \
330
+ "#{cta.length.zero? ? 'null' : "'#{cta}'"}," \
331
+ "#{Integer(ops[:h][htx[:blockNumber]] || 0)})"
332
+ end
333
+
334
+ # @example (see Apibc#inter_es)
335
+ # @param [Hash] htx transacao internas etherscan
336
+ # @return [String] valores formatados ethi (internas parte1)
337
+ def ethi_1val(htx)
338
+ cta = htx[:contractAddress]
339
+ "(#{Integer(htx[:blockNumber])}," \
340
+ "#{Integer(htx[:timeStamp])}," \
341
+ "'#{htx[:hash]}'," \
342
+ "'#{htx[:from]}'," \
343
+ "'#{htx[:to]}'," \
344
+ "'#{htx[:iax]}'," \
345
+ "cast('#{htx[:value]}' as numeric)," \
346
+ "#{cta.length.zero? ? 'null' : "'#{cta}'"}," \
347
+ "#{ethi_2val(htx)}"
348
+ end
349
+
350
+ # @param (see ethi_1val)
351
+ # @return [String] valores formatados ethi (internas parte2)
352
+ def ethi_2val(htx)
353
+ inp = htx[:input]
354
+ tid = htx[:traceId]
355
+ txr = htx[:errCode]
356
+ "#{inp.length.zero? ? 'null' : "'#{inp}'"}," \
357
+ "'#{htx[:type]}'," \
358
+ "cast('#{htx[:gas]}' as numeric)," \
359
+ "cast('#{htx[:gasUsed]}' as numeric)," \
360
+ "#{tid.length.zero? ? 'null' : "'#{tid}'"}," \
361
+ "#{Integer(htx[:isError])}," \
362
+ "#{txr.length.zero? ? 'null' : txr})"
363
+ end
364
+
365
+ # @example (see Apibc#block_es)
366
+ # @param [Hash] htx transacao block etherscan
367
+ # @return [String] valores formatados ethi (block parte1)
368
+ def ethp_1val(htx)
369
+ "(#{Integer(htx[:blockNumber])}," \
370
+ "#{Integer(htx[:timeStamp])}," \
371
+ "cast('#{htx[:blockReward]}' as numeric)," \
372
+ "'#{htx[:iax]}')"
373
+ end
374
+
375
+ # @example (see Apibc#block_es)
376
+ # @param [Hash] htx transacao withdrawals etherscan
377
+ # @return [String] valores formatados ethi (withdrawals parte1)
378
+ def ethw_1val(htx)
379
+ "(#{Integer(htx[:withdrawalIndex])}," \
380
+ "#{Integer(htx[:validatorIndex])}," \
381
+ "'#{htx[:address]}'," \
382
+ "cast('#{htx[:amount]}' as numeric)," \
383
+ "#{Integer(htx[:blockNumber])}," \
384
+ "#{Integer(htx[:timestamp])})"
385
+ end
386
+
387
+ # @example (see Apibc#token_es)
388
+ # @param [Hash] hkx token event etherscan
389
+ # @return [String] valores formatados ethk (token parte1)
390
+ def ethk_1val(hkx)
391
+ "(#{Integer(hkx[:blockNumber])}," \
392
+ "#{Integer(hkx[:timeStamp])}," \
393
+ "'#{hkx[:hash]}'," \
394
+ "#{Integer(hkx[:nonce])}," \
395
+ "'#{hkx[:blockHash]}'," \
396
+ "#{Integer(hkx[:transactionIndex])}," \
397
+ "'#{hkx[:from]}'," \
398
+ "'#{hkx[:to]}'," \
399
+ "'#{hkx[:iax]}'," \
400
+ "#{ethk_2val(hkx)}"
401
+ end
402
+
403
+ # @param (see ethk_1val)
404
+ # @return [String] valores formatados ethk (token parte2)
405
+ def ethk_2val(hkx)
406
+ "cast('#{hkx[:value]}' as numeric)," \
407
+ "'#{hkx[:tokenName]}'," \
408
+ "'#{hkx[:tokenSymbol]}'," \
409
+ "#{Integer(hkx[:tokenDecimal])}," \
410
+ "cast('#{hkx[:gas]}' as numeric)," \
411
+ "cast('#{hkx[:gasPrice]}' as numeric)," \
412
+ "cast('#{hkx[:gasUsed]}' as numeric)," \
413
+ "#{ethk_3val(hkx)}"
414
+ end
415
+
416
+ # @param (see ethk_1val)
417
+ # @return [String] valores formatados ethk (token parte3)
418
+ def ethk_3val(hkx)
419
+ cta = hkx[:contractAddress]
420
+ inp = hkx[:input]
421
+ "#{inp.length.zero? ? 'null' : "'#{inp}'"}," \
422
+ "#{cta.length.zero? ? 'null' : "'#{cta}'"}," \
423
+ "#{Integer(ops[:h][hkx[:blockNumber]] || 0)})"
424
+ end
425
+
426
+ # @example (see Apibc#ledger_gm)
427
+ # @param [Hash] hlx ledger greymass
428
+ # @return [String] valores formatados para insert eos (parte1)
429
+ def eost_1val(hlx)
430
+ act = hlx[:action_trace][:act]
431
+ "(#{hlx[:global_action_seq]}," \
432
+ "#{hlx[:account_action_seq]}," \
433
+ "#{hlx[:block_num]}," \
434
+ "DATETIME(TIMESTAMP('#{hlx[:block_time]}'))," \
435
+ "'#{act[:account]}'," \
436
+ "'#{act[:name]}'," \
437
+ "#{eost_2val(hlx, act)}"
438
+ end
439
+
440
+ # @param (see eost_1val)
441
+ # @param [Hash] act dados da acao
442
+ # @return [String] valores formatados para insert eos (parte2)
443
+ def eost_2val(hlx, act)
444
+ dat = act[:data]
445
+ qtd = dat[:quantity].to_s
446
+ str = dat[:memo].inspect
447
+ "'#{dat[:from]}'," \
448
+ "'#{dat[:to]}'," \
449
+ "'#{hlx[:iax]}'," \
450
+ "#{qtd.to_d},'#{qtd[/[[:upper:]]+/]}'," \
451
+ "nullif('#{str.gsub(/['"]/, '')}','nil')," \
452
+ "#{ops[:h][String(hlx[:itx])] || 0})"
453
+ end
454
+
455
+ # @example (see Apice#trades_de)
456
+ # @param [Hash] htx trade bitcoinde
457
+ # @return [String] valores formatados det (trades parte1)
458
+ def det_1val(htx)
459
+ "('#{htx[:trade_id]}'," \
460
+ "DATETIME(TIMESTAMP('#{htx[:successfully_finished_at]}'))," \
461
+ "'#{htx[:type]}'," \
462
+ "'#{htx[:trading_partner_information][:username]}'," \
463
+ "#{det_2val(htx)}"
464
+ end
465
+
466
+ # @param (see det_1val)
467
+ # @return [String] valores formatados det (trades parte2)
468
+ def det_2val(htx)
469
+ 'cast(' \
470
+ "#{htx[:type] == 'buy' ? htx[:amount_currency_to_trade_after_fee] : "-#{htx[:amount_currency_to_trade]}"}" \
471
+ ' as numeric),' \
472
+ "cast(#{htx[:volume_currency_to_pay_after_fee]} as numeric)," \
473
+ "DATETIME(TIMESTAMP('#{htx[:trade_marked_as_paid_at]}'))," \
474
+ "#{Integer(ops[:h][htx[:trade_id]] || 0)})"
475
+ end
476
+
477
+ # @example (see Apice#deposits_de)
478
+ # @example (see Apice#withdrawals_de)
479
+ # @param [Hash] hlx ledger (deposits + withdrawals) bitcoinde
480
+ # @return [String] valores formatados del (ledger)
481
+ def del_val(hlx)
482
+ tip = hlx[:tp]
483
+ "(#{hlx[:txid]}," \
484
+ "DATETIME(TIMESTAMP('#{hlx[:time].iso8601}'))," \
485
+ "'#{tip}'," \
486
+ "'#{hlx[:add]}'," \
487
+ "'#{hlx[:moe]}'," \
488
+ "cast(#{tip == 'withdrawal' ? '-' : ''}#{hlx[:qt]} as numeric)," \
489
+ "cast(#{hlx[:fee]} as numeric))"
490
+ end
491
+
492
+ # @example (see Apice#trades_us)
493
+ # @param [String] idx identificador transacao
494
+ # @param [Hash] htx trade kraken
495
+ # @return [String] valores formatados ust (trades parte1)
496
+ def ust_1val(idx, htx)
497
+ "('#{idx}'," \
498
+ "'#{htx[:ordertxid]}'," \
499
+ "'#{htx[:pair]}'," \
500
+ "PARSE_DATETIME('%s', '#{String(htx[:time].round)}')," \
501
+ "'#{htx[:type]}'," \
502
+ "'#{htx[:ordertype]}'," \
503
+ "cast(#{htx[:price]} as numeric)," \
504
+ "cast(#{htx[:cost]} as numeric)," \
505
+ "cast(#{htx[:fee]} as numeric)," \
506
+ "#{ust_2val(idx, htx)}"
507
+ end
508
+
509
+ # @param (see ust_1val)
510
+ # @return [String] valores formatados ust (trades parte2)
511
+ def ust_2val(idx, htx)
512
+ msc = htx[:misc].to_s
513
+ "cast(#{htx[:vol]} as numeric)," \
514
+ "cast(#{htx[:margin]} as numeric)," \
515
+ "#{msc.empty? ? 'null' : "'#{msc}'"}," \
516
+ "'#{apius.ledger.select { |_, val| val[:refid] == idx }.keys.join(',') || ''}'," \
517
+ "#{Integer(ops[:h][idx] || 0)})"
518
+ end
519
+
520
+ # @example (see Apice#ledger_us)
521
+ # @param idx (see ust_1val)
522
+ # @param [Hash] hlx ledger kraken
523
+ # @return [String] valores formatados usl (ledger)
524
+ def usl_val(idx, hlx)
525
+ acl = hlx[:aclass].to_s
526
+ "('#{idx}'," \
527
+ "'#{hlx[:refid]}'," \
528
+ "PARSE_DATETIME('%s', '#{String(hlx[:time].round)}')," \
529
+ "'#{hlx[:type]}'," \
530
+ "#{acl.empty? ? 'null' : "'#{acl}'"}," \
531
+ "'#{hlx[:asset]}'," \
532
+ "cast(#{hlx[:amount]} as numeric)," \
533
+ "cast(#{hlx[:fee]} as numeric))"
534
+ end
535
+
536
+ # @example (see Apice#ledger_fr)
537
+ # @param [Hash] hlx ledger paymium
538
+ # @return [String] valores formatados frl (ledger)
539
+ def frl_val(hlx)
540
+ uid = hlx[:uuid]
541
+ "('#{uid}'," \
542
+ "'#{hlx[:name]}'," \
543
+ "cast(#{hlx[:amount]} as numeric)," \
544
+ "'#{hlx[:currency]}'," \
545
+ "PARSE_DATETIME('%s', '#{hlx[:created_at_int]}')," \
546
+ "#{Integer(ops[:h][uid] || 0)})"
547
+ end
548
+
549
+ # @example (see Apice#ledger_mt)
550
+ # @param [Hash] hlx ledger therock
551
+ # @return [String] valores formatados mtl (ledger parte1)
552
+ def mtl_1val(hlx)
553
+ fid = hlx[:fund_id].to_s
554
+ "(#{hlx[:id]}," \
555
+ "DATETIME(TIMESTAMP('#{hlx[:date]}'))," \
556
+ "'#{hlx[:type]}'," \
557
+ "cast(#{hlx[:price]} as numeric)," \
558
+ "'#{hlx[:currency]}'," \
559
+ "#{fid.empty? ? 'null' : "'#{fid}'"}," \
560
+ "#{mtl_2val(hlx)}"
561
+ end
562
+
563
+ # @param (see mtl_1val)
564
+ # @return [String] valores formatados mtl (ledger parte2)
565
+ def mtl_2val(hlx)
566
+ nte = hlx[:note].to_s
567
+ tid = hlx[:trade_id].to_s
568
+ "#{nte.empty? ? 'null' : "'#{nte}'"}," \
569
+ "#{tid.empty? ? 'null' : tid.to_s}," \
570
+ "#{Integer(ops[:h][String(hlx[:id])] || 0)})"
571
+ end
572
+
573
+ # def eth2at_ins
574
+ # "insert #{BD}.eth2at(attesterslot,committeeindex,epoch,inclusionslot,status,validatorindex" \
575
+ # ") VALUES#{apibc.novtx.map { |obj| eth2at_1val(obj) }.join(',')}"
576
+ # end
577
+ # def eth2pr_ins
578
+ # "insert #{BD}.eth2pr(attestationscount,attesterslashingscount,blockroot,depositscount,epoch," \
579
+ # 'eth1data_blockhash,eth1data_depositcount,eth1data_depositroot,graffiti,graffiti_text,parentroot,' \
580
+ # 'proposer,proposerslashingscount,randaoreveal,signature,slot,stateroot,status,voluntaryexitscount' \
581
+ # ") VALUES#{apibc.novkx.map { |obj| eth2pr_1val(obj) }.join(',')}"
582
+ # end
583
+ # def eth2at_1val(htx)
584
+ # "(#{Integer(htx[:attesterslot])}," \
585
+ # "#{Integer(htx[:committeeindex])}," \
586
+ # "#{Integer(htx[:epoch])}," \
587
+ # "#{Integer(htx[:inclusionslot])}," \
588
+ # "#{Integer(htx[:status])}," \
589
+ # "#{Integer(htx[:validatorindex])})"
590
+ # end
591
+ # def eth2pr_1val(htx)
592
+ # "(#{Integer(htx[:attestationscount])}," \
593
+ # "#{Integer(htx[:attesterslashingscount])}," \
594
+ # "'#{htx[:blockroot]}'," \
595
+ # "#{Integer(htx[:depositscount])}," \
596
+ # "#{Integer(htx[:epoch])}," \
597
+ # "'#{htx[:eth1data_blockhash]}'," \
598
+ # "#{eth2pr_2val(htx)}"
599
+ # end
600
+ # def eth2pr_2val(htx)
601
+ # grf = htx[:graffiti_text]
602
+ # "#{Integer(htx[:eth1data_depositcount])}," \
603
+ # "'#{htx[:eth1data_depositroot]}'," \
604
+ # "'#{htx[:graffiti]}'," \
605
+ # "#{grf.length.zero? ? 'null' : "'#{grf}'"}," \
606
+ # "'#{htx[:parentroot]}'," \
607
+ # "#{Integer(htx[:proposer])}," \
608
+ # "#{eth2pr_3val(htx)}"
609
+ # end
610
+ # def eth2pr_3val(htx)
611
+ # "#{Integer(htx[:proposerslashingscount])}," \
612
+ # "'#{htx[:randaoreveal]}'," \
613
+ # "'#{htx[:signature]}'," \
614
+ # "#{Integer(htx[:slot])}," \
615
+ # "'#{htx[:stateroot]}'," \
616
+ # "#{Integer(htx[:status])}," \
617
+ # "#{Integer(htx[:voluntaryexitscount])})"
618
+ # end
619
+ end
620
+ end