Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support on history event #14

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"rules": {
"semi": ["error", "always", {"omitLastInOneLineBlock": true}],
"no-var": ["error"],
"prefer-const": ["error"]
"prefer-const": ["error"],
"no-console": 0
}
}
125 changes: 6 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
5 changes: 3 additions & 2 deletions libs/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -97,7 +98,7 @@ class PoloManager extends EventEmitter {

_.keys(this.markets).forEach(channel => {
this.socket.subscribe(channel);
})
});
}

/**
Expand Down
17 changes: 14 additions & 3 deletions libs/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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];
}

/**
Expand Down
8 changes: 5 additions & 3 deletions libs/ws_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -91,8 +91,10 @@ class WsWrapper extends EventEmitter {

clearInterval(this._keepAliveInterval);

this.emit('close');

debug('close', { type, wasClean, reason, code });
}
};
}

/**
Expand Down Expand Up @@ -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]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -12,7 +12,7 @@
"eslint": "^4.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "eslint ."
},
"keywords": [
"poloniex",
Expand All @@ -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"
}
}