forked from askmike/bitstamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstamp.js
302 lines (250 loc) · 7.82 KB
/
bitstamp.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var querystring = require("querystring");
var https = require('https');
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 Bitstamp = function(key, secret, client_id) {
this.key = key;
this.secret = secret;
this.client_id = client_id;
_.bindAll(this);
}
Bitstamp.prototype._request = function(method, path, data, callback, args) {
var options = {
host: 'www.bitstamp.net',
path: path,
method: method,
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; Bitstamp node.js client)'
}
};
if(method === 'post') {
options.headers['Content-Length'] = data.length;
options.headers['content-type'] = 'application/x-www-form-urlencoded';
}
var req = https.request(options, function(res) {
res.setEncoding('utf8');
var buffer = '';
res.on('data', function(data) {
buffer += data;
});
res.on('end', function() {
if (res.statusCode !== 200) {
return callback(new Error('Bitstamp error ' + res.statusCode + ': ' + buffer));
}
try {
var json = JSON.parse(buffer);
} catch (err) {
return callback(err);
}
callback(null, json);
});
});
req.on('error', function(err) {
callback(err);
});
req.on('socket', function (socket) {
socket.setTimeout(5000);
socket.on('timeout', function() {
req.abort();
});
});
req.end(data);
}
// 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).
Bitstamp.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;
}
Bitstamp.prototype._get = function(market, action, callback, args) {
args = _.compactObject(args);
if(market)
var path = '/api/v2/' + action + '/' + market;
else
// some documented endpoints (eg `https://www.bitstamp.net/api/eur_usd/`)
// do not go via the v2 api.
var path = '/api/' + action;
path += (querystring.stringify(args) === '' ? '/' : '/?') + querystring.stringify(args);
this._request('get', path, undefined, callback, args)
}
Bitstamp.prototype._post = function(market, action, callback, args, legacy_endpoint) {
if(!this.key || !this.secret || !this.client_id)
return callback(new Error('Must provide key, secret and client ID to make this API request.'));
if(legacy_endpoint)
var path = '/api/' + action + '/';
else {
if(market)
var path = '/api/v2/' + action + '/' + market + '/';
else
var path = '/api/v2/' + action + '/';
}
var nonce = this._generateNonce();
var message = nonce + this.client_id + this.key;
var signer = crypto.createHmac('sha256', new Buffer(this.secret, 'utf8'));
var signature = signer.update(message).digest('hex').toUpperCase();
args = _.extend({
key: this.key,
signature: signature,
nonce: nonce
}, args);
args = _.compactObject(args);
var data = querystring.stringify(args);
this._request('post', path, data, callback, args);
}
//
// Public API
//
Bitstamp.prototype.transactions = function(market, options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
this._get(market, 'transactions', callback, options);
}
Bitstamp.prototype.ticker = function(market, callback) {
this._get(market, 'ticker', callback);
}
Bitstamp.prototype.ticker_hour = function(market, callback) {
this._get(market, 'ticker_hour', callback);
}
Bitstamp.prototype.order_book = function(market, group, callback) {
if(!callback) {
callback = group;
group = undefined;
}
var options;
if(typeof limit === 'object')
options = group;
else
options = {group: group};
this._get(market, 'order_book', callback, options);
}
// This API calls are removed from the documentation as of `Sat Jun 11 2016 10:10:07`
// Bitstamp.prototype.bitinstant = function(callback) {
// this._get('bitinstant', callback);
// }
Bitstamp.prototype.eur_usd = function(callback) {
this._get(null, 'eur_usd', callback);
}
//
// Private API
// (you need to have key / secret / client ID set)
//
Bitstamp.prototype.balance = function(market, callback) {
this._post(market, 'balance', callback);
}
Bitstamp.prototype.user_transactions = function(market, options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
this._post(market, 'user_transactions', callback, options);
}
Bitstamp.prototype.open_orders = function(market, callback) {
this._post(market, 'open_orders', callback);
}
Bitstamp.prototype.order_status = function (id, callback) {
this._post(null, 'order_status', callback, {id: id}, true);
};
Bitstamp.prototype.cancel_order = function(id, callback) {
this._post(null, 'cancel_order', callback, {id: id}, true);
}
Bitstamp.prototype.cancel_all_orders = function(callback) {
this._post(null, 'cancel_all_orders', callback, null, true);
}
Bitstamp.prototype.buy = function(market, amount, price, limit_price, callback) {
this._post(market, 'buy', callback, {
amount: amount,
price: price,
limit_price: limit_price
});
}
Bitstamp.prototype.buyMarket = function(market, amount, callback) {
this._post(market, 'buy/market', callback, {
amount: amount
});
}
Bitstamp.prototype.sell = function(market, amount, price, limit_price, callback) {
this._post(market, 'sell', callback, {
amount: amount,
price: price,
limit_price: limit_price
});
}
Bitstamp.prototype.sellMarket = function(market, amount, callback) {
this._post(market, 'sell/market', callback, {
amount: amount
});
}
Bitstamp.prototype.withdrawal_requests = function(callback) {
this._post(null, 'withdrawal_requests', callback, null, true);
}
Bitstamp.prototype.bitcoin_withdrawal = function(amount, address, instant, callback) {
this._post(null, 'bitcoin_withdrawal', callback, {
amount: amount,
address: address,
instant: instant
}, true);
}
Bitstamp.prototype.bitcoin_deposit_address = function(callback) {
this._post(null, 'bitcoin_deposit_address', callback, null, true);
}
Bitstamp.prototype.unconfirmed_btc = function(callback) {
this._post(null, 'unconfirmed_btc', callback, null, true);
}
// the API documentation is wrong as of `Sat Jun 11 2016 10:10:07`.
// It doesn't corectly list this call. Therefor not sure if all
// arguments are correct.
Bitstamp.prototype.ripple_withdrawal = function(amount, address, currency, callback) {
this._post(null, 'ripple_withdrawal', callback, {
amount: amount,
address: address,
currency: currency
}, true);
}
Bitstamp.prototype.xrp_withdrawal = function(amount, address, destination_tag, callback) {
this._post(null, 'xrp_withdrawal', callback, {
amount: amount,
address: address,
destination_tag: destination_tag
}, false);
}
Bitstamp.prototype.ripple_address = function(callback) {
this._post(null, 'ripple_address', callback, null, true);
}
Bitstamp.prototype.transfer_to_main = function(amount, currency, subAccount, callback) {
this._post(null, 'transfer-to-main', callback, {
amount: amount,
currency: currency,
subAccount: subAccount
}, true);
}
Bitstamp.prototype.transfer_from_main = function(amount, currency, subAccount, callback) {
this._post(null, 'transfer-from-main', callback, {
amount: amount,
currency: currency,
subAccount: subAccount
}, true);
}
module.exports = Bitstamp;