Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit fc790e0

Browse files
author
Jon Eyrick
authored
Fix for Duplicate Deletion Issue (Thanks davewang!)
Fixes issue: binance-exchange #148 by davewang #148
2 parents 2caa181 + 631dd0f commit fc790e0

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

node-binance-api.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
* ============================================================ */
88

99
/**
10-
* Node Binance Api
10+
* Node Binance API
1111
* @module jaggedsoft/node-binance-api
12+
* @return {object} instance to class object
1213
*/
13-
function Binance() {
14-
if (!(this instanceof Binance)) { return new Binance(); }
14+
let api = function Binance() {
1515
'use strict';
16+
if (!(this instanceof Binance)) {
17+
return new Binance();
18+
}
1619
const WebSocket = require('ws');
1720
const request = require('request');
1821
const crypto = require('crypto');
@@ -22,6 +25,7 @@ function Binance() {
2225
const HttpsProxyAgent = require('https-proxy-agent');
2326
const SocksProxyAgent = require('socks-proxy-agent');
2427
const stringHash = require('string-hash');
28+
const async = require('async');
2529
const base = 'https://api.binance.com/api/';
2630
const wapi = 'https://api.binance.com/wapi/';
2731
const stream = 'wss://stream.binance.com:9443/ws/';
@@ -803,9 +807,9 @@ function Binance() {
803807
* @param {float} float - get the price precision point
804808
* @return {int} - number of place
805809
*/
806-
getPrecision: function(float) { //
807-
return float.toString().split('.')[1].length || 0;
808-
},
810+
getPrecision: function(float) { //
811+
return float.toString().split('.')[1].length || 0;
812+
},
809813

810814
/**
811815
* rounds number with given step
@@ -1663,26 +1667,38 @@ function Binance() {
16631667
}
16641668
};
16651669

1666-
let getSymbolDepthSnapshot = function(symbol) {
1667-
publicRequest(base+'v1/depth', { symbol:symbol, limit:limit }, function(error, json) {
1668-
// Initialize depth cache from snapshot
1669-
depthCache[symbol] = depthData(json);
1670-
// Prepare depth cache context
1671-
let context = depthCacheContext[symbol];
1672-
context.snapshotUpdateId = json.lastUpdateId;
1673-
context.messageQueue = context.messageQueue.filter(depth => depth.u > context.snapshotUpdateId);
1674-
// Process any pending depth messages
1675-
for ( let depth of context.messageQueue ) {
1676-
1677-
/* Although sync errors shouldn't ever happen here, we catch and swallow them anyway
1678-
just in case. The stream handler function above will deal with broken caches. */
1679-
try {depthHandler(depth);} catch (err) {
1680-
// do nothing
1681-
}
1670+
let getSymbolDepthSnapshot = function(symbol,cb){
1671+
1672+
publicRequest(base+'v1/depth', { symbol:symbol, limit:limit }, function(error, json) {
1673+
if (error) {
1674+
return cb(error,null);
1675+
}
1676+
// Store symbol next use
1677+
json.symb = symbol;
1678+
cb(null,json)
1679+
});
1680+
};
1681+
1682+
let updateSymbolDepthCache = function(json){
1683+
// Get previous store symbol
1684+
let symbol = json.symb;
1685+
// Initialize depth cache from snapshot
1686+
depthCache[symbol] = depthData(json);
1687+
// Prepare depth cache context
1688+
let context = depthCacheContext[symbol];
1689+
context.snapshotUpdateId = json.lastUpdateId;
1690+
context.messageQueue = context.messageQueue.filter(depth => depth.u > context.snapshotUpdateId);
1691+
// Process any pending depth messages
1692+
for ( let depth of context.messageQueue ) {
1693+
1694+
/* Although sync errors shouldn't ever happen here, we catch and swallow them anyway
1695+
just in case. The stream handler function above will deal with broken caches. */
1696+
try {depthHandler(depth);} catch (err) {
1697+
// do nothing
16821698
}
1683-
delete context.messageQueue;
1684-
if ( callback ) callback(symbol, depthCache[symbol]);
1685-
});
1699+
}
1700+
delete context.messageQueue;
1701+
if ( callback ) callback(symbol, depthCache[symbol]);
16861702
};
16871703

16881704
/* If an array of symbols are sent we use a combined stream connection rather.
@@ -1697,14 +1713,20 @@ function Binance() {
16971713
return symbol.toLowerCase()+'@depth';
16981714
});
16991715
subscription = subscribeCombined(streams, handleDepthStreamData, reconnect, function() {
1700-
symbols.forEach(getSymbolDepthSnapshot);
1716+
async.mapLimit(symbols, symbols.length, getSymbolDepthSnapshot,(err, results) => {
1717+
if (err) throw err
1718+
results.forEach(updateSymbolDepthCache);
1719+
});
17011720
});
17021721
symbols.forEach(s => assignEndpointIdToContext(s, subscription.endpoint));
17031722
} else {
17041723
let symbol = symbols;
17051724
symbolDepthInit(symbol);
17061725
subscription = subscribe(symbol.toLowerCase()+'@depth', handleDepthStreamData, reconnect, function() {
1707-
getSymbolDepthSnapshot(symbol);
1726+
async.mapLimit([symbol], 1, getSymbolDepthSnapshot,(err, results) => {
1727+
if (err) throw err
1728+
results.forEach(updateSymbolDepthCache);
1729+
});
17081730
});
17091731
assignEndpointIdToContext(symbol, subscription.endpoint);
17101732
}
@@ -1923,6 +1945,6 @@ function Binance() {
19231945
}
19241946
}
19251947
};
1926-
};
1927-
module.exports = Binance;
1948+
}
1949+
module.exports = api;
19281950
//https://github.com/binance-exchange/binance-official-api-docs

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "node-binance-api",
3-
"version": "0.7.0",
3+
"version": "0.7.2",
44
"description": "Binance API for node https://github.com/jaggedsoft/node-binance-api",
55
"main": "node-binance-api.js",
66
"dependencies": {
7+
"async": "^2.6.1",
78
"dns-sync": "^0.1.3",
89
"fs": "0.0.1-security",
910
"https-proxy-agent": "^2.2.1",

0 commit comments

Comments
 (0)