binance-api-node
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -29,3 +29,3 @@ 'use strict'; | ||
var depth = function depth(payload, cb) { | ||
return (Array.isArray(payload) ? payload : [payload]).forEach(function (symbol) { | ||
var cache = (Array.isArray(payload) ? payload : [payload]).forEach(function (symbol) { | ||
var w = new _ws2.default(BASE + '/' + symbol.toLowerCase() + '@depth'); | ||
@@ -55,4 +55,43 @@ w.on('message', function (msg) { | ||
}); | ||
return function () { | ||
return cache.forEach(function (w) { | ||
return w.close(); | ||
}); | ||
}; | ||
}; | ||
var partialDepth = function partialDepth(payload, cb) { | ||
var cache = (Array.isArray(payload) ? payload : [payload]).map(function (_ref) { | ||
var symbol = _ref.symbol, | ||
level = _ref.level; | ||
var w = new _ws2.default(BASE + '/' + symbol.toLowerCase() + '@depth' + level); | ||
w.on('message', function (msg) { | ||
var _JSON$parse2 = JSON.parse(msg), | ||
lastUpdateId = _JSON$parse2.lastUpdateId, | ||
bids = _JSON$parse2.bids, | ||
asks = _JSON$parse2.asks; | ||
cb({ | ||
lastUpdateId: lastUpdateId, | ||
bids: bids.map(function (b) { | ||
return (0, _lodash2.default)(['price', 'quantity'], b); | ||
}), | ||
asks: asks.map(function (a) { | ||
return (0, _lodash2.default)(['price', 'quantity'], a); | ||
}) | ||
}); | ||
}); | ||
return w; | ||
}); | ||
return function () { | ||
return cache.forEach(function (w) { | ||
return w.close(); | ||
}); | ||
}; | ||
}; | ||
var candles = function candles(payload, interval, cb) { | ||
@@ -63,10 +102,10 @@ if (!interval || !cb) { | ||
;(Array.isArray(payload) ? payload : [payload]).forEach(function (symbol) { | ||
var cache = (Array.isArray(payload) ? payload : [payload]).map(function (symbol) { | ||
var w = new _ws2.default(BASE + '/' + symbol.toLowerCase() + '@kline_' + interval); | ||
w.on('message', function (msg) { | ||
var _JSON$parse2 = JSON.parse(msg), | ||
eventType = _JSON$parse2.e, | ||
eventTime = _JSON$parse2.E, | ||
symbol = _JSON$parse2.s, | ||
tick = _JSON$parse2.k; | ||
var _JSON$parse3 = JSON.parse(msg), | ||
eventType = _JSON$parse3.e, | ||
eventTime = _JSON$parse3.E, | ||
symbol = _JSON$parse3.s, | ||
tick = _JSON$parse3.k; | ||
@@ -103,17 +142,86 @@ var open = tick.o, | ||
}); | ||
return w; | ||
}); | ||
return function () { | ||
return cache.forEach(function (w) { | ||
return w.close(); | ||
}); | ||
}; | ||
}; | ||
var tickerTransform = function tickerTransform(m) { | ||
return { | ||
eventType: m.e, | ||
eventTime: m.E, | ||
symbol: m.s, | ||
priceChange: m.p, | ||
priceChangePercent: m.P, | ||
weightedAvg: m.w, | ||
prevDayClose: m.x, | ||
curDayClose: m.c, | ||
closeTradeQuantity: m.Q, | ||
bestBid: m.b, | ||
bestBidQnt: m.B, | ||
bestAsk: m.a, | ||
bestAskQnt: m.A, | ||
open: m.o, | ||
high: m.h, | ||
low: m.l, | ||
volume: m.v, | ||
volumeQuote: m.q, | ||
openTime: m.O, | ||
closeTime: m.C, | ||
firstTradeId: m.F, | ||
lastTradeId: m.L, | ||
totalTrades: m.n | ||
}; | ||
}; | ||
var ticker = function ticker(payload, cb) { | ||
var cache = (Array.isArray(payload) ? payload : [payload]).map(function (symbol) { | ||
var w = new _ws2.default(BASE + '/' + symbol.toLowerCase() + '@ticker'); | ||
w.on('message', function (msg) { | ||
cb(tickerTransform(JSON.parse(msg))); | ||
}); | ||
return w; | ||
}); | ||
return function () { | ||
return cache.forEach(function (w) { | ||
return w.close(); | ||
}); | ||
}; | ||
}; | ||
var allTickers = function allTickers(cb) { | ||
var w = new _ws2.default(BASE + '/!ticker@arr'); | ||
w.on('message', function (msg) { | ||
var arr = JSON.parse(msg); | ||
cb(arr.map(function (m) { | ||
return tickerTransform(m); | ||
})); | ||
}); | ||
return function () { | ||
return w.close(); | ||
}; | ||
}; | ||
var trades = function trades(payload, cb) { | ||
return (Array.isArray(payload) ? payload : [payload]).forEach(function (symbol) { | ||
var cache = (Array.isArray(payload) ? payload : [payload]).map(function (symbol) { | ||
var w = new _ws2.default(BASE + '/' + symbol.toLowerCase() + '@aggTrade'); | ||
w.on('message', function (msg) { | ||
var _JSON$parse3 = JSON.parse(msg), | ||
eventType = _JSON$parse3.e, | ||
eventTime = _JSON$parse3.E, | ||
symbol = _JSON$parse3.s, | ||
price = _JSON$parse3.p, | ||
quantity = _JSON$parse3.q, | ||
maker = _JSON$parse3.m, | ||
tradeId = _JSON$parse3.a; | ||
var _JSON$parse4 = JSON.parse(msg), | ||
eventType = _JSON$parse4.e, | ||
eventTime = _JSON$parse4.E, | ||
symbol = _JSON$parse4.s, | ||
price = _JSON$parse4.p, | ||
quantity = _JSON$parse4.q, | ||
maker = _JSON$parse4.m, | ||
tradeId = _JSON$parse4.a; | ||
@@ -130,3 +238,11 @@ cb({ | ||
}); | ||
return w; | ||
}); | ||
return function () { | ||
return cache.forEach(function (w) { | ||
return w.close(); | ||
}); | ||
}; | ||
}; | ||
@@ -174,5 +290,5 @@ | ||
return function (msg) { | ||
var _JSON$parse4 = JSON.parse(msg), | ||
type = _JSON$parse4.e, | ||
rest = _objectWithoutProperties(_JSON$parse4, ['e']); | ||
var _JSON$parse5 = JSON.parse(msg), | ||
type = _JSON$parse5.e, | ||
rest = _objectWithoutProperties(_JSON$parse5, ['e']); | ||
@@ -197,4 +313,4 @@ cb(userTransforms[type] ? userTransforms[type](rest) : _extends({ type: type }, rest)); | ||
return getDataStream().then(function (_ref) { | ||
var listenKey = _ref.listenKey; | ||
return getDataStream().then(function (_ref2) { | ||
var listenKey = _ref2.listenKey; | ||
@@ -210,2 +326,3 @@ var w = new _ws2.default(BASE + '/' + listenKey); | ||
closeDataStream({ listenKey: listenKey }); | ||
w.close(); | ||
}; | ||
@@ -219,6 +336,9 @@ }); | ||
depth: depth, | ||
partialDepth: partialDepth, | ||
candles: candles, | ||
trades: trades, | ||
ticker: ticker, | ||
allTickers: allTickers, | ||
user: user(opts) | ||
}; | ||
}; |
{ | ||
"name": "binance-api-node", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A node API wrapper for Binance", | ||
@@ -5,0 +5,0 @@ "main": "dist", |
119
README.md
@@ -1,2 +0,2 @@ | ||
# binance-api-node [![Build](https://img.shields.io/travis/HyperCubeProject/binance-api-node.svg?style=flat-square)](https://travis-ci.org/HyperCubeProject/binance-api-node) [![Coverage](https://img.shields.io/coveralls/HyperCubeProject/binance-api-node.svg?style=flat-square)](https://coveralls.io/github/HyperCubeProject/binance-api-node) [![bnb](https://img.shields.io/badge/binance-winner-yellow.svg?style=flat-square)](https://github.com/binance-exchange/binance-api-node) | ||
# binance-api-node [![build](https://img.shields.io/travis/HyperCubeProject/binance-api-node.svg?style=flat-square)](https://travis-ci.org/HyperCubeProject/binance-api-node) [![cover](https://img.shields.io/coveralls/HyperCubeProject/binance-api-node.svg?style=flat-square)](https://coveralls.io/github/HyperCubeProject/binance-api-node) [![bnb](https://img.shields.io/badge/binance-winner-yellow.svg?style=flat-square)](https://github.com/binance-exchange/binance-api-node) | ||
@@ -61,2 +61,5 @@ > A complete API wrapper for the [Binance](https://binance.com) API. | ||
- [depth](#depth) | ||
- [partialDepth](#partialdepth) | ||
- [ticker](#ticker) | ||
- [allTickers](#alltickers) | ||
- [candles](#candles-1) | ||
@@ -304,6 +307,6 @@ - [trades](#trades) | ||
console.log(await client.order({ | ||
symbol: 'ETHBTC', | ||
symbol: 'XLMETH', | ||
side: 'BUY', | ||
quantity: 1, | ||
price: 1, | ||
quantity: 100, | ||
price: 0.0002, | ||
})) | ||
@@ -329,2 +332,15 @@ ``` | ||
```js | ||
{ | ||
symbol: 'XLMETH', | ||
orderId: 1740797, | ||
clientOrderId: '1XZTVBTGS4K1e', | ||
transactTime: 1514418413947, | ||
price: '0.00020000', | ||
origQty: '100.00000000', | ||
executedQty: '0.00000000', | ||
status: 'NEW', | ||
timeInForce: 'GTC', | ||
type: 'LIMIT', | ||
side: 'BUY' | ||
} | ||
``` | ||
@@ -672,2 +688,14 @@ | ||
Every websocket utility returns a function you can call to close the opened | ||
connection and avoid memory issues. | ||
```js | ||
const clean = client.ws.depth('ETHBTC', depth => { | ||
console.log(depth) | ||
}) | ||
// After you're done | ||
clean() | ||
``` | ||
#### depth | ||
@@ -706,2 +734,85 @@ | ||
#### partialDepth | ||
Top levels bids and asks, pushed every second. Valid levels are 5, 10, or 20. | ||
Accepts an array of objects for multiple depths. | ||
```js | ||
client.ws.partialDepth({ symbol: 'ETHBTC', level: 10 }, depth => { | ||
console.log(depth) | ||
}) | ||
``` | ||
<details> | ||
<summary>Output</summary> | ||
```js | ||
{ | ||
bids: [ | ||
{ price: '0.04896500', quantity: '0.00000000' }, | ||
{ price: '0.04891100', quantity: '15.00000000' }, | ||
{ price: '0.04891000', quantity: '0.00000000' } | ||
], | ||
asks: [ | ||
{ price: '0.04910600', quantity: '0.00000000' }, | ||
{ price: '0.04910700', quantity: '11.24900000' } | ||
] | ||
} | ||
``` | ||
</details> | ||
#### ticker | ||
24hr Ticker statistics for a symbol pushed every second. Accepts an array of symbols. | ||
```js | ||
client.ws.ticker('HSRETH', ticker => { | ||
console.log(ticker) | ||
}) | ||
``` | ||
<details> | ||
<summary>Output</summary> | ||
```js | ||
{ | ||
eventType: '24hrTicker', | ||
eventTime: 1514670820924, | ||
symbol: 'HSRETH', | ||
priceChange: '-0.00409700', | ||
priceChangePercent: '-11.307', | ||
weightedAvg: '0.03394946', | ||
prevDayClose: '0.03623500', | ||
curDayClose: '0.03213800', | ||
closeTradeQuantity: '7.02000000', | ||
bestBid: '0.03204200', | ||
bestBidQnt: '78.00000000', | ||
bestAsk: '0.03239800', | ||
bestAskQnt: '7.00000000', | ||
open: '0.03623500', | ||
high: '0.03659900', | ||
low: '0.03126000', | ||
volume: '100605.15000000', | ||
volumeQuote: '3415.49097353', | ||
openTime: 1514584420922, | ||
closeTime: 1514670820922, | ||
firstTradeId: 344803, | ||
lastTradeId: 351380, | ||
totalTrades: 6578 | ||
} | ||
``` | ||
</details> | ||
#### allTickers | ||
Retrieves all the tickers. | ||
```js | ||
client.ws.partialDepth(tickers => { | ||
console.log(tickers) | ||
}) | ||
``` | ||
#### candles | ||
@@ -708,0 +819,0 @@ |
@@ -8,4 +8,4 @@ import WebSocket from 'ws' | ||
const depth = (payload, cb) => | ||
(Array.isArray(payload) ? payload : [payload]).forEach(symbol => { | ||
const depth = (payload, cb) => { | ||
const cache = (Array.isArray(payload) ? payload : [payload]).forEach(symbol => { | ||
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@depth`) | ||
@@ -33,2 +33,23 @@ w.on('message', msg => { | ||
return () => cache.forEach(w => w.close()) | ||
} | ||
const partialDepth = (payload, cb) => { | ||
const cache = (Array.isArray(payload) ? payload : [payload]).map(({ symbol, level }) => { | ||
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@depth${level}`) | ||
w.on('message', msg => { | ||
const { lastUpdateId, bids, asks } = JSON.parse(msg) | ||
cb({ | ||
lastUpdateId, | ||
bids: bids.map(b => zip(['price', 'quantity'], b)), | ||
asks: asks.map(a => zip(['price', 'quantity'], a)), | ||
}) | ||
}) | ||
return w | ||
}) | ||
return () => cache.forEach(w => w.close()) | ||
} | ||
const candles = (payload, interval, cb) => { | ||
@@ -39,3 +60,3 @@ if (!interval || !cb) { | ||
;(Array.isArray(payload) ? payload : [payload]).forEach(symbol => { | ||
const cache = (Array.isArray(payload) ? payload : [payload]).map(symbol => { | ||
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@kline_${interval}`) | ||
@@ -75,7 +96,62 @@ w.on('message', msg => { | ||
}) | ||
return w | ||
}) | ||
return () => cache.forEach(w => w.close()) | ||
} | ||
const trades = (payload, cb) => | ||
(Array.isArray(payload) ? payload : [payload]).forEach(symbol => { | ||
const tickerTransform = m => ({ | ||
eventType: m.e, | ||
eventTime: m.E, | ||
symbol: m.s, | ||
priceChange: m.p, | ||
priceChangePercent: m.P, | ||
weightedAvg: m.w, | ||
prevDayClose: m.x, | ||
curDayClose: m.c, | ||
closeTradeQuantity: m.Q, | ||
bestBid: m.b, | ||
bestBidQnt: m.B, | ||
bestAsk: m.a, | ||
bestAskQnt: m.A, | ||
open: m.o, | ||
high: m.h, | ||
low: m.l, | ||
volume: m.v, | ||
volumeQuote: m.q, | ||
openTime: m.O, | ||
closeTime: m.C, | ||
firstTradeId: m.F, | ||
lastTradeId: m.L, | ||
totalTrades: m.n, | ||
}) | ||
const ticker = (payload, cb) => { | ||
const cache = (Array.isArray(payload) ? payload : [payload]).map(symbol => { | ||
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@ticker`) | ||
w.on('message', msg => { | ||
cb(tickerTransform(JSON.parse(msg))) | ||
}) | ||
return w | ||
}) | ||
return () => cache.forEach(w => w.close()) | ||
} | ||
const allTickers = cb => { | ||
const w = new WebSocket(`${BASE}/!ticker@arr`) | ||
w.on('message', msg => { | ||
const arr = JSON.parse(msg) | ||
cb(arr.map(m => tickerTransform(m))) | ||
}) | ||
return () => w.close() | ||
} | ||
const trades = (payload, cb) => { | ||
const cache = (Array.isArray(payload) ? payload : [payload]).map(symbol => { | ||
const w = new WebSocket(`${BASE}/${symbol.toLowerCase()}@aggTrade`) | ||
@@ -103,4 +179,9 @@ w.on('message', msg => { | ||
}) | ||
return w | ||
}) | ||
return () => cache.forEach(w => w.close()) | ||
} | ||
const userTransforms = { | ||
@@ -160,2 +241,3 @@ outboundAccountInfo: m => ({ | ||
closeDataStream({ listenKey }) | ||
w.close() | ||
} | ||
@@ -167,5 +249,8 @@ }) | ||
depth, | ||
partialDepth, | ||
candles, | ||
trades, | ||
ticker, | ||
allTickers, | ||
user: user(opts), | ||
}) |
@@ -120,3 +120,2 @@ import test from 'ava' | ||
t.true(typeof clean === 'function') | ||
clean() | ||
}) |
@@ -110,3 +110,2 @@ import test from 'ava' | ||
client.ws.depth('ETHBTC', depth => { | ||
t.is(depth, depth, 'ETHBTC') | ||
checkFields(t, depth, [ | ||
@@ -125,2 +124,31 @@ 'eventType', | ||
test.serial('[WS] partial depth', t => { | ||
return new Promise(resolve => { | ||
client.ws.partialDepth({ symbol: 'ETHBTC', level: 10 }, depth => { | ||
checkFields(t, depth, ['lastUpdateId', 'bids', 'asks']) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
test.serial('[WS] ticker', t => { | ||
return new Promise(resolve => { | ||
client.ws.ticker('ETHBTC', ticker => { | ||
checkFields(t, ticker, ['open', 'high', 'low', 'eventTime', 'symbol', 'volume']) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
test.serial('[WS] allTicker', t => { | ||
return new Promise(resolve => { | ||
client.ws.allTickers(tickers => { | ||
t.truthy(Array.isArray(tickers)) | ||
t.is(tickers[0].eventType, '24hrTicker') | ||
checkFields(t, tickers[0], ['symbol', 'priceChange', 'priceChangePercent']) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
test.serial('[WS] candles', t => { | ||
@@ -127,0 +155,0 @@ try { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
212625
1358
906