This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
README
460 lines (387 loc) · 12.4 KB
/
README
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
Node Bittrex API
=========
Node Bittrex API is an asynchronous node.js library for the Bittrex API - https://bittrex.com/.
The Bittrex API data can be received either as a GET request or via Websockets API (the Stream option will no longer be maintained and will be removed in further releases - please switch to Websockets if you want to use real Streams).
Documentation to the Bittrex API: https://bittrex.com/Home/Api
This Library was created by [Adrian Soluch (@n0mad01)](https://github.com/n0mad01/) [soluch.us](http://soluch.us) and is licensed under the [MIT license](https://github.com/n0mad01/node.bittrex.api/blob/master/LICENSE).
Contributors
----
Thanks go to the people who have contributed code to this Library.
* [dparlevliet](https://github.com/dparlevliet) Special kudos - thanks to him i was able to add the Websocket API, also did he added the error object/handling param and the getcandles method for the Bittrex API V2
* [samuelhei](https://github.com/samuelhei) Special kudos - thanks to him all missing calls are complemented as also structural improvements have been made.
* [mhuggins](https://github.com/mhuggins)
* [192-sean](https://github.com/192-sean)
* [caffeinewriter](https://github.com/caffeinewriter)
* [apense](https://github.com/apense)
Before you start
----
This is just a quick reminder that you are handling coins with this library (and thus real money), so, understand the situation as much as possible and make everything to prevent losing them.
Here is a small checklist you should go through before you start:
1. Make sure you don't give your api key more rights as absolutely necessary - for first testing READ INFO alone should be enough! (bittrex.com under: Settings/API Keys)
![bittrex_ap_keys_control](https://user-images.githubusercontent.com/260321/29748739-a6c2c00e-8b1c-11e7-95ec-1b0221348235.png)
2. make sure to understand the API Key permissions
1. READ INFO - Allows you to read private details such as open orders, order history, balances, etc
2. TRADE LIMIT - Allows you to create/cancel trade limit buy/sell orders
3. TRADE MARKET - allows you to create/cancel market buy/sell orders
4. WITHDRAW - Allows you to withdraw to another address
3. Make use of the Bittrex IP Whitelist as also the Withdrawal Whitelist features
4. Do not ever commit your API Keys to GitHub or expose them under any circumstances!
Quick start
----
```sh
$ npm install node.bittrex.api
```
```javascript
var bittrex = require('node.bittrex.api');
bittrex.options({
'apikey' : API_KEY,
'apisecret' : API_SECRET,
});
bittrex.getmarketsummaries( function( data, err ) {
if (err) {
return console.error(err);
}
for( var i in data.result ) {
bittrex.getticker( { market : data.result[i].MarketName }, function( ticker ) {
console.log( ticker );
});
}
});
```
Advanced start
----
fetch the project via git:
```sh
$ git clone https://github.com/n0mad01/node.bittrex.api.git
```
then meet the package dependencies:
```sh
$ cd node-bittrex-api/
$ npm install
```
include node.bittrex.api.js into your project:
```javascript
var bittrex = require('./node.bittrex.api.js');
```
##### configure
```javascript
bittrex.options({
'apikey' : API_KEY,
'apisecret' : API_SECRET,
'stream' : true, // will be removed from future versions
'verbose' : true,
'cleartext' : false
});
```
By default the returned data is an object, in order to get clear text you have to add the option **cleartext** (streams will always return text):
```javascript
'cleartext' : true
```
The baseUrl itself can also be set via options
```javascript
'baseUrl' : 'https://bittrex.com/api/v1',
'baseUrlv2' : 'https://bittrex.com/Api/v2.0',
```
Change the callbacks arguments sequence
```javascript
'inverse_callback_arguments' : true,
```
This simply changes the sequence in which the arguments are passed, instead of e.g.:
```javascript
getmarkethistory({market : 'USDT-BTC'}, function(data, error) {});
```
you'll get the reversed order:
```javascript
getmarkethistory({market : 'USDT-BTC'}, function(error, data) {});
```
Websockets
--
following methods are implemented:
> websockets.listen, websockets.subscribe
listen example
```javascript
var websocketsclient = bittrex.websockets.listen( function( data ) {
if (data.M === 'updateSummaryState') {
data.A.forEach(function(data_for) {
data_for.Deltas.forEach(function(marketsDelta) {
console.log('Ticker Update for '+ marketsDelta.MarketName, marketsDelta);
});
});
}
});
```
subscribe example
```javascript
var websocketsclient = bittrex.websockets.subscribe(['BTC-ETH','BTC-SC','BTC-ZEN'], function(data) {
if (data.M === 'updateExchangeState') {
data.A.forEach(function(data_for) {
console.log('Market Update for '+ data_for.MarketName, data_for);
});
}
});
```
simple client & redefine serviceHandlers example
```javascript
var websocketsclient = bittrex.websockets.client();
websocketsclient.serviceHandlers.reconnecting = function (message) {
return true; // set to true stops reconnect/retrying
}
websocketsclient.serviceHandlers.messageReceived = function (message) {
console.log(message); // the messages received must be parsed as json first e.g. via jsonic(message.utf8Data)
}
```
all possible serviceHandlers
```javascript
bound: function() { console.log("Websocket bound"); },
connectFailed: function(error) { console.log("Websocket connectFailed: ", error); },
connected: function(connection) { console.log("Websocket connected"); },
disconnected: function() { console.log("Websocket disconnected"); },
onerror: function (error) { console.log("Websocket onerror: ", error); },
messageReceived: function (message) { console.log("Websocket messageReceived: ", message); return false; },
bindingError: function (error) { console.log("Websocket bindingError: ", error); },
connectionLost: function (error) { console.log("Connection Lost: ", error); },
reconnecting: function (retry { inital: true/false, count: 0} ) {
console.log("Websocket Retrying: ", retry);
//return retry.count >= 3; // cancel retry true
return true;
}
```
Streams - please notice that streams will be removed from future versions
--
To activate Streaming simply add to your options:
```javascript
'stream' : true
```
Examples
--
After configuration you can use the object right away:
example #1
```javascript
bittrex.getmarketsummaries( function( data, err ) {
if (err) {
return console.error(err);
}
for( var i in data.result ) {
bittrex.getticker( { market : data.result[i].MarketName }, function( ticker ) {
console.log( ticker );
});
}
});
```
example #2
```javascript
bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
if (err) {
return console.error(err);
}
console.log( data );
});
```
Libraries
--
Websockets depends on the following npm packages:
- signalR websockets client https://www.npmjs.com/package/signalrjs
- jsonic JSON parser https://www.npmjs.com/package/jsonic
Streaming depends on the following npm packages (will be removed in future versions):
- JSONStream https://www.npmjs.org/package/JSONStream
- event-stream https://www.npmjs.org/package/event-stream
Other libraries utilized:
- request https://www.npmjs.org/package/request
For HmacSHA512 this package is using a part of Googles Crypto.js (the node crypt package could not provide any appropriate result).
- http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js
Error examples
---
Example of request/domain based errors (not Bittrex API error)
```javascript
var url = 'http://fake.bittrex.com/api/v1.1/public/getticker?market=USDT-BTCXXX';
bittrex.sendCustomRequest( url, function( data, err ) {
if (err) {
/**
{
success: false,
message: 'URL request error',
error:
{ Error: getaddrinfo ENOTFOUND fake.bittrex.com fake.bittrex.com:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'fake.bittrex.com',
host: 'fake.bittrex.com',
port: 80 },
result: undefined
}
*/
return console.error(err);
}
console.log(data);
});
```
Example of request/url based errors (not Bittrex API error)
```javascript
var url = 'http://bittrex.com/api/v1.1/public/getfakeendpoint';
bittrex.sendCustomRequest( url, function( data, err ) {
if (err) {
/**
{
success: false,
message: 'URL request error',
error: undefined,
result: {
statusCode: 404,
statusMessage: 'Not Found',
body: '<!DOCTYPE html>\r\n<html > ...'
}
}
*/
return console.error(err);
}
console.log(data);
});
```
Example of Bittrex API error
```javascript
bittrex.getcandles({
marketName: 'USDT-BTC',
tickInterval: 300
}, function(data, err) {
if (err) {
/**
{
success: false,
message: 'INVALID_TICK_INTERVAL',
result: null
}
*/
return console.error(err);
}
console.log(data);
});
```
Methods
----
Optional parameters may have to be looked up at https://bittrex.com/Home/Api.
> It may happen that some Bittrex API methods are missing, also they could have been forgotten in the documentation. In this case, if this strikes you, feel free to open a issue or send me a pull request.
> Also: the method **sendCustomRequest** enables completely custom requests, regardless the specific API methods.
##### sendCustomRequest
- url String
- callback Function
- credentials Boolean optional whether the credentials should be applied to the request/stream or not, default is set to false.
example #1
```javascript
var url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
bittrex.sendCustomRequest( url, function( data, err ) {
console.log( data );
});
```
example #2 (credentials applied to request/stream)
```javascript
bittrex.sendCustomRequest( 'https://bittrex.com/api/v1.1/account/getbalances?currency=BTC', function( data, err ) {
console.log( data );
}, true );
will result in (the Header is being set too):
https://bittrex.com/api/v1.1/account/getbalances?currency=BTC&apikey=API_KEY&nonce=4456490600
```
##### getticker
```javascript
bittrex.getticker( { market : 'BTC-LTC' }, function( data, err ) {
console.log( data );
});
```
##### getbalances
```javascript
bittrex.getbalances( function( data, err ) {
console.log( data );
});
```
##### getmarkethistory
```javascript
bittrex.getmarkethistory({ market : 'BTC-LTC' }, function( data, err ) {
console.log( data );
});
```
##### getcandles (v2 method)
```javascript
bittrex.getcandles({
marketName: 'USDT-BTC',
tickInterval: 'fiveMin', // intervals are keywords (oneMin, fiveMin, hour, thirtyMin, Day)
}, function( data, err ) {
console.log( data );
});
```
##### getmarketsummaries
```javascript
bittrex.getmarketsummaries( function( data, err ) {
console.log( data );
});
```
##### getmarketsummary
```javascript
bittrex.getmarketsummary( { market : 'BTC-LTC'}, function( data, err ) {
console.log( data );
});
```
##### getorderbook
```javascript
bittrex.getorderbook({ market : 'BTC-LTC', depth : 10, type : 'both' }, function( data, err ) {
console.log( data );
});
```
##### getwithdrawalhistory
```javascript
bittrex.getwithdrawalhistory({ currency : 'BTC' }, function( data, err ) {
console.log( data );
});
```
##### getdepositaddress
```javascript
bittrex.getdepositaddress({ currency : 'BTC' }, function( data, err ) {
console.log( data );
});
```
##### getdeposithistory
```javascript
bittrex.getdeposithistory({ currency : 'BTC' }, function( data, err ) {
console.log( data );
});
```
##### getbalance
```javascript
bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
console.log( data );
});
```
##### withdraw
```javascript
bittrex.withdraw({ currency : 'BTC', quantity : '1.5112', address : 'THE_ADDRESS' }, function( data, err ) {
console.log( data );
});
```
Testing
----
Installing test gear
```bash
npm install --only=dev
```
Running all tests
```bash
npm test tests
```
or individually
```bash
npm test tests/public.js
npm test tests/private.js
```
##### Testing private methods
Testing private method endpoints requires an api key/secret which should be
installed in to ``tests/config.json`` - you will find an example file in
``tests/config_example.json``.
```bash
cp tests/tests_example.json tests/config.json
vim tests/config.json
```
Donations welcome!
---
BTC
> 17gtixgt4Q8hZcSictQwj5WUdgFjegCt36