-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuda.js
More file actions
390 lines (335 loc) · 11.3 KB
/
buda.js
File metadata and controls
390 lines (335 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
var querystring = require("querystring");
var axios = require('axios');
var _ = require('underscore');
var crypto = require('crypto');
_.mixin({
// compact for objects
compactObject: function(to_clean) {
_.map(to_clean, function(value, key, to_clean) {
if (value === undefined)
delete to_clean[key];
});
return to_clean;
}
});
var Buda = function(api_key, api_secret, options) {
if (typeof options === 'string') {
// backwards compatibility: 3rd param used to be base_url string
this.base_url = options;
this.timeout = 5000;
} else {
options = options || {};
this.base_url = options.base_url || 'https://www.buda.com';
this.timeout = options.timeout || 5000;
}
this.api_key = api_key;
this.api_secret = api_secret;
_.bindAll.apply(_, [this].concat(_.functions(this)));
}
Buda.prototype._request = function(method, path, args, data, auth) {
var fullPath = path + (querystring.stringify(args) === '' ? '' : '?') + querystring.stringify(args);
var options = {
url: this.base_url + fullPath,
method: method,
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; buda-promise Node.js client)',
'Content-Type': 'application/json'
},
timeout: this.timeout
};
if (data) {
options.data = data;
}
if (auth) {
if (!this.api_key || !this.api_secret)
return Promise.reject(new Error('Must provide api_key and api_secret to make this API request.'));
var authHeader = this._authHeader(method, fullPath, data);
options.headers = Object.assign(options.headers, authHeader);
}
return axios(options)
.then(function(res) {
return res.data;
}).catch(function(err) {
let message;
if (err.response) {
message = 'Buda error ' + err.response.status + ': ' + (err.response.status === 404 ? 'Not found' : JSON.stringify(err.response.data));
} else {
message = 'Buda error: ' + err.message;
}
throw new Error(message);
});
}
// if you call new Date too fast it will generate
// the same ms, helper to make sure the nonce is
// truly unique (supports up to 999 calls per ms).
Buda.prototype._generateNonce = function() {
var now = new Date().getTime();
if (now !== this.last)
this.nonceIncr = -1;
this.last = now;
this.nonceIncr++;
// add padding to nonce incr
// @link https://stackoverflow.com/questions/6823592/numbers-in-the-form-of-001
var padding =
this.nonceIncr < 10 ? '000' :
this.nonceIncr < 100 ? '00' :
this.nonceIncr < 1000 ? '0' : '';
return now + padding + this.nonceIncr;
}
Buda.prototype._authHeader = function(method, path, body) {
var nonce = this._generateNonce();
var message;
if (body) {
var base64_encoded_body = Buffer.from(JSON.stringify(body)).toString('base64');
message = method + ' ' + path + ' ' + base64_encoded_body + ' ' + nonce;
} else {
message = method + ' ' + path + ' ' + nonce;
}
var signature = crypto.createHmac('sha384', this.api_secret).update(message).digest('hex');
return {
'X-SBTC-APIKEY': this.api_key,
'X-SBTC-NONCE': nonce,
'X-SBTC-SIGNATURE': signature
};
}
//
// Public API
//
// https://api.buda.com/#ticker
Buda.prototype.ticker = function(market) {
return this._request('GET', '/api/v2/markets/' + market + '/ticker');
}
// https://api.buda.com/#tickers
Buda.prototype.tickers = function() {
return this._request('GET', '/api/v2/tickers');
}
// https://api.buda.com/#order-book
Buda.prototype.order_book = function(market) {
return this._request('GET', '/api/v2/markets/' + market + '/order_book');
}
// https://api.buda.com/#rest-api-llamadas-publicas-volumen-transado
Buda.prototype.volume = function(market) {
return this._request('GET', '/api/v2/markets/' + market + '/volume');
}
// https://api.buda.com/#trades
Buda.prototype.trades = function(market, timestamp, limit) {
var payload = {
timestamp: timestamp,
limit: limit
};
return this._request('GET', '/api/v2/markets/' + market + '/trades', payload);
}
// https://api.buda.com/#markets
Buda.prototype.markets = function() {
return this._request('GET', '/api/v2/markets');
}
// https://api.buda.com/#detalle-de-mercado
Buda.prototype.market = function(market) {
return this._request('GET', '/api/v2/markets/' + market);
}
// https://api.buda.com/#costos-de-abonos-retiros
Buda.prototype.fees = function(currency, type) {
return this._request('GET', `/api/v2/currencies/${currency}/fees/${type}`);
}
// https://api.buda.com/#cotizaciones
Buda.prototype.get_quotation = function(market, type, amount, limit) {
var payload = {
type: type,
amount: amount,
limit: limit
};
var url = '/api/v2/markets/' + market + '/quotations';
return this._request('POST', url, null, payload, true);
};
//
// Private API
// (you need to have api_key / api_secret)
//
// Undocumented
Buda.prototype.me = function() {
return this._request('GET', '/api/v2/me', null, null, true);
}
// https://api.buda.com/#balances
Buda.prototype.balance = function(currency) {
var curr = '';
if (currency) curr = '/' + currency;
return this._request('GET', '/api/v2/balances' + curr, null, null, true);
}
// https://api.buda.com/#mis-rdenes
Buda.prototype.order_pages = function(market, per, page, state) {
var args = {
per: per,
page: page,
state: state
}
return this._request('GET', '/api/v2/markets/' + market + '/orders', args, null, true);
}
// https://api.buda.com/#nueva-orden
Buda.prototype.new_order = function(market, type, price_type, limit, amount, client_id) {
var payload = {
order: _.compactObject({
type: type,
price_type: price_type,
limit: limit,
amount: amount,
client_id: client_id
})
}
return this._request('POST', '/api/v2/markets/' + market + '/orders', null, payload, true);
}
// https://api.buda.com/#cancelar-orden
Buda.prototype.cancel_order = function(order_id) {
var payload = {
state: "canceling"
}
return this._request('PUT', '/api/v2/orders/' + order_id, null, payload, true);
}
// https://api.buda.com/#rest-api-llamadas-privadas-cancelar-todas-mis-ordenes
Buda.prototype.cancel_orders = function(market, type) {
var payload = {
market: market,
type: type
}
return this._request('DELETE', '/api/v2/orders', null, payload, true);
}
// https://api.buda.com/#estado-de-la-orden
Buda.prototype.single_order = function(order_id) {
return this._request('GET', '/api/v2/orders/' + order_id, null, null, true);
}
// https://api.buda.com/#lote-de-ordenes
Buda.prototype.batch_orders = function(diff) {
var payload = {
diff: diff
}
return this._request('POST', '/api/v2/orders', null, payload, true);
}
// https://api.buda.com/#orden-por-client-id
Buda.prototype.order_by_client_id = function(client_id) {
return this._request('GET', '/api/v2/orders/by-client-id/' + client_id, null, null, true);
}
// https://api.buda.com/#cancelar-orden-por-client-id
Buda.prototype.cancel_order_by_client_id = function(client_id) {
var payload = {
state: "canceling"
}
return this._request('PUT', '/api/v2/orders/by-client-id/' + client_id, null, payload, true);
}
// https://api.buda.com/#historial-de-depositos-retiros
Buda.prototype.deposits = function(currency, per, page, state) {
var args = {
per: per,
page: page,
state: state,
}
return this._request('GET', '/api/v2/currencies/' + currency + '/deposits', args, null, true);
}
Buda.prototype.withdrawals = function(currency, per, page, state) {
var args = {
per: per,
page: page,
state: state,
}
return this._request('GET', '/api/v2/currencies/' + currency + '/withdrawals', args, null, true);
}
// https://api.buda.com/#nuevo-retiro
Buda.prototype.new_crypto_withdrawal = function(currency, amount, target_address, simulate) {
var payload = {
amount: amount,
currency: currency,
simulate: simulate || false,
withdrawal_data: {
target_address: target_address
}
}
return this._request('POST', '/api/v2/currencies/' + currency + '/withdrawals', null, payload, true);
}
// https://api.buda.com/#retiro-dinero-fiat
Buda.prototype.new_fiat_withdrawal = function(currency, amount, simulate) {
var payload = {
amount: amount,
simulate: simulate || false
}
return this._request('POST', '/api/v2/currencies/' + currency + '/withdrawals', null, payload, true);
}
// https://api.buda.com/#nuevo-retiro-lightning
Buda.prototype.lightning_withdrawal = function(amount, invoice, simulate) {
var payload = {
amount: amount,
withdrawal_data: {
payment_request: invoice,
},
simulate: simulate || false,
}
return this._request('POST', '/api/v2/reserves/ln-btc/withdrawals', null, payload, true);
}
// https://api.buda.com/#nuevo-abono-lightning
Buda.prototype.lightning_network_invoices = function(amount, currency, memo, expiry_seconds) {
var payload = {
amount_satoshis: amount,
currency: currency,
memo: memo,
expiry_seconds: expiry_seconds || false
}
return this._request('POST', '/api/v2/lightning_network_invoices', null, payload, true);
}
// https://api.buda.com/#dep-sito-dinero-fiat
Buda.prototype.new_fiat_deposit = function(currency, amount, simulate) {
var payload = {
amount: amount,
simulate: simulate || false
}
return this._request('POST', '/api/v2/currencies/' + currency + '/deposits', null, payload, true);
}
// https://api.buda.com/#dep-sito-criptomonedas
Buda.prototype.new_crypto_address = function(currency) {
return this._request('POST', '/api/v2/currencies/' + currency + '/receive_addresses', null, null, true);
}
Buda.prototype.get_address = function(currency, address_id) {
var addr = '';
if (address_id) addr = '/' + address_id;
return this._request('GET', '/api/v2/currencies/' + currency + '/receive_addresses' + addr, null, null, true);
}
//
// Cross Border Payments API
//
// https://api.buda.com/#nueva-remesa
Buda.prototype.quote_remittance = function(params) {
var payload = {
origin_amount: params.origin_amount,
destination_amount: params.destination_amount,
origin_currency: params.origin_currency,
destination_currency: params.destination_currency,
client_reference_id: params.client_reference_id,
recipient_data: params.recipient_data
}
return this._request('POST', '/api/v2/remittances', null, _.compactObject(payload), true);
}
// https://api.buda.com/#aceptar-remesa
Buda.prototype.accept_remittance = function(remittance_id) {
return this._request('POST', '/api/v2/remittances/' + remittance_id + '/accept', null, null, true);
}
// https://api.buda.com/#detalle-de-remesa
Buda.prototype.remittance = function(remittance_id) {
return this._request('GET', '/api/v2/remittances/' + remittance_id, null, null, true);
}
// https://api.buda.com/#listar-remesas
Buda.prototype.remittances = function(per, page) {
var args = {
per: per,
page: page
}
return this._request('GET', '/api/v2/remittances', args, null, true);
}
// https://api.buda.com/#listar-destinatarios
Buda.prototype.remittance_recipients = function(per, page) {
var args = {
per: per,
page: page
}
return this._request('GET', '/api/v2/remittance_recipients', args, null, true);
}
// https://api.buda.com/#detalle-de-destinatario
Buda.prototype.remittance_recipient = function(recipient_id) {
return this._request('GET', '/api/v2/remittance_recipients/' + recipient_id, null, null, true);
}
module.exports = Buda;