From 63ddfb41d50e1e5e331c0b46d577e03978b9887e Mon Sep 17 00:00:00 2001 From: Scripnic Alexandru Date: Tue, 12 Jun 2018 17:02:18 +0300 Subject: [PATCH] Add bulkDepth functionality --- README.md | 37 +++++++++++++++++++++++++++++++++++++ src/websocket.js | 33 +++++++++++++++++++++++++++++++++ test/index.js | 20 ++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/README.md b/README.md index c3c6fc33..28b3eefb 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Following examples will use the `await` form, which requires some configuration - [depositAddress](#depositaddress) - [Websockets](#websockets) - [depth](#depth) + - [bulkDepth](#bulkdepth) - [partialDepth](#partialdepth) - [ticker](#ticker) - [allTickers](#alltickers) @@ -910,6 +911,42 @@ client.ws.depth('ETHBTC', depth => { +#### bulkDepth + +Live depth market data feed. The first parameter can either +be a single symbol string or an array of symbols. + +This method creates only one connection, that prevents from overpopulating the server. + +```js +client.ws.bulkDepth('ETHBTC', depth => { + console.log(depth); +}) +``` + +
+Output + +```js +{ + eventType: 'depthUpdate', + eventTime: 1508612956950, + symbol: 'ETHBTC', + firstUpdateId: 18331140, + finalUpdateId: 18331145, + bidDepth: [ + { price: '0.04896500', quantity: '0.00000000' }, + { price: '0.04891100', quantity: '15.00000000' }, + { price: '0.04891000', quantity: '0.00000000' } ], + askDepth: [ + { price: '0.04910600', quantity: '0.00000000' }, + { price: '0.04910700', quantity: '11.24900000' } + ] +} +``` + +
+ #### partialDepth Top levels bids and asks, pushed every second. Valid levels are 5, 10, or 20. diff --git a/src/websocket.js b/src/websocket.js index b8e149ff..25e79bea 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -36,6 +36,38 @@ const depth = (payload, cb) => { return (options) => cache.forEach(w => w.close(1000, 'Close handle was called', { keepClosed: true, ...options })) } +const bulkDepth = (payload, cb) => { + const _BASE = BASE.replace(/ws$/gi, 'stream'); + const streams = (Array.isArray(payload) ? payload : [payload]) + .map(symbol => `${symbol}@depth`) + .join('/'); + + const w = openWebSocket(`${_BASE}?streams=${streams.toLowerCase()}`); + w.onmessage = msg => { + const { + e: eventType, + E: eventTime, + s: symbol, + U: firstUpdateId, + u: finalUpdateId, + b: bidDepth, + a: askDepth, + } = JSON.parse(msg.data).data + + cb({ + eventType, + eventTime, + symbol, + firstUpdateId, + finalUpdateId, + bidDepth: bidDepth.map(b => zip(['price', 'quantity'], b)), + askDepth: askDepth.map(a => zip(['price', 'quantity'], a)), + }); + } + + return (options) => w.close(1000, 'Close handle was called', { keepClosed: true, ...options }); +} + const partialDepth = (payload, cb) => { const cache = (Array.isArray(payload) ? payload : [payload]).map(({ symbol, level }) => { const w = openWebSocket(`${BASE}/${symbol.toLowerCase()}@depth${level}`) @@ -272,6 +304,7 @@ const user = opts => cb => { export default opts => ({ depth, + bulkDepth, partialDepth, candles, trades, diff --git a/test/index.js b/test/index.js index d871ffb7..1d4e0f30 100644 --- a/test/index.js +++ b/test/index.js @@ -130,6 +130,26 @@ test('[WS] depth', t => { }) }) +test('[WS] bulk depth', t => { + return new Promise(resolve => { + const symbols = ['ETHBTC', 'LTCBTC']; + const connection = client.ws.bulkDepth(symbols, depth => { + checkFields(t, depth, [ + 'eventType', + 'eventTime', + 'firstUpdateId', + 'finalUpdateId', + 'symbol', + 'bidDepth', + 'askDepth', + ]); + + connection(); + resolve(); + }); + }); +}); + test('[WS] partial depth', t => { return new Promise(resolve => { client.ws.partialDepth({ symbol: 'ETHBTC', level: 10 }, depth => {