forked from naddison36/bitstamp-api
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitso.js
More file actions
202 lines (156 loc) · 5.02 KB
/
bitso.js
File metadata and controls
202 lines (156 loc) · 5.02 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
var _ = require('underscore');
var request = require('request');
var crypto = require('crypto');
var Bitso = function(key, secret, client_id) {
this.key = key;
this.secret = secret;
this.client_id = client_id;
this.timeoutMS = 10000;
this.url = 'https://api.bitso.com';
this.version = '/v2/';
_.bindAll(this, 'postReq', 'getReq', 'transactions', 'ticker', 'order_book', 'balance', 'user_transactions', 'open_orders', 'cancel_order', 'buy', 'sell', 'withdrawal_requests', 'bitcoin_withdrawal', 'bitcoin_deposit_address', 'unconfirmed_btc', 'ripple_withdrawal', 'ripple_address');
};
Bitso.prototype.postReq = function(action, callback, params) {
// Set custom User-Agent string
var headers = [];
headers['User-Agent'] = 'Bitso Javascript API Client';
var path = this.version + action + '/';
if(!this.key || !this.secret || !this.client_id)
return callback('Must provide key, secret and client ID to make this API request.');
var nonce = '' + new Date().getTime();
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();
params = _.extend({
key: this.key,
signature: signature,
nonce: nonce
}, params);
var options = {
url: this.url + path,
method: 'POST',
headers: headers,
timeout: this.timeoutMS,
form: params
};
var req = request.post(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;
if(error) {
callback(new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}
try {
data = JSON.parse(body);
}
catch(e) {
callback(new Error('Could not understand response from server: ' + body), null);
return;
}
if(data.error && data.error.length && (data.error === 'Invalid nonce' || data.error === 'Invalid signature' || data.error === 'API key not found')) {
callback(new Error(data.error), null);
} else {
callback(null, data);
}
}
});
return req;
};
Bitso.prototype.getReq = function(action, callback, params) {
// Set custom User-Agent string
var headers = [];
headers['User-Agent'] = 'Bitso Javascript API Client';
var path = this.version + action + '/';
var options = {
url: this.url + path,
method: 'GET',
headers: headers,
qs: params,
timeout: this.timeoutMS
};
var req = request.get(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;
if(error) {
callback(new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}
try {
data = JSON.parse(body);
}
catch(e) {
callback(new Error('Could not understand response from server: ' + body), null);
return;
}
callback(null, data);
}
});
return req;
};
//
// Public Functions
//
Bitso.prototype.transactions = function(options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
this.getReq('transactions', callback, options);
};
Bitso.prototype.ticker = function(callback) {
this.getReq('ticker', callback);
};
Bitso.prototype.order_book = function(group, callback) {
if(!callback) {
callback = group;
group = undefined;
}
this.getReq('order_book', callback, {group: group});
};
//
// Private Functions
//
Bitso.prototype.balance = function(callback) {
this.postReq('balance', callback);
};
Bitso.prototype.user_transactions = function(options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
this.postReq('user_transactions', callback, options);
};
Bitso.prototype.open_orders = function(callback) {
this.postReq('open_orders', callback);
};
Bitso.prototype.cancel_order = function(id, callback) {
this.postReq('cancel_order', callback, {id: id});
};
Bitso.prototype.lookup_order = function(id, callback) {
this.postReq('lookup_order', callback, {id: id});
};
Bitso.prototype.buy = function(amount, price, callback) {
this.postReq('buy', callback, {amount: amount, price: price});
};
Bitso.prototype.sell = function(amount, price, callback) {
this.postReq('sell', callback, {amount: amount, price: price});
};
Bitso.prototype.withdrawal_requests = function(callback) {
this.postReq('withdrawal_requests', callback);
};
Bitso.prototype.bitcoin_withdrawal = function(amount, address, callback) {
this.postReq('bitcoin_withdrawal', callback, {amount: amount, address: address});
};
Bitso.prototype.bitcoin_deposit_address = function(callback) {
this.postReq('bitcoin_deposit_address', callback);
};
Bitso.prototype.unconfirmed_btc = function(callback) {
this.postReq('unconfirmed_btc', callback);
};
Bitso.prototype.ripple_withdrawal = function(amount, address, currency, callback) {
this.postReq('ripple_withdrawal', callback, {amount: amount, address: address, currency: currency});
};
Bitso.prototype.ripple_address = function(callback) {
this.postReq('ripple_address', callback);
};
module.exports = Bitso;