forked from ajunge/buda-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buda.js
279 lines (235 loc) · 7.55 KB
/
buda.js
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
var querystring = require("querystring");
var requestPromise = require('request-promise');
var _ = require('underscore');
var crypto = require('crypto');
var Promise = require('bluebird');
_.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, base_url='https://www.buda.com') {
this.api_key = api_key;
this.api_secret = api_secret;
this.base_url = base_url;
_.bindAll.apply(_, [this].concat(_.functions(this)));
}
Buda.prototype._request = function(method, path, args, data, auth=false) {
var fullPath = path + (querystring.stringify(args) === '' ? '' : '?') + querystring.stringify(args);
var options = {
uri: this.base_url + fullPath,
method: method,
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; buda-promise Node.js client)',
'Content-Type': 'application/json'
},
timeout: 5000,
resolveWithFullResponse: true,
json: true
};
if (data) {
options.body = data;
}
if(auth){
if(!this.api_key || !this.api_secret)
return Promise.reject('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);
}
//console.log(options)
return requestPromise(options)
.then(function(res) {
return res.body;
}).catch(function(err) {
let message;
if (err.name === 'StatusCodeError') {
message = 'Buda error ' + err.statusCode + ': ' + (err.statusCode === 404 ? 'Not found' : JSON.stringify(err.response.body));
} else {
message = 'Buda error: ' + err.message;
}
throw new Error(message);
});
}
// if you call new Date to 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/#order-book
Buda.prototype.order_book = function(market) {
return this._request('GET','/api/v2/markets/'+market+'/order_book');
}
// https://api.buda.com/#trades
Buda.prototype.trades = function(market, timestamp) {
return this._request('GET','/api/v2/markets/'+market+'/trades',{timestamp: timestamp});
}
// https://api.buda.com/#markets
Buda.prototype.markets = function() {
return this._request('GET','/api/v2/markets');
}
// 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) {
var payload={
order: {
type: type,
price_type: price_type,
limit: limit,
amount: amount
}
}
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/#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/#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.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/#nuevo-retiro-lightning
Buda.prototype.lightning_withdrawal = function(amount, invoice, simulate) {
var payload={
amount: amount,
simulate: simulate || false,
reserve_code: 'ln-btc',
withdrawal_data: {
payment_request: invoice,
}
}
return this._request('POST','/api/v2/currencies/btc/withdrawals',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);
}
module.exports = Buda;