cns 0.3.5 → 0.3.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '07580f6979525f8cc820f784769c92c439a5609cd167af622ff03325bd889f5a'
4
- data.tar.gz: f6038962e0dae1af567bccbcc50cc9eb82fa43f0520d431bad6020fec6d02028
3
+ metadata.gz: 598e00d798ff5697dfcb400b4698b364b4278cca4814e5c55c61509116c28807
4
+ data.tar.gz: 35d2b7241b9f7573f2c5ae382eb19aea9a08f6200e12f22183774faab2cae33e
5
5
  SHA512:
6
- metadata.gz: ae31f87a851c5e930a4bb516dbee3c69ede3dccd9772cbb96d9ad63d6ac1ce4428a61839cb3275fc9f59db2f31777cfad7ced54bfc669d50a807ce02a2072ddd
7
- data.tar.gz: dcb4d565c017bb425895f4ba32ad8e8644f6cbbb61a187ea3622bfcc4ed709e7a6f5cc24b96de9a442cd817e6093b7f4cc4518d1c98b9858bd96acddeb62e9f4
6
+ metadata.gz: b0a128f371ba2e8eacbf987a1c2826fef5ac2530592b85bba04409bec35433915d257f59a8b37fd303176047bdb034a07459c12877cbc91463cd1371b2e106de
7
+ data.tar.gz: bd063a649dd9d275a916ffc6ffaaa44ed4008a73a64e63d6fe67b9bf9a4a9d9c6d1c1c1c28a2f060b630ec7cc63bd4d9cc5bfb2aeca2b955affc7e08d2ec2df5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cns (0.3.5)
4
+ cns (0.3.7)
5
5
  curb
6
6
  faraday
7
7
  google-cloud-bigquery
@@ -71,7 +71,7 @@ GEM
71
71
  parser (2.7.2.0)
72
72
  ast (~> 2.4.1)
73
73
  psych (3.3.4)
74
- public_suffix (5.0.1)
74
+ public_suffix (5.0.3)
75
75
  racc (1.7.1)
76
76
  rainbow (3.1.1)
77
77
  rake (12.3.3)
@@ -7,8 +7,109 @@ require('json')
7
7
 
8
8
  # @author Hernani Rodrigues Vaz
9
9
  module Cns
10
+ DC = %w[LTC NMC PPC DOGE XRP Linden USD CAD GBP ZEC BCH EURN NOKU FDZ GUSD SEED USDC].freeze
11
+
10
12
  # classe para acesso dados centralized exchanges
11
13
  class Apice
14
+ # @example account_de
15
+ # {
16
+ # data: {
17
+ # balances: {
18
+ # btc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
19
+ # bch: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
20
+ # btg: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
21
+ # eth: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
22
+ # bsv: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
23
+ # ltc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' }
24
+ # },
25
+ # encrypted_information: { uid: '0y...', bic_short: '0y...', bic_full: '0y...' }
26
+ # },
27
+ # errors: [],
28
+ # credits: 23
29
+ # }
30
+ # @param [String] uri Uniform Resource Identifier do pedido HTTP
31
+ # @return [Hash] saldos no bitcoinde
32
+ def account_de(uri = 'https://api.bitcoin.de/v4/account')
33
+ JSON.parse(
34
+ Curl.get(uri) { |obj| obj.headers = hde(uri) }.body,
35
+ symbolize_names: true
36
+ )[:data][:balances]
37
+ rescue StandardError
38
+ {}
39
+ end
40
+
41
+ # @example account_fr
42
+ # {
43
+ # name: '...',
44
+ # email: '...',
45
+ # locale: 'en',
46
+ # channel_id: '...',
47
+ # meta_state: 'approved',
48
+ # balance_eur: '0.0',
49
+ # locked_eur: '0.0',
50
+ # balance_btc: '0.0',
51
+ # locked_btc: '0.0',
52
+ # balance_lbtc: '0.0',
53
+ # locked_lbtc: '0.0'
54
+ # }
55
+ # @param (see account_de)
56
+ # @return [Hash] saldos no paymium
57
+ def account_fr(uri = 'https://paymium.com/api/v1/user')
58
+ JSON.parse(
59
+ Curl.get(uri) { |obj| obj.headers = hfr(uri) }.body,
60
+ symbolize_names: true
61
+ )
62
+ rescue StandardError
63
+ {}
64
+ end
65
+
66
+ # @example account_mt
67
+ # {
68
+ # balances: [
69
+ # { currency: 'BTC', balance: 0.0, trading_balance: 0.0 },
70
+ # { currency: 'ETH', balance: 0.0, trading_balance: 0.0 },
71
+ # { currency: 'EUR', balance: 0.0, trading_balance: 0.0 },
72
+ # { currency: 'DAI', balance: 0.0, trading_balance: 0.0 },
73
+ # ]
74
+ # }
75
+ # @param (see account_de)
76
+ # @return [Array<Hash>] lista saldos no therock
77
+ def account_mt(uri = 'https://api.therocktrading.com/v1/balances')
78
+ JSON.parse(
79
+ Curl.get(uri) { |obj| obj.headers = hmt(uri) }.body,
80
+ symbolize_names: true
81
+ )[:balances]
82
+ .delete_if { |del| DC.include?(del[:currency]) }
83
+ .sort { |oba, obb| oba[:currency] <=> obb[:currency] }
84
+ rescue StandardError
85
+ []
86
+ end
87
+
88
+ # @example account_us
89
+ # {
90
+ # error: [],
91
+ # result: {
92
+ # ZEUR: '0.0038',
93
+ # XXBT: '0.0000000000',
94
+ # XETH: '1.0000000000',
95
+ # XETC: '0.0000000000',
96
+ # EOS: '0.0000001700',
97
+ # BCH: '0.0000000000'
98
+ # }
99
+ # }
100
+ # @param [String] urb Uniform Resource Base do pedido HTTP
101
+ # @param uri (see account_de)
102
+ # @param non (see hde)
103
+ # @return [Hash] saldos no kraken
104
+ def account_us(urb = 'https://api.kraken.com/0/private', uri = 'Balance', non = nnc)
105
+ JSON.parse(
106
+ Curl.post("#{urb}/#{uri}", nonce: non) { |obj| obj.headers = hus(uri, nonce: non) }.body,
107
+ symbolize_names: true
108
+ )[:result]
109
+ rescue StandardError
110
+ {}
111
+ end
112
+
12
113
  # @example trades_de
13
114
  # {
14
115
  # trades: [{
@@ -279,7 +380,8 @@ module Cns
279
380
  symbolize_names: true
280
381
  )[:result]
281
382
  has.merge!(res[:trades])
282
- (ofs += 50) < res[:count] ? trades_us(ofs, has) : has
383
+ sleep 2
384
+ res[:trades].count > 0 ? trades_us(ofs + res[:trades].count, has) : has
283
385
  rescue StandardError
284
386
  has
285
387
  end
@@ -315,9 +417,74 @@ module Cns
315
417
  symbolize_names: true
316
418
  )[:result]
317
419
  has.merge!(res[:ledger])
318
- (ofs += 50) < res[:count] ? ledger_us(ofs, has) : has
420
+ sleep 2
421
+ res[:ledger].count > 0 ? ledger_us(ofs + res[:ledger].count, has) : has
319
422
  rescue StandardError
320
423
  has
321
424
  end
425
+
426
+ private
427
+
428
+ # @return [Integer] continually-increasing unsigned integer nonce from the current Unix Time
429
+ def nnc
430
+ Integer(Float(Time.now) * 1e6)
431
+ end
432
+
433
+ # @param [String] qde query a incluir no pedido HTTP
434
+ # @param [Integer] non continually-increasing unsigned integer
435
+ # @return [Hash] headers necessarios para pedido HTTP da exchange bitcoinde
436
+ def hde(qde, non = nnc)
437
+ key = ENV['BITCOINDE_API_KEY']
438
+ {
439
+ 'X-API-KEY': key,
440
+ 'X-API-NONCE': non,
441
+ 'X-API-SIGNATURE': OpenSSL::HMAC.hexdigest(
442
+ 'sha256',
443
+ ENV['BITCOINDE_API_SECRET'],
444
+ ['GET', qde, key, non, Digest::MD5.hexdigest('')].join('#')
445
+ )
446
+ }
447
+ end
448
+
449
+ # @param [String] qfr query a incluir no pedido HTTP
450
+ # @param non (see hde)
451
+ # @return [Hash] headers necessarios para pedido HTTP da exchange paymium
452
+ def hfr(qfr, non = nnc)
453
+ {
454
+ content_type: 'application/json',
455
+ 'Api-Key': ENV['PAYMIUM_API_KEY'],
456
+ 'Api-Nonce': non,
457
+ 'Api-Signature': OpenSSL::HMAC.hexdigest('sha256', ENV['PAYMIUM_API_SECRET'], [non, qfr].join)
458
+ }
459
+ end
460
+
461
+ # @param [String] qmt query a incluir no pedido HTTP
462
+ # @param non (see hde)
463
+ # @return [Hash] headers necessarios para pedido HTTP da exchange therock
464
+ def hmt(qmt, non = nnc)
465
+ {
466
+ content_type: 'application/json',
467
+ 'X-TRT-KEY': ENV['THEROCK_API_KEY'],
468
+ 'X-TRT-NONCE': non,
469
+ 'X-TRT-SIGN': OpenSSL::HMAC.hexdigest('sha512', ENV['THEROCK_API_SECRET'], [non, qmt].join)
470
+ }
471
+ end
472
+
473
+ # @param [String] qus query a incluir no pedido HTTP
474
+ # @param [Hash] ops opcoes trabalho
475
+ # @option ops [Hash] :nonce continually-increasing unsigned integer
476
+ # @return [Hash] headers necessarios para pedido HTTP da exchange kraken
477
+ def hus(qus, ops)
478
+ {
479
+ 'api-key': ENV['KRAKEN_API_KEY'],
480
+ 'api-sign': Base64.strict_encode64(
481
+ OpenSSL::HMAC.digest(
482
+ 'sha512',
483
+ Base64.decode64(ENV['KRAKEN_API_SECRET']),
484
+ ['/0/private/', qus, Digest::SHA256.digest("#{ops[:nonce]}#{URI.encode_www_form(ops)}")].join
485
+ )
486
+ )
487
+ }
488
+ end
322
489
  end
323
490
  end
@@ -4,7 +4,7 @@ require('bigdecimal/util')
4
4
 
5
5
  # @author Hernani Rodrigues Vaz
6
6
  module Cns
7
- # (see Beaconchain)
7
+ # classe para processar historicos da beaconchain
8
8
  class Beaconchain
9
9
  # @return [Apibc] API blockchains
10
10
  attr_reader :api
@@ -98,5 +98,88 @@ module Cns
98
98
  eb: abc[:bx]
99
99
  }
100
100
  end
101
+
102
+ # @return [String] texto validadores & saldos historicos
103
+ def mostra_resumo
104
+ return unless dados.count.positive?
105
+
106
+ puts("\nindex address beaconchain blh bigquery blh")
107
+ dados.each { |obj| puts(formata_validador(obj)) }
108
+ mostra_saldos
109
+ end
110
+
111
+ # @param [Hash] hjn dados juntos bigquery & beaconchain
112
+ # @return [String] texto formatado dum validador
113
+ def formata_validador(hjn)
114
+ format('%<s1>-5.5s %<s2>-34.34s ', s1: hjn[:id], s2: formata_endereco(hjn[:ax], 34)) + formata_valores(hjn)
115
+ end
116
+
117
+ # @param (see formata_validador)
118
+ # @return [String] texto formatado valores dum validador
119
+ def formata_valores(hjn)
120
+ format(
121
+ '%<v1>11.6f %<n1>3i %<v2>12.6f %<n2>6i %<ok>-3s',
122
+ v1: hjn[:es],
123
+ n1: hjn[:eb].count,
124
+ v2: hjn[:bs],
125
+ n2: hjn[:bb].count,
126
+ ok: ok?(hjn) ? 'OK' : 'NOK'
127
+ )
128
+ end
129
+
130
+ # @param (see formata_validador)
131
+ # @return [Boolean] validador tem historicos novos(sim=NOK, nao=OK)?
132
+ def ok?(hjn)
133
+ hjn[:bs] == hjn[:es]
134
+ end
135
+
136
+ # @example pubkey inicio..fim
137
+ # 0x10f3a0cf0b534c..c033cf32e8a03586
138
+ # @param [String] add chave publica validador
139
+ # @param [Integer] max chars a mostrar
140
+ # @return [String] pubkey formatada
141
+ def formata_endereco(add, max)
142
+ return 'erro' if max < 7
143
+
144
+ max -= 2
145
+ ini = Integer(max / 2)
146
+ inf = max % 2
147
+ "#{add[0, ini - 3]}..#{add[-inf - ini - 3..]}"
148
+ end
149
+
150
+ # @example
151
+ # {
152
+ # balance: 32_489_497_108,
153
+ # effectivebalance: 32_000_000_000,
154
+ # epoch: 8296,
155
+ # validatorindex: 11_766,
156
+ # week: 5
157
+ # }
158
+ # @param [Hash] hbh historico beaconchain
159
+ # @return [String] texto formatado historico beaconchain
160
+ def formata_saldos(hbh)
161
+ idx = hbh[:validatorindex]
162
+ epc = hbh[:epoch]
163
+ format(
164
+ '%<vi>5i %<vl>17.6f %<ep>6i %<id>9i',
165
+ vi: idx,
166
+ vl: (hbh[:balance].to_d / (10**9)).round(10),
167
+ ep: epc,
168
+ id: itx(epc, idx)
169
+ )
170
+ end
171
+
172
+ # @return [String] texto historico saldos
173
+ def mostra_saldos
174
+ return unless ops[:v] && nov.count.positive?
175
+
176
+ puts("\nindex saldo epoch itx")
177
+ sorbx.each { |obj| puts(formata_saldos(obj)) }
178
+ end
179
+
180
+ # @return [Array<Hash>] lista ordenada historico saldos
181
+ def sorbx
182
+ nov.sort { |ant, prx| ant[:itx] <=> prx[:itx] }
183
+ end
101
184
  end
102
185
  end