diff --git a/.eslintrc.json b/.eslintrc.json index b4f1215..b90624b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,6 +7,7 @@ "rules": { "semi": ["error", "always", {"omitLastInOneLineBlock": true}], "no-var": ["error"], - "prefer-const": ["error"] + "prefer-const": ["error"], + "no-console": 0 } } \ No newline at end of file diff --git a/README.md b/README.md index eb57027..be1f16b 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ Poloniex Orderbook ================== -[![npm](https://img.shields.io/npm/dm/poloniex-orderbook.svg)](https://www.npmjs.com/package/poloniex-orderbook) -[![GitHub tag](https://img.shields.io/github/tag/evilive3000/poloniex-orderbook.svg)](https://github.com/evilive3000/poloniex-orderbook) -[![GitHub stars](https://img.shields.io/github/stars/evilive3000/poloniex-orderbook.svg?style=social&label=Star)]() +[![npm](https://img.shields.io/npm/dm/@btcc_exchange/poloniex-orderbook.svg)](https://www.npmjs.com/package/@btcc_exchange/poloniex-orderbook) +[![GitHub tag](https://img.shields.io/github/tag/BTCChina/poloniex-orderbook.svg)](https://github.com/BTCChina/poloniex-orderbook) +[![GitHub stars](https://img.shields.io/github/stars/BTCChina/poloniex-orderbook.svg?style=social&label=Star)]() ### !!! Read it !!! > Since August 2017 Poloniex added anti-bot protection for their site and websocket connection which were used @@ -17,122 +17,9 @@ Module for creating and maintaining Poloniex's orderbook on server side. Installation ------------------ ```Shell -$ npm install poloniex-orderbook +$ npm install @btcc_exchange/poloniex-orderbook ``` -Usage ------ -Requires nodejs => 6.0 +## docs -Create file `headers.json` and fill it with your `User-Agent` and `cf_clearance` cookie. -You should grab this information from your browser. -```json -{ - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36", - "Cookie": "cf_clearance=7332b18714d3aa45aed82424e50cf166d6093c6f-1501832791-1800" -} -``` -> As I can suggest this cookie gives you a window in 30 minutes to connects. It's mean you can coonect many times -in this time frame, then you have to renew cookie. When ws-connection is set up you can stop worrying about -renewing because websocket sends cookies only on connection, so I hope it will last until you decide to reconnect. - Yes, it's very inconvenient, but that's all I can offer you now. - -The central part of the lib is PoloManager. This class holds socket connection and PairMarket instances, -and connects them between. - -```javascript -const PoloManager = require('poloniex-orderbook'); -const poloman = new PoloManager().connect({ - headers: require('./headers.json') -}); - -// call connect to initiate socket connection -poloman.connect(); -``` -Now you can set event handlers: -```javascript -poloman.on('change', info => { /* info = {channel, side, rate, amount}*/}); -poloman.on('error', info => { /* info = {msg} */}); -``` - -initiate markets: -```javascript -poloman.market('BTC_ETH'); -``` - -remove markets: -```javascript -poloman.remove('BTC_ETH'); -``` - -get access to markets orderbooks: -```javascript -// take first -poloman.market('BTC_ETH').asks[0]; - -// top 5 -poloman.market('BTC_ETH').bids.slice(0, 5); -``` - -close connection: -```javascript -poloman.disconnect(); -``` - -### Note: - * You HAVE to set Error handler otherwise the script will throw an Error and exit if error event will occur. - (see: [Node.js ErrorEvents](https://nodejs.org/api/events.html#events_error_events)) - * For debug purposes run with `DEBUG=*` variable. - -Example -------- -```javascript -const PoloManager = require('poloniex-orderbook'); -const poloman = new PoloManager().connect({ - headers: require('./headers.json') -}); - -poloman.on('error', err => console.log(err)); - -poloman.on('change', info => { - const {channel, side} = info; - const market = poloman.market(channel); - const top5 = market[side].slice(0, 5); - - console.log(`${side.toUpperCase()} :: ${market.seq}`); - console.log(top5); -}); - -poloman.market('BTC_ETC'); - -// 5 seconds later -setTimeout(() => { - //poloman.disconnect(); -}, 5000); -``` - -you should get the output: -```Shell -ASKS :: 132227424 -[ [ '0.00178668', '412.77676591' ], - [ '0.00178684', '99.99000000' ], - [ '0.00178685', '10.85537516' ], - [ '0.00178700', '0.23521517' ], - [ '0.00179312', '1156.41500000' ] ] -ASKS :: 132227424 -[ [ '0.00178668', '412.77676591' ], - [ '0.00178684', '99.99000000' ], - [ '0.00178685', '10.85537516' ], - [ '0.00178700', '0.23521517' ], - [ '0.00179312', '1156.41500000' ] ] -BIDS :: 132227425 -[ [ '0.00178665', '1028.38378169' ], - [ '0.00178610', '3400.02648465' ], - [ '0.00178600', '177.11520540' ], - [ '0.00178278', '179.50264208' ], - [ '0.00178277', '10.93802113' ] ] -``` - -Contacts --------- -If you have some suggestions please email me: evilive3000@gmail.com +Please see [https://github.com/evilive3000/poloniex-orderbook](https://github.com/evilive3000/poloniex-orderbook) \ No newline at end of file diff --git a/libs/manager.js b/libs/manager.js index 64bdf23..602bc39 100644 --- a/libs/manager.js +++ b/libs/manager.js @@ -59,7 +59,8 @@ class PoloManager extends EventEmitter { this.socket.on('initialize', proxyMethod('initialize')); this.socket.on('order', proxyMethod('order')); - //this.socket.on('history', proxyMethod('history')); + this.socket.on('history', proxyMethod('history')); + this.socket.on('close', this.restartConnection.bind(this)); //this.socket.on('unsubscribed', _.noop) @@ -97,7 +98,7 @@ class PoloManager extends EventEmitter { _.keys(this.markets).forEach(channel => { this.socket.subscribe(channel); - }) + }); } /** diff --git a/libs/market.js b/libs/market.js index 413f93c..52ef651 100644 --- a/libs/market.js +++ b/libs/market.js @@ -25,7 +25,7 @@ class Market { // will be set True if it's valid this.validPair = null; this._initTimeout = setTimeout(() => { - this.setValid(false) + this.setValid(false); }, TIMEOUT * 1000); } @@ -64,6 +64,17 @@ class Market { this._onChange(side, rate, amount); } + history(channel, seq, id, side, rate, amount) { + this.manager.marketEvent(`history`, { + id, + channel, + seq, + side, + rate, + amount + }); + } + /** * * @private @@ -86,11 +97,11 @@ class Market { } get asks() { - return this.orderList[0] + return this.orderList[0]; } get bids() { - return this.orderList[1] + return this.orderList[1]; } /** diff --git a/libs/ws_wrapper.js b/libs/ws_wrapper.js index e5ffc0a..2d7ac6e 100644 --- a/libs/ws_wrapper.js +++ b/libs/ws_wrapper.js @@ -77,7 +77,7 @@ class WsWrapper extends EventEmitter { if (/403/.test(errorEvent.message)) { this.ws.terminate(); - console.log("Poloniex is protected with CloudFlare & reCaptcha.") + console.log("Poloniex is protected with CloudFlare & reCaptcha."); console.log("Please set or check request header information to use this lib."); process.exit(); } @@ -91,8 +91,10 @@ class WsWrapper extends EventEmitter { clearInterval(this._keepAliveInterval); + this.emit('close'); + debug('close', { type, wasClean, reason, code }); - } + }; } /** @@ -153,7 +155,7 @@ class WsWrapper extends EventEmitter { } if (arguments.length === 2 && seq === 0) { - this.emit('unsubscribed', this.channelById[cid]) + this.emit('unsubscribed', this.channelById[cid]); } } } diff --git a/package-lock.json b/package-lock.json index 977bd0d..fa03800 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "poloniex-orderbook", - "version": "3.2.0", + "version": "3.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d0f0f59..aa54286 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "poloniex-orderbook", - "version": "3.2.0", + "name": "@btcc_exchange/poloniex-orderbook", + "version": "3.2.3", "description": "Poloniex's orderbook manager", "main": "index.js", "dependencies": { @@ -12,7 +12,7 @@ "eslint": "^4.3.0" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "eslint ." }, "keywords": [ "poloniex", @@ -23,12 +23,9 @@ "exchange", "arbitrage" ], - "author": "evilive3000", - "license": "ISC", - "url": "https://github.com/evilive3000/poloniex-orderbook/issues", - "email": "evilive3000@gmail.com", + "license": "BSD-2-Clause", "repository": { "type": "git", - "url": "https://github.com/evilive3000/poloniex-orderbook.git" + "url": "git@github.com:BTCChina/poloniex-orderbook.git" } }