From c5da11eb962bdcdaec21d3f982e50e80a5de1583 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 14:48:57 +0800 Subject: [PATCH 01/11] support on history event --- libs/manager.js | 2 +- libs/market.js | 11 +++++++++++ libs/ws_wrapper.js | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libs/manager.js b/libs/manager.js index 64bdf23..e853467 100644 --- a/libs/manager.js +++ b/libs/manager.js @@ -59,7 +59,7 @@ 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('unsubscribed', _.noop) diff --git a/libs/market.js b/libs/market.js index 413f93c..d8faa70 100644 --- a/libs/market.js +++ b/libs/market.js @@ -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 diff --git a/libs/ws_wrapper.js b/libs/ws_wrapper.js index e5ffc0a..ed256ce 100644 --- a/libs/ws_wrapper.js +++ b/libs/ws_wrapper.js @@ -148,6 +148,9 @@ class WsWrapper extends EventEmitter { if (char == 'i') { this.channelById[cid] = data[0].currencyPair; } + if (char2event[char] === 'history') { + console.log('--------------------', this.channelById[cid], seq, data) + } this.emit(char2event[char], this.channelById[cid], seq, ...data); } } From c744489637654041c043cb3ebf5821513faa9c80 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 14:50:47 +0800 Subject: [PATCH 02/11] no console log --- libs/ws_wrapper.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/libs/ws_wrapper.js b/libs/ws_wrapper.js index ed256ce..e5ffc0a 100644 --- a/libs/ws_wrapper.js +++ b/libs/ws_wrapper.js @@ -148,9 +148,6 @@ class WsWrapper extends EventEmitter { if (char == 'i') { this.channelById[cid] = data[0].currencyPair; } - if (char2event[char] === 'history') { - console.log('--------------------', this.channelById[cid], seq, data) - } this.emit(char2event[char], this.channelById[cid], seq, ...data); } } From 6dc20e51fb2b8a1f4b740f31c26c73a8be1f29d9 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:35:46 +0800 Subject: [PATCH 03/11] make libs ourselves --- .eslintrc.json | 3 ++- libs/manager.js | 2 +- libs/market.js | 6 +++--- libs/ws_wrapper.js | 6 +++--- package.json | 12 +++++------- 5 files changed, 14 insertions(+), 15 deletions(-) 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/libs/manager.js b/libs/manager.js index e853467..377c65b 100644 --- a/libs/manager.js +++ b/libs/manager.js @@ -97,7 +97,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 d8faa70..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); } @@ -97,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..98eeced 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(); } @@ -92,7 +92,7 @@ class WsWrapper extends EventEmitter { clearInterval(this._keepAliveInterval); debug('close', { type, wasClean, reason, code }); - } + }; } /** @@ -153,7 +153,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.json b/package.json index d0f0f59..b494553 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "poloniex-orderbook", + "name": "@btcc_exchange/poloniex-orderbook", "version": "3.2.0", "description": "Poloniex's orderbook manager", "main": "index.js", @@ -12,7 +12,7 @@ "eslint": "^4.3.0" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "eslint ." }, "keywords": [ "poloniex", @@ -23,12 +23,10 @@ "exchange", "arbitrage" ], - "author": "evilive3000", "license": "ISC", - "url": "https://github.com/evilive3000/poloniex-orderbook/issues", - "email": "evilive3000@gmail.com", + "url": "git@github.com:BTCChina/poloniex-orderbook/issues", "repository": { "type": "git", - "url": "https://github.com/evilive3000/poloniex-orderbook.git" + "url": "git@github.com:BTCChina/poloniex-orderbook.git" } -} +} \ No newline at end of file From e5f109ed41e3ab8ea9d1e93d1f81e08feeb281ef Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:38:41 +0800 Subject: [PATCH 04/11] Release v3.2.1 --- package-lock.json | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 977bd0d..58d5d57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "poloniex-orderbook", - "version": "3.2.0", + "version": "3.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b494553..6d77e0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@btcc_exchange/poloniex-orderbook", - "version": "3.2.0", + "version": "3.2.1", "description": "Poloniex's orderbook manager", "main": "index.js", "dependencies": { @@ -29,4 +29,4 @@ "type": "git", "url": "git@github.com:BTCChina/poloniex-orderbook.git" } -} \ No newline at end of file +} From e9731e764a47be6638736719005e95061c852b6f Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:40:24 +0800 Subject: [PATCH 05/11] change liences --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6d77e0f..11ad8d2 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,10 @@ "exchange", "arbitrage" ], - "license": "ISC", + "license": "BSD-2-Clause", "url": "git@github.com:BTCChina/poloniex-orderbook/issues", "repository": { "type": "git", "url": "git@github.com:BTCChina/poloniex-orderbook.git" } -} +} \ No newline at end of file From d585828bb15d39cfe91dba25440be77884758acb Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:48:17 +0800 Subject: [PATCH 06/11] backup --- README.md | 10 +++++----- package.json | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eb57027..2c89e54 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,7 +17,7 @@ Module for creating and maintaining Poloniex's orderbook on server side. Installation ------------------ ```Shell -$ npm install poloniex-orderbook +$ npm install @btcc_exchange/poloniex-orderbook ``` Usage @@ -135,4 +135,4 @@ BIDS :: 132227425 Contacts -------- -If you have some suggestions please email me: evilive3000@gmail.com +If you have some suggestions please email me: evilive3000@gmail.com \ No newline at end of file diff --git a/package.json b/package.json index 11ad8d2..74fa25b 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "arbitrage" ], "license": "BSD-2-Clause", - "url": "git@github.com:BTCChina/poloniex-orderbook/issues", "repository": { "type": "git", "url": "git@github.com:BTCChina/poloniex-orderbook.git" From 75f5ae247275a933208a767a46980431bafa7aa7 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:57:54 +0800 Subject: [PATCH 07/11] update readme --- README.md | 117 +----------------------------------------------------- 1 file changed, 2 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 2c89e54..be1f16b 100644 --- a/README.md +++ b/README.md @@ -20,119 +20,6 @@ Installation $ 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 \ No newline at end of file +Please see [https://github.com/evilive3000/poloniex-orderbook](https://github.com/evilive3000/poloniex-orderbook) \ No newline at end of file From 62ed415ee272c46b1dc789f7b62393a2a1aa6263 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Wed, 6 Sep 2017 15:58:06 +0800 Subject: [PATCH 08/11] Release v3.2.2 --- package-lock.json | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 58d5d57..c7f358a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "poloniex-orderbook", - "version": "3.2.1", + "version": "3.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 74fa25b..e5da890 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@btcc_exchange/poloniex-orderbook", - "version": "3.2.1", + "version": "3.2.2", "description": "Poloniex's orderbook manager", "main": "index.js", "dependencies": { @@ -28,4 +28,4 @@ "type": "git", "url": "git@github.com:BTCChina/poloniex-orderbook.git" } -} \ No newline at end of file +} From 78a3a998be0fd5877adc87272e74ab2ff6424786 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Thu, 7 Sep 2017 11:17:51 +0800 Subject: [PATCH 09/11] reconnect on close --- libs/manager.js | 1 + libs/ws_wrapper.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/libs/manager.js b/libs/manager.js index 377c65b..602bc39 100644 --- a/libs/manager.js +++ b/libs/manager.js @@ -60,6 +60,7 @@ 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('close', this.restartConnection.bind(this)); //this.socket.on('unsubscribed', _.noop) diff --git a/libs/ws_wrapper.js b/libs/ws_wrapper.js index 98eeced..45b4b1b 100644 --- a/libs/ws_wrapper.js +++ b/libs/ws_wrapper.js @@ -91,6 +91,8 @@ class WsWrapper extends EventEmitter { clearInterval(this._keepAliveInterval); + this.emit('close') + debug('close', { type, wasClean, reason, code }); }; } From f8819d8c770f782ddca3269637a919de30868971 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Thu, 7 Sep 2017 11:18:11 +0800 Subject: [PATCH 10/11] fix eslint --- libs/ws_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/ws_wrapper.js b/libs/ws_wrapper.js index 45b4b1b..2d7ac6e 100644 --- a/libs/ws_wrapper.js +++ b/libs/ws_wrapper.js @@ -91,7 +91,7 @@ class WsWrapper extends EventEmitter { clearInterval(this._keepAliveInterval); - this.emit('close') + this.emit('close'); debug('close', { type, wasClean, reason, code }); }; From 818c9c122bcdf3b03db4eb25153d0079dc9f3fc7 Mon Sep 17 00:00:00 2001 From: zcong1993 <1005997919@qq.com> Date: Thu, 7 Sep 2017 11:18:15 +0800 Subject: [PATCH 11/11] Release v3.2.3 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index c7f358a..fa03800 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "poloniex-orderbook", - "version": "3.2.2", + "version": "3.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index e5da890..aa54286 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@btcc_exchange/poloniex-orderbook", - "version": "3.2.2", + "version": "3.2.3", "description": "Poloniex's orderbook manager", "main": "index.js", "dependencies": {