cns 0.1.4 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,7 +31,7 @@ module Cns
31
31
  # @return [Hash] saldos no bitcoinde
32
32
  def account_de(uri = 'https://api.bitcoin.de/v4/account')
33
33
  JSON.parse(
34
- Curl.get(uri) { |r| r.headers = hde(uri) }.body,
34
+ Curl.get(uri) { |obj| obj.headers = hde(uri) }.body,
35
35
  symbolize_names: true
36
36
  )[:data][:balances]
37
37
  rescue StandardError
@@ -56,7 +56,7 @@ module Cns
56
56
  # @return [Hash] saldos no paymium
57
57
  def account_fr(uri = 'https://paymium.com/api/v1/user')
58
58
  JSON.parse(
59
- Curl.get(uri) { |h| h.headers = hfr(uri) }.body,
59
+ Curl.get(uri) { |obj| obj.headers = hfr(uri) }.body,
60
60
  symbolize_names: true
61
61
  )
62
62
  rescue StandardError
@@ -76,11 +76,11 @@ module Cns
76
76
  # @return [Array<Hash>] lista saldos no therock
77
77
  def account_mt(uri = 'https://api.therocktrading.com/v1/balances')
78
78
  JSON.parse(
79
- Curl.get(uri) { |h| h.headers = hmt(uri) }.body,
79
+ Curl.get(uri) { |obj| obj.headers = hmt(uri) }.body,
80
80
  symbolize_names: true
81
81
  )[:balances]
82
- .delete_if { |e| DC.include?(e[:currency]) }
83
- .sort { |a, b| a[:currency] <=> b[:currency] }
82
+ .delete_if { |del| DC.include?(del[:currency]) }
83
+ .sort { |oba, obb| oba[:currency] <=> obb[:currency] }
84
84
  rescue StandardError
85
85
  []
86
86
  end
@@ -103,7 +103,7 @@ module Cns
103
103
  # @return [Hash] saldos no kraken
104
104
  def account_us(urb = 'https://api.kraken.com/0/private', uri = 'Balance', non = nnc)
105
105
  JSON.parse(
106
- Curl.post("#{urb}/#{uri}", nonce: non) { |h| h.headers = hus(uri, nonce: non) }.body,
106
+ Curl.post("#{urb}/#{uri}", nonce: non) { |obj| obj.headers = hus(uri, nonce: non) }.body,
107
107
  symbolize_names: true
108
108
  )[:result]
109
109
  rescue StandardError
@@ -112,113 +112,63 @@ module Cns
112
112
 
113
113
  private
114
114
 
115
- # @example deposits_unif_de
116
- # [
117
- # {
118
- # txid: 177_245,
119
- # time: '2014-01-31T22:01:30+01:00',
120
- # tp: 'deposit',
121
- # add: '1KK6HhG3quojFS4CY1mPcbyrjQ8BMDQxmT',
122
- # qt: '0.13283',
123
- # moe: 'btc',
124
- # fee: '0'
125
- # },
126
- # {}
127
- # ]
128
- # @param [Hash] hde pagina depositos bitcoinde
129
- # @return [Array<Hash>] lista uniformizada pagina depositos bitcoinde
130
- def deposits_unif_de(hde)
131
- hde[:deposits].map do |h|
132
- {
133
- add: h[:address],
134
- time: Time.parse(h[:created_at]),
135
- qt: h[:amount],
136
- txid: Integer(h[:deposit_id])
137
- }.merge(tp: 'deposit', moe: 'btc', fee: '0')
138
- end
139
- end
140
-
141
- # @example withdrawals_unif_de
142
- # [
143
- # {
144
- # txid: 136_605,
145
- # time: '2014-02-05T13:05:17+01:00',
146
- # tp: 'withdrawal',
147
- # add: '1K9YMDDrmMV25EoYNqi7KUEK57Kn3TCNUJ',
148
- # qt: '0.120087',
149
- # fee: '0',
150
- # moe: 'btc'
151
- # },
152
- # {}
153
- # ]
154
- # @param [Hash] hwi pagina withdrawals bitcoinde
155
- # @return [Array<Hash>] lista uniformizada pagina withdrawals bitcoinde
156
- def withdrawals_unif_de(hwi)
157
- hwi[:withdrawals].map do |h|
158
- {
159
- add: h[:address],
160
- time: Time.parse(h[:transferred_at]),
161
- qt: h[:amount],
162
- fee: h[:network_fee],
163
- txid: Integer(h[:withdrawal_id])
164
- }.merge(tp: 'withdrawal', moe: 'btc')
165
- end
166
- end
167
-
168
115
  # @return [Integer] continually-increasing unsigned integer nonce from the current Unix Time
169
116
  def nnc
170
117
  Integer(Float(Time.now) * 1e6)
171
118
  end
172
119
 
173
- # @param [String] qry query a incluir no pedido HTTP
120
+ # @param [String] qde query a incluir no pedido HTTP
174
121
  # @param [Integer] non continually-increasing unsigned integer
175
122
  # @return [Hash] headers necessarios para pedido HTTP da exchange bitcoinde
176
- def hde(qry, non = nnc)
123
+ def hde(qde, non = nnc)
124
+ key = ENV['BITCOINDE_API_KEY']
177
125
  {
178
- 'X-API-KEY': ENV['BITCOINDE_API_KEY'],
126
+ 'X-API-KEY': key,
179
127
  'X-API-NONCE': non,
180
128
  'X-API-SIGNATURE': OpenSSL::HMAC.hexdigest(
181
129
  'sha256',
182
130
  ENV['BITCOINDE_API_SECRET'],
183
- ['GET', qry, ENV['BITCOINDE_API_KEY'], non, Digest::MD5.hexdigest('')].join('#')
131
+ ['GET', qde, key, non, Digest::MD5.hexdigest('')].join('#')
184
132
  )
185
133
  }
186
134
  end
187
135
 
188
- # @param (see hde)
136
+ # @param [String] qfr query a incluir no pedido HTTP
137
+ # @param non (see hde)
189
138
  # @return [Hash] headers necessarios para pedido HTTP da exchange paymium
190
- def hfr(qry, non = nnc)
139
+ def hfr(qfr, non = nnc)
191
140
  {
192
141
  content_type: 'application/json',
193
142
  'Api-Key': ENV['PAYMIUM_API_KEY'],
194
143
  'Api-Nonce': non,
195
- 'Api-Signature': OpenSSL::HMAC.hexdigest('sha256', ENV['PAYMIUM_API_SECRET'], [non, qry].join)
144
+ 'Api-Signature': OpenSSL::HMAC.hexdigest('sha256', ENV['PAYMIUM_API_SECRET'], [non, qfr].join)
196
145
  }
197
146
  end
198
147
 
199
- # @param (see hde)
148
+ # @param [String] qmt query a incluir no pedido HTTP
149
+ # @param non (see hde)
200
150
  # @return [Hash] headers necessarios para pedido HTTP da exchange therock
201
- def hmt(qry, non = nnc)
151
+ def hmt(qmt, non = nnc)
202
152
  {
203
153
  content_type: 'application/json',
204
154
  'X-TRT-KEY': ENV['THEROCK_API_KEY'],
205
155
  'X-TRT-NONCE': non,
206
- 'X-TRT-SIGN': OpenSSL::HMAC.hexdigest('sha512', ENV['THEROCK_API_SECRET'], [non, qry].join)
156
+ 'X-TRT-SIGN': OpenSSL::HMAC.hexdigest('sha512', ENV['THEROCK_API_SECRET'], [non, qmt].join)
207
157
  }
208
158
  end
209
159
 
210
- # @param qry (see hde)
160
+ # @param [String] qus query a incluir no pedido HTTP
211
161
  # @param [Hash] ops opcoes trabalho
212
162
  # @option ops [Hash] :nonce continually-increasing unsigned integer
213
163
  # @return [Hash] headers necessarios para pedido HTTP da exchange kraken
214
- def hus(qry, ops)
164
+ def hus(qus, ops)
215
165
  {
216
166
  'api-key': ENV['KRAKEN_API_KEY'],
217
167
  'api-sign': Base64.strict_encode64(
218
168
  OpenSSL::HMAC.digest(
219
169
  'sha512',
220
170
  Base64.decode64(ENV['KRAKEN_API_SECRET']),
221
- ['/0/private/', qry, Digest::SHA256.digest("#{ops[:nonce]}#{URI.encode_www_form(ops)}")].join
171
+ ['/0/private/', qus, Digest::SHA256.digest("#{ops[:nonce]}#{URI.encode_www_form(ops)}")].join
222
172
  )
223
173
  )
224
174
  }
@@ -50,13 +50,11 @@ module Cns
50
50
  # @param [String] uri Uniform Resource Identifier do pedido HTTP
51
51
  # @return [Array<Hash>] lista completa trades bitcoinde
52
52
  def trades_de(pag = 0, ary = [], uri = 'https://api.bitcoin.de/v4/trades')
53
- p = "#{uri}?#{URI.encode_www_form(state: 1, page: pag + 1)}"
54
- r = JSON.parse(
55
- Curl.get(p) { |h| h.headers = hde(p) }.body,
56
- symbolize_names: true
57
- )
58
- ary += r[:trades]
59
- r[:page][:current] < r[:page][:last] ? trades_de(pag + 1, ary) : ary
53
+ par = "#{uri}?#{URI.encode_www_form(state: 1, page: pag += 1)}"
54
+ res = JSON.parse(Curl.get(par) { |obj| obj.headers = hde(par) }.body, symbolize_names: true)
55
+ ary += res[:trades]
56
+ rep = res[:page]
57
+ rep[:current] < rep[:last] ? trades_de(pag, ary) : ary
60
58
  rescue StandardError
61
59
  ary
62
60
  end
@@ -82,17 +80,38 @@ module Cns
82
80
  # @param (see trades_de)
83
81
  # @return [Array<Hash>] lista completa uniformizada depositos bitcoinde
84
82
  def deposits_de(pag = 0, ary = [], uri = 'https://api.bitcoin.de/v4/btc/deposits')
85
- p = "#{uri}?#{URI.encode_www_form(state: 2, page: pag + 1)}"
86
- r = JSON.parse(
87
- Curl.get(p) { |h| h.headers = hde(p) }.body,
88
- symbolize_names: true
89
- )
90
- ary += deposits_unif_de(r)
91
- r[:page][:current] < r[:page][:last] ? deposits_de(pag + 1, ary) : ary
83
+ par = "#{uri}?#{URI.encode_www_form(state: 2, page: pag += 1)}"
84
+ res = JSON.parse(Curl.get(par) { |obj| obj.headers = hde(par) }.body, symbolize_names: true)
85
+ ary += res[:deposits].map { |has| deposit_unif(has) }
86
+ rep = res[:page]
87
+ rep[:current] < rep[:last] ? deposits_de(pag, ary) : ary
92
88
  rescue StandardError
93
89
  ary
94
90
  end
95
91
 
92
+ # @example deposit_unif
93
+ # [
94
+ # {
95
+ # txid: 177_245,
96
+ # time: '2014-01-31T22:01:30+01:00',
97
+ # tp: 'deposit',
98
+ # add: '1KK6HhG3quojFS4CY1mPcbyrjQ8BMDQxmT',
99
+ # qt: '0.13283',
100
+ # moe: 'btc',
101
+ # fee: '0'
102
+ # },
103
+ # {}
104
+ # ]
105
+ # @return [Hash] deposit uniformizado bitcoinde
106
+ def deposit_unif(has)
107
+ {
108
+ add: has[:address],
109
+ time: Time.parse(has[:created_at]),
110
+ qt: has[:amount],
111
+ txid: Integer(has[:deposit_id])
112
+ }.merge(tp: 'deposit', moe: 'btc', fee: '0')
113
+ end
114
+
96
115
  # @example withdrawals_de
97
116
  # {
98
117
  # withdrawals: [
@@ -116,17 +135,39 @@ module Cns
116
135
  # @param (see deposits_de)
117
136
  # @return [Array<Hash>] lista completa uniformizada withdrawals bitcoinde
118
137
  def withdrawals_de(pag = 0, ary = [], uri = 'https://api.bitcoin.de/v4/btc/withdrawals')
119
- p = "#{uri}?#{URI.encode_www_form(state: 1, page: pag + 1)}"
120
- r = JSON.parse(
121
- Curl.get(p) { |h| h.headers = hde(p) }.body,
122
- symbolize_names: true
123
- )
124
- ary += withdrawals_unif_de(r)
125
- r[:page][:current] < r[:page][:last] ? withdrawals_de(pag + 1, ary) : ary
138
+ par = "#{uri}?#{URI.encode_www_form(state: 1, page: pag += 1)}"
139
+ res = JSON.parse(Curl.get(par) { |obj| obj.headers = hde(par) }.body, symbolize_names: true)
140
+ ary += res[:withdrawals].map { |has| withdrawal_unif(has) }
141
+ rep = res[:page]
142
+ rep[:current] < rep[:last] ? withdrawals_de(pag, ary) : ary
126
143
  rescue StandardError
127
144
  ary
128
145
  end
129
146
 
147
+ # @example withdrawal_unif
148
+ # [
149
+ # {
150
+ # txid: 136_605,
151
+ # time: '2014-02-05T13:05:17+01:00',
152
+ # tp: 'withdrawal',
153
+ # add: '1K9YMDDrmMV25EoYNqi7KUEK57Kn3TCNUJ',
154
+ # qt: '0.120087',
155
+ # fee: '0',
156
+ # moe: 'btc'
157
+ # },
158
+ # {}
159
+ # ]
160
+ # @return [Hash] withdrawal uniformizada bitcoinde
161
+ def withdrawal_unif(has)
162
+ {
163
+ add: has[:address],
164
+ time: Time.parse(has[:transferred_at]),
165
+ qt: has[:amount],
166
+ fee: has[:network_fee],
167
+ txid: Integer(has[:withdrawal_id])
168
+ }.merge(tp: 'withdrawal', moe: 'btc')
169
+ end
170
+
130
171
  # @example ledger_fr
131
172
  # [
132
173
  # {
@@ -156,11 +197,11 @@ module Cns
156
197
  # @param (see trades_de)
157
198
  # @return [Array<Hash>] lista ledger paymium
158
199
  def ledger_fr(pag = 0, ary = [], uri = 'https://paymium.com/api/v1/user/orders')
159
- r = JSON.parse(
160
- Curl.get(uri, offset: pag) { |h| h.headers = hfr("#{uri}?#{URI.encode_www_form(offset: pag)}") }.body,
200
+ res = JSON.parse(
201
+ Curl.get(uri, offset: pag) { |obj| obj.headers = hfr("#{uri}?#{URI.encode_www_form(offset: pag)}") }.body,
161
202
  symbolize_names: true
162
203
  )
163
- r.empty? ? ary : ledger_fr(pag + r.size, ary + r)
204
+ res.empty? ? ary : ledger_fr(pag + res.size, ary + res)
164
205
  rescue StandardError
165
206
  ary
166
207
  end
@@ -194,11 +235,11 @@ module Cns
194
235
  # @param (see trades_de)
195
236
  # @return [Array<Hash>] lista ledger therock
196
237
  def ledger_mt(pag = 1, ary = [], uri = 'https://api.therocktrading.com/v1/transactions')
197
- r = JSON.parse(
198
- Curl.get(uri, page: pag) { |h| h.headers = hmt("#{uri}?#{URI.encode_www_form(page: pag)}") }.body,
238
+ res = JSON.parse(
239
+ Curl.get(uri, page: pag) { |obj| obj.headers = hmt("#{uri}?#{URI.encode_www_form(page: pag)}") }.body,
199
240
  symbolize_names: true
200
241
  )[:transactions]
201
- r.empty? ? ary : ledger_mt(pag + r.size, ary + r)
242
+ res.empty? ? ary : ledger_mt(pag + res.size, ary + res)
202
243
  rescue StandardError
203
244
  ary
204
245
  end
@@ -230,14 +271,15 @@ module Cns
230
271
  # @param [Hash] has acumulador dos dados
231
272
  # @param (see account_us)
232
273
  # @return [Hash] dados trades kraken
233
- def trades_us(ofs = 0, has = {}, urb = 'https://api.kraken.com/0/private', uri = 'TradesHistory', non = nnc)
234
- r = JSON.parse(
235
- Curl.post("#{urb}/#{uri}", nonce: non, ofs: ofs) { |h| h.headers = hus(uri, nonce: non, ofs: ofs) }.body,
274
+ def trades_us(ofs = 0, has = {}, urb = 'https://api.kraken.com/0/private')
275
+ uri = 'TradesHistory'
276
+ non = nnc
277
+ res = JSON.parse(
278
+ Curl.post("#{urb}/#{uri}", nonce: non, ofs: ofs) { |obj| obj.headers = hus(uri, nonce: non, ofs: ofs) }.body,
236
279
  symbolize_names: true
237
280
  )[:result]
238
- has.merge!(r[:trades])
239
- ofs += 50
240
- ofs < r[:count] ? trades_us(ofs, has) : has
281
+ has.merge!(res[:trades])
282
+ (ofs += 50) < res[:count] ? trades_us(ofs, has) : has
241
283
  rescue StandardError
242
284
  has
243
285
  end
@@ -265,14 +307,15 @@ module Cns
265
307
  # }
266
308
  # @param (see trades_us)
267
309
  # @return [Hash] dados ledger kraken
268
- def ledger_us(ofs = 0, has = {}, urb = 'https://api.kraken.com/0/private', uri = 'Ledgers', non = nnc)
269
- r = JSON.parse(
270
- Curl.post("#{urb}/#{uri}", nonce: non, ofs: ofs) { |h| h.headers = hus(uri, nonce: non, ofs: ofs) }.body,
310
+ def ledger_us(ofs = 0, has = {}, urb = 'https://api.kraken.com/0/private')
311
+ uri = 'Ledgers'
312
+ non = nnc
313
+ res = JSON.parse(
314
+ Curl.post("#{urb}/#{uri}", nonce: non, ofs: ofs) { |obj| obj.headers = hus(uri, nonce: non, ofs: ofs) }.body,
271
315
  symbolize_names: true
272
316
  )[:result]
273
- has.merge!(r[:ledger])
274
- ofs += 50
275
- ofs < r[:count] ? ledger_us(ofs, has) : has
317
+ has.merge!(res[:ledger])
318
+ (ofs += 50) < res[:count] ? ledger_us(ofs, has) : has
276
319
  rescue StandardError
277
320
  has
278
321
  end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bigdecimal/util')
4
+
5
+ # @author Hernani Rodrigues Vaz
6
+ module Cns
7
+ # (see Beaconchain)
8
+ class Beaconchain
9
+ # @return [Apibc] API blockchains
10
+ attr_reader :api
11
+ # @return [Array<Hash>] todos os dados bigquery
12
+ attr_reader :bqd
13
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
+ attr_reader :ops
15
+
16
+ # @param [Hash] dad todos os dados bigquery
17
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
18
+ # @option pop [Boolean] :v (false) mostra saldos?
19
+ # @option pop [Boolean] :t (false) mostra todos saldos ou somente novos?
20
+ # @return [Beaconchain] API beaconchain - processar historico saldos
21
+ def initialize(dad, pop)
22
+ @api = Apibc.new
23
+ @bqd = dad
24
+ @ops = pop
25
+ end
26
+
27
+ # @return [Array<Hash>] lista balancos novos
28
+ def nov
29
+ @nov ||= bcd.map { |obc| obc[:bx].select { |obj| idb.include?(itx(obj[:epoch], obj[:validatorindex])) } }.flatten
30
+ end
31
+
32
+ # @return [Array<Integer>] lista dos meus validators
33
+ def lax
34
+ @lax ||= bqd[:wb].map { |obj| obj[:id] }
35
+ end
36
+
37
+ # @return [Array<Hash>] todos os dados beaconchain - saldos & historico
38
+ def bcd
39
+ @bcd ||= api.data_bc("/api/v1/validator/#{lax.join(',')}").map { |obj| base_bc(obj) }
40
+ end
41
+
42
+ # @return [Array<Hash>] todos os dados juntos bigquery & beaconchain
43
+ def dados
44
+ @dados ||= bqd[:wb].map { |obq| bq_bc(obq, bcd.select { |obc| obq[:id] == obc[:ax] }.first) }
45
+ end
46
+
47
+ # @return [Array<Integer>] lista historicos novos
48
+ def idb
49
+ @idb ||= bcd.map { |obc| obc[:bx].map { |obj| itx(obj[:epoch], obj[:validatorindex]) } }.flatten -
50
+ (ops[:t] ? [] : bqd[:nb].map { |obq| obq[:itx] })
51
+ end
52
+
53
+ # @param [Integer] intum
54
+ # @param [Integer] intdois
55
+ # @return [Integer] szudzik pairing two integers
56
+ def itx(intum, intdois)
57
+ intum >= intdois ? intum * intum + intum + intdois : intum + intdois * intdois
58
+ end
59
+
60
+ # @example
61
+ # {
62
+ # activationeligibilityepoch: 0,
63
+ # activationepoch: 0,
64
+ # balance: 32_489_497_108,
65
+ # effectivebalance: 32_000_000_000,
66
+ # exitepoch: 9_223_372_036_854_775_807,
67
+ # lastattestationslot: 265_446,
68
+ # name: '',
69
+ # pubkey: '0x93bf23a587f11f9eca329a12ef51296b8a9848af8c0fe61201524b14cb85b0c6fbd3e427501cdfa3b28719bd1ed96fff',
70
+ # slashed: false,
71
+ # status: 'active_online',
72
+ # validatorindex: 11_766,
73
+ # withdrawableepoch: 9_223_372_036_854_775_807,
74
+ # withdrawalcredentials: '0x004f11be01cb72187715c55d6348c67c5a3880687cd42692306fdbc91ac2da9b'
75
+ # }
76
+ # @param [Hash] abc account beaconchain
77
+ # @return [Hash] dados beaconchain - index, saldo & historico
78
+ def base_bc(abc)
79
+ acc = abc[:validatorindex]
80
+ {
81
+ ax: acc,
82
+ sl: (abc[:balance].to_d / 10**9).round(10),
83
+ bx: api.data_bc("/api/v1/validator/#{acc}/balancehistory")
84
+ }
85
+ end
86
+
87
+ # @param [Hash] wbq wallet bigquery
88
+ # @param abc (see base_bc)
89
+ # @return [Hash] dados juntos bigquery & beaconchain
90
+ def bq_bc(wbq, abc)
91
+ xbq = wbq[:id]
92
+ {
93
+ id: xbq,
94
+ ax: wbq[:ax],
95
+ bs: wbq[:sl],
96
+ bb: bqd[:nb].select { |onb| onb[:iax] == xbq },
97
+ es: abc[:sl],
98
+ eb: abc[:bx]
99
+ }
100
+ end
101
+ end
102
+ end