cns 0.1.0 → 0.1.5

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,227 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('openssl')
4
+ require('base64')
5
+ require('curb')
6
+ require('json')
7
+
8
+ # @author Hernani Rodrigues Vaz
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
+
12
+ # (see Apice)
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) { |r| r.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) { |h| h.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) { |h| h.headers = hmt(uri) }.body,
80
+ symbolize_names: true
81
+ )[:balances]
82
+ .delete_if { |e| DC.include?(e[:currency]) }
83
+ .sort { |a, b| a[:currency] <=> b[: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) { |h| h.headers = hus(uri, nonce: non) }.body,
107
+ symbolize_names: true
108
+ )[:result]
109
+ rescue StandardError
110
+ {}
111
+ end
112
+
113
+ private
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
+ # @return [Integer] continually-increasing unsigned integer nonce from the current Unix Time
169
+ def nnc
170
+ Integer(Float(Time.now) * 1e6)
171
+ end
172
+
173
+ # @param [String] qry query a incluir no pedido HTTP
174
+ # @param [Integer] non continually-increasing unsigned integer
175
+ # @return [Hash] headers necessarios para pedido HTTP da exchange bitcoinde
176
+ def hde(qry, non = nnc)
177
+ {
178
+ 'X-API-KEY': ENV['BITCOINDE_API_KEY'],
179
+ 'X-API-NONCE': non,
180
+ 'X-API-SIGNATURE': OpenSSL::HMAC.hexdigest(
181
+ 'sha256',
182
+ ENV['BITCOINDE_API_SECRET'],
183
+ ['GET', qry, ENV['BITCOINDE_API_KEY'], non, Digest::MD5.hexdigest('')].join('#')
184
+ )
185
+ }
186
+ end
187
+
188
+ # @param (see hde)
189
+ # @return [Hash] headers necessarios para pedido HTTP da exchange paymium
190
+ def hfr(qry, non = nnc)
191
+ {
192
+ content_type: 'application/json',
193
+ 'Api-Key': ENV['PAYMIUM_API_KEY'],
194
+ 'Api-Nonce': non,
195
+ 'Api-Signature': OpenSSL::HMAC.hexdigest('sha256', ENV['PAYMIUM_API_SECRET'], [non, qry].join)
196
+ }
197
+ end
198
+
199
+ # @param (see hde)
200
+ # @return [Hash] headers necessarios para pedido HTTP da exchange therock
201
+ def hmt(qry, non = nnc)
202
+ {
203
+ content_type: 'application/json',
204
+ 'X-TRT-KEY': ENV['THEROCK_API_KEY'],
205
+ 'X-TRT-NONCE': non,
206
+ 'X-TRT-SIGN': OpenSSL::HMAC.hexdigest('sha512', ENV['THEROCK_API_SECRET'], [non, qry].join)
207
+ }
208
+ end
209
+
210
+ # @param qry (see hde)
211
+ # @param [Hash] ops opcoes trabalho
212
+ # @option ops [Hash] :nonce continually-increasing unsigned integer
213
+ # @return [Hash] headers necessarios para pedido HTTP da exchange kraken
214
+ def hus(qry, ops)
215
+ {
216
+ 'api-key': ENV['KRAKEN_API_KEY'],
217
+ 'api-sign': Base64.strict_encode64(
218
+ OpenSSL::HMAC.digest(
219
+ 'sha512',
220
+ Base64.decode64(ENV['KRAKEN_API_SECRET']),
221
+ ['/0/private/', qry, Digest::SHA256.digest("#{ops[:nonce]}#{URI.encode_www_form(ops)}")].join
222
+ )
223
+ )
224
+ }
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,280 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('openssl')
4
+ require('base64')
5
+ require('curb')
6
+ require('json')
7
+
8
+ # @author Hernani Rodrigues Vaz
9
+ module Cns
10
+ # classe para acesso dados centralized exchanges
11
+ class Apice
12
+ # @example trades_de
13
+ # {
14
+ # trades: [{
15
+ # trade_id: 'XUWWD3',
16
+ # trading_pair: 'btceur',
17
+ # is_external_wallet_trade: false,
18
+ # type: 'sell',
19
+ # amount_currency_to_trade: '0.1',
20
+ # price: 430,
21
+ # volume_currency_to_pay: 43,
22
+ # volume_currency_to_pay_after_fee: 42.79,
23
+ # amount_currency_to_trade_after_fee: 0.099,
24
+ # fee_currency_to_pay: 0.22,
25
+ # fee_currency_to_trade: '0.00100000',
26
+ # created_at: '2014-03-22T08:14:48+01:00',
27
+ # successfully_finished_at: '2014-03-25T14:03:22+01:00',
28
+ # state: 1,
29
+ # is_trade_marked_as_paid: true,
30
+ # trade_marked_as_paid_at: '2014-03-22T08:20:01+01:00',
31
+ # payment_method: 1,
32
+ # my_rating_for_trading_partner: 'positive',
33
+ # trading_partner_information: {
34
+ # username: 'emax2000',
35
+ # is_kyc_full: false,
36
+ # trust_level: 'bronze',
37
+ # amount_trades: 4,
38
+ # rating: 100,
39
+ # bank_name: 'CASSA DI RISPARMIO DI CIVITAVECCHIA SPA',
40
+ # bic: 'CRFIIT2CXXX',
41
+ # seat_of_bank: 'IT'
42
+ # }
43
+ # }, {}],
44
+ # page: { current: 1, last: 2 },
45
+ # errors: [],
46
+ # credits: 22
47
+ # }
48
+ # @param [Integer] pag pagina dos dados
49
+ # @param [Array<Hash>] ary acumulador dos dados
50
+ # @param [String] uri Uniform Resource Identifier do pedido HTTP
51
+ # @return [Array<Hash>] lista completa trades bitcoinde
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
60
+ rescue StandardError
61
+ ary
62
+ end
63
+
64
+ # @example deposits_de
65
+ # {
66
+ # deposits: [
67
+ # {
68
+ # deposit_id: '177245',
69
+ # txid: '84f9e85bc5709cd471e3d58a7d0f42d2c4a7bbd888cabf844e200efbf0a7fda2',
70
+ # address: '1KK6HhG3quojFS4CY1mPcbyrjQ8BMDQxmT',
71
+ # amount: '0.13283',
72
+ # confirmations: 6,
73
+ # state: 2,
74
+ # created_at: '2014-01-31T22:01:30+01:00'
75
+ # },
76
+ # {}
77
+ # ],
78
+ # page: { current: 1, last: 1 },
79
+ # errors: [],
80
+ # credits: 23
81
+ # }
82
+ # @param (see trades_de)
83
+ # @return [Array<Hash>] lista completa uniformizada depositos bitcoinde
84
+ 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
92
+ rescue StandardError
93
+ ary
94
+ end
95
+
96
+ # @example withdrawals_de
97
+ # {
98
+ # withdrawals: [
99
+ # {
100
+ # withdrawal_id: '136605',
101
+ # address: '1K9YMDDrmMV25EoYNqi7KUEK57Kn3TCNUJ',
102
+ # amount: '0.120087',
103
+ # network_fee: '0',
104
+ # comment: '',
105
+ # created_at: '2014-02-05T13:01:09+01:00',
106
+ # txid: '6264fe528116fcb87c812a306ca8409eecfec8fa941546c86f98984b882c8042',
107
+ # transferred_at: '2014-02-05T13:05:17+01:00',
108
+ # state: 1
109
+ # },
110
+ # {}
111
+ # ],
112
+ # page: { current: 1, last: 2 },
113
+ # errors: [],
114
+ # credits: 23
115
+ # }
116
+ # @param (see deposits_de)
117
+ # @return [Array<Hash>] lista completa uniformizada withdrawals bitcoinde
118
+ 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
126
+ rescue StandardError
127
+ ary
128
+ end
129
+
130
+ # @example ledger_fr
131
+ # [
132
+ # {
133
+ # uuid: '50551e61-4e74-4ae7-85fd-9c2040542818',
134
+ # currency_amount: nil,
135
+ # state: 'executed',
136
+ # btc_fee: '0.0',
137
+ # currency_fee: '0.0',
138
+ # created_at: '2014-03-04T09:00Z',
139
+ # updated_at: '2014-03-04T09:00Z',
140
+ # currency: 'EUR',
141
+ # comment: '5723',
142
+ # amount: '100.0',
143
+ # type: 'WireDeposit',
144
+ # account_operations: [{
145
+ # uuid: 'b5058a68-cf99-4438-86d3-e773eba418ec',
146
+ # name: 'wire_deposit',
147
+ # amount: '100.0',
148
+ # currency: 'EUR',
149
+ # created_at: '2014-03-04T09:00Z',
150
+ # created_at_int: 1_393_923_644,
151
+ # is_trading_account: false
152
+ # }, {}]
153
+ # },
154
+ # {}
155
+ # ]
156
+ # @param (see trades_de)
157
+ # @return [Array<Hash>] lista ledger paymium
158
+ 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,
161
+ symbolize_names: true
162
+ )
163
+ r.empty? ? ary : ledger_fr(pag + r.size, ary + r)
164
+ rescue StandardError
165
+ ary
166
+ end
167
+
168
+ # @example ledger_mt
169
+ # {
170
+ # transactions: [
171
+ # {
172
+ # id: 305_445,
173
+ # date: '2014-03-06T10:59:13.000Z',
174
+ # type: 'withdraw',
175
+ # price: 97.47,
176
+ # currency: 'EUR',
177
+ # fund_id: nil,
178
+ # order_id: nil,
179
+ # trade_id: nil,
180
+ # note: 'BOV withdraw',
181
+ # transfer_detail: nil
182
+ # },
183
+ # {}
184
+ # ],
185
+ # meta: {
186
+ # total_count: nil,
187
+ # first: { page: 1, href: 'https://api.therocktrading.com/v1/transactions?page=1' },
188
+ # previous: nil,
189
+ # current: { page: 1, href: 'https://api.therocktrading.com/v1/transactions?page=1' },
190
+ # next: { page: 2, href: 'https://api.therocktrading.com/v1/transactions?page=2' },
191
+ # last: nil
192
+ # }
193
+ # }
194
+ # @param (see trades_de)
195
+ # @return [Array<Hash>] lista ledger therock
196
+ 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,
199
+ symbolize_names: true
200
+ )[:transactions]
201
+ r.empty? ? ary : ledger_mt(pag + r.size, ary + r)
202
+ rescue StandardError
203
+ ary
204
+ end
205
+
206
+ # @example trades_us
207
+ # {
208
+ # error: [],
209
+ # result: {
210
+ # trades: {
211
+ # "TVINF5-TIOUB-YFNGKE": {
212
+ # ordertxid: 'ORPSUW-YKP4F-UJZOC6',
213
+ # pair: 'XETHXXBT',
214
+ # time: 1_463_435_684.8387,
215
+ # type: 'buy',
216
+ # ordertype: 'market',
217
+ # price: '0.024989',
218
+ # cost: '1.193973',
219
+ # fee: '0.003104',
220
+ # vol: '47.77994129',
221
+ # margin: '0.000000',
222
+ # misc: ''
223
+ # },
224
+ # "OUTRO-TRADE-ID": {}
225
+ # },
226
+ # count: 157
227
+ # }
228
+ # }
229
+ # @param [Integer] ofs offset dos dados
230
+ # @param [Hash] has acumulador dos dados
231
+ # @param (see account_us)
232
+ # @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,
236
+ symbolize_names: true
237
+ )[:result]
238
+ has.merge!(r[:trades])
239
+ ofs += 50
240
+ ofs < r[:count] ? trades_us(ofs, has) : has
241
+ rescue StandardError
242
+ has
243
+ end
244
+
245
+ # @example ledger_us
246
+ # {
247
+ # error: [],
248
+ # result: {
249
+ # ledger: {
250
+ # "LXXURB-ITI7S-CXVERS": {
251
+ # refid: 'ACCHF3A-RIBBMO-VYBESY',
252
+ # time: 1_543_278_716.2775,
253
+ # type: 'withdrawal',
254
+ # subtype: '',
255
+ # aclass: 'currency',
256
+ # asset: 'ZEUR',
257
+ # amount: '-15369.6200',
258
+ # fee: '0.0900',
259
+ # balance: '0.0062'
260
+ # },
261
+ # "OUTRO-LEDGER-ID": {}
262
+ # },
263
+ # count: 376
264
+ # }
265
+ # }
266
+ # @param (see trades_us)
267
+ # @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,
271
+ symbolize_names: true
272
+ )[:result]
273
+ has.merge!(r[:ledger])
274
+ ofs += 50
275
+ ofs < r[:count] ? ledger_us(ofs, has) : has
276
+ rescue StandardError
277
+ has
278
+ end
279
+ end
280
+ end