forked from askmike/bitstamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstamp.js
191 lines (157 loc) · 4.96 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
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() {
try {
var json = JSON.parse(buffer);
} catch (err) {
return callback(err);
}
callback(null, json);
});
});
req.on('error', function(err) {
callback(err);
});
req.end(data);
}
Bitstamp.prototype._get = function(action, callback, args) {
args = _.compactObject(args);
var path = '/api/' + action + '/?' + querystring.stringify(args);
this._request('get', path, undefined, callback, args)
}
Bitstamp.prototype._post = function(action, callback, args) {
if(!this.key || !this.secret || !this.client_id)
return callback('Must provide key, secret and client ID to make this API request.');
var path = '/api/' + action + '/';
var nonce = +new Date() + '';
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(options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
this._get('transactions', callback, options);
}
Bitstamp.prototype.ticker = function(callback) {
this._get('ticker', callback);
}
Bitstamp.prototype.order_book = function(group, callback) {
if(!callback) {
callback = group;
group = undefined;
}
this._get('order_book', callback, {group: group});
}
Bitstamp.prototype.bitinstant = function(callback) {
this._get('bitinstant', callback);
}
Bitstamp.prototype.eur_usd = function(callback) {
this._get('eur_usd', callback);
}
//
// Private API
// (you need to have key / secret / client ID set)
//
Bitstamp.prototype.balance = function(callback) {
this._post('balance', callback);
}
Bitstamp.prototype.user_transactions = function(timedelta, callback) {
if(!callback) {
callback = timedelta;
timedelta = undefined;
}
this._post('user_transactions', callback, {timedelta: timedelta});
}
Bitstamp.prototype.open_orders = function(callback) {
this._post('open_orders', callback);
}
Bitstamp.prototype.cancel_order = function(id, callback) {
this._post('cancel_order', callback, {id: id});
}
Bitstamp.prototype.buy = function(amount, price, callback) {
this._post('buy', callback, {amount: amount, price: price});
}
Bitstamp.prototype.sell = function(amount, price, callback) {
this._post('sell', callback, {amount: amount, price: price});
}
Bitstamp.prototype.withdrawal_requests = function(callback) {
this._post('withdrawal_requests', callback);
}
Bitstamp.prototype.bitcoin_withdrawal = function(amount, address, callback) {
this._post('bitcoin_withdrawal', callback, {amount: amount, address: address});
}
Bitstamp.prototype.bitcoin_deposit_address = function(callback) {
this._post('bitcoin_deposit_address', callback);
}
Bitstamp.prototype.unconfirmed_btc = function(callback) {
this._post('unconfirmed_btc', callback);
}
Bitstamp.prototype.ripple_withdrawal = function(amount, address, currency, callback) {
this._post('ripple_withdrawal', callback, {amount: amount, address: address, currency: currency});
}
Bitstamp.prototype.ripple_address = function(callback) {
this._post('ripple_address', callback);
}
// These API calls return a 404 as of `Thu Oct 31 13:54:19 CET 2013`
// even though they are still in the API documentation
Bitstamp.prototype.create_code = function(usd, btc, callback) {
this._post('create_code', callback, {usd: usd, btc: btc});
}
Bitstamp.prototype.check_code = function(code, callback) {
this._post('check_code', callback, {code: code});
}
Bitstamp.prototype.redeem_code = function(code, callback) {
this._post('redeem_code', callback, {code: code});
}
module.exports = Bitstamp;