forked from askmike/btcchina
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbtcchina.js
290 lines (225 loc) · 7.85 KB
/
btcchina.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
var querystring = require("querystring");
var https = require('https');
var http = require('http');
var _ = require('underscore');
var crypto = require('crypto');
var BTCChina = function(key, secret) {
this.key = key;
this.secret = secret;
_.bindAll(this);
}
// internal method for making a REST call
BTCChina.prototype._request = function(handler, options, data, callback) {
var req = handler.request(options, function(res) {
res.setEncoding('utf8');
var buffer = '';
res.on('data', function(data) {
buffer += data;
});
res.on('end', function() {
// check and return unauthorized messages
if(buffer.lastIndexOf('401 Unauthorized', 0) === 0)
return callback('General API error: '+buffer);
else if(buffer === 'HTTP 403 Forbidden')
return callback('403 Forbidden to access method');
try {
var json = JSON.parse(buffer);
} catch (err) {
return callback(err);
}
if('error' in json)
return callback('API error: ' + json.error.message + ' (code ' + json.error.code + ')');
callback(null, json);
});
});
req.on('error', function(err) {
callback(err);
});
req.end(data);
}
// internal method for accessing the Market API
//
// http://btcchina.org/api-market-data-documentation-en
BTCChina.prototype._marketRequest = function(method, params, callback) {
var options = {
host: 'data.btcchina.com',
path: '/data/' + method + '/?' + querystring.stringify(params),
method: 'GET',
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; BTCchina node.js client)'
}
};
this._request(https, options, null, callback);
}
//
// Market API calls
//
BTCChina.prototype.ticker = function(market, callback) {
var params = {};
if(market)
params['market']=market;
this._marketRequest('ticker', params, callback);
}
BTCChina.prototype.trades = function(callback) {
this._marketRequest('trades', null, callback);
}
BTCChina.prototype.historydata = function(limit, since, sincetype, callback) {
var params = {};
if(limit)
params['limit']=limit;
if(since)
params['since']=since;
if(sincetype)
params['sincetype']=sincetype;
this._marketRequest('historydata', params, callback);
}
BTCChina.prototype.orderbook = function(market, limit, callback) {
var params = {};
if(market)
params['market']=market;
if(limit)
params['limit']=limit
this._marketRequest('orderbook', params, callback);
}
// internal method for accessing the Trade API
//
// http://btcchina.org/api-trade-documentation-en
BTCChina.prototype._tradeRequest = function(method, params, callback) {
if(!this.key || !this.secret)
throw 'Must provide key and secret to make Trade API requests';
if(!_.isArray(params))
throw 'Params need to be an array with parameters in the order they are listed in the API docs.';
if(!_.isFunction(callback))
callback = function() {};
var tonce = new Date() * 1000; // spoof microsecond
var args = {
tonce: tonce,
accesskey: this.key,
requestmethod: 'post',
id: 1,
method: method,
params: params.join('~') // we need commas here in the querystring
// hacky workaround to perserve them
};
var qs = querystring.stringify(args).replace(/~/g, ',');
var signer = crypto.createHmac('sha1', this.secret);
var hmac = signer.update(qs).digest('hex');
var signature = new Buffer(this.key + ':' + hmac).toString('base64');
var body = JSON.stringify({
method: args.method,
params: params,
id: args.id
}, null, 4);
var options = {
host: 'api.btcchina.com',
path: '/api_trade_v1.php',
method: 'POST',
headers: {
'User-Agent': 'Mozilla/4.0 (compatible; BTCchina node.js client)',
'Content-Length': body.length,
'Authorization': 'Basic ' + signature,
'Json-Rpc-Tonce': tonce
}
};
this._request(https, options, body, callback);
}
//
// Trade API calls
//
BTCChina.prototype.buyOrder = function(price, amount, callback) {
throw 'This method is deprecated. Please use buyOrder2.';
}
BTCChina.prototype.buyOrder2 = function(price, amount, market, callback) {
if(market === null || market === undefined)
this._tradeRequest('buyOrder2', [price, amount], callback);
else
this._tradeRequest('buyOrder2', [price, amount], market, callback);
}
BTCChina.prototype.cancelOrder = function(id, market, callback) {
if(market === null || market === undefined)
this._tradeRequest('cancelOrder', [id], callback);
else
this._tradeRequest('cancelOrder', [id, market], callback);
}
BTCChina.prototype.getAccountInfo = function(type, callback) {
if(type === null || type === undefined)
this._tradeRequest('getAccountInfo', [], callback);
else
this._tradeRequest('getAccountInfo', [type], callback);
}
BTCChina.prototype.getDeposits = function(currency, pendingonly, callback) {
if(pendingonly === null || pendingonly === undefined)
// default is true
this._tradeRequest('getDeposits', [currency], callback);
else
this._tradeRequest('getDeposits', [currency, pendingonly], callback);
}
BTCChina.prototype.getMarketDepth = function() {
throw 'This method is deprecated. Please use getMarketDepth2.';
}
BTCChina.prototype.getMarketDepth2 = function(limit, market, callback) {
if(limit === null || limit === undefined)
limit = 10;
if(market === null || market === undefined)
market='BTCCNY';
this._tradeRequest('getMarketDepth2', [limit, market], callback);
}
BTCChina.prototype.getOrder = function(id, market, withdetail, callback) {
if(market === null || market === undefined)
market='BTCCNY';
if(withdetail === null || withdetail === undefined)
withdetail=false;
this._tradeRequest('getOrder', [id, market, withdetail], callback);
}
BTCChina.prototype.getOrders = function(openonly, market, limit, offset, since, withdetail, callback) {
if(openonly === null || openonly === undefined)
openonly=true;
if(market === null || market === undefined)
market='BTCCNY';
if(limit === null || limit === undefined)
limit=1000;
if(offset === null || offset === undefined)
offset=0;
if(since === null || since === undefined)
since=0;
if(withdetail === null || withdetail === undefined)
withdetail=false;
this._tradeRequest('getOrders', [openonly, market, limit, offset, since, withdetail], callback);
}
BTCChina.prototype.getTransactions = function(type, limit, offset, since, sincetype, callback) {
if(type === null || type === undefined)
type = 'all';
if(limit === null || limit === undefined)
limit = 10;
if(offset === null || offset === undefined)
offset = 0;
if(since === null || since === undefined)
since = 0;
if(sincetype === null || sincetype === undefined)
sincetype = 'time';
this._tradeRequest('getTransactions', [type, limit, offset, since, sincetype], callback);
}
BTCChina.prototype.getWithdrawal = function(id, currency, callback) {
if(currency === null || currency === undefined)
currency = 'BTC';
this._tradeRequest('getWithdrawal', [id, currency], callback);
}
BTCChina.prototype.getWithdrawals = function(currency, pendingonly, callback) {
if(pendingonly === false)
this._tradeRequest('getWithdrawals', [currency, pendingonly], callback);
else
this._tradeRequest('getWithdrawals', [currency], callback);
}
BTCChina.prototype.requestWithdrawal = function(currency, amount, callback) {
this._tradeRequest('requestWithdrawal', [currency, amount], callback);
}
BTCChina.prototype.sellOrder = function(currency, amount, callback) {
throw 'This method is deprecated. Please use sellOrder2.';
}
BTCChina.prototype.sellOrder2 = function(currency, amount, market, callback) {
if(market === null || market === undefined)
this._tradeRequest('sellOrder2', [currency, amount], callback);
else
this._tradeRequest('sellOrder2', [currency, amount], market, callback);
}
module.exports = BTCChina;