MCPcopy Create free account
hub / github.com/ccxt/node-binance-api / depthHandler

Function depthHandler

deprecated/node-binance-api.js:2529–2580  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2527 * @return {undefined}
2528 */
2529 const depthHandler = depth => {
2530 let symbol = depth.s, obj;
2531 let context = Binance.depthCacheContext[symbol];
2532 let updateDepthCache = () => {
2533 Binance.depthCache[symbol].eventTime = depth.E;
2534 for (obj of depth.b) { //bids
2535 if (obj[1] == 0) {
2536 delete Binance.depthCache[symbol].bids[obj[0]];
2537 } else {
2538 Binance.depthCache[symbol].bids[obj[0]] = parseFloat(obj[1]);
2539 }
2540 }
2541 for (obj of depth.a) { //asks
2542 if (obj[1] == 0) {
2543 delete Binance.depthCache[symbol].asks[obj[0]];
2544 } else {
2545 Binance.depthCache[symbol].asks[obj[0]] = parseFloat(obj[1]);
2546 }
2547 }
2548 context.skipCount = 0;
2549 context.lastEventUpdateId = depth.u;
2550 context.lastEventUpdateTime = depth.E;
2551 };
2552
2553 // This now conforms 100% to the Binance docs constraints on managing a local order book
2554 if (context.lastEventUpdateId) {
2555 const expectedUpdateId = context.lastEventUpdateId + 1;
2556 if (depth.U <= expectedUpdateId) {
2557 updateDepthCache();
2558 } else {
2559 let msg = 'depthHandler: [' + symbol + '] The depth cache is out of sync.';
2560 msg += ' Symptom: Unexpected Update ID. Expected "' + expectedUpdateId + '", got "' + depth.U + '"';
2561 if (Binance.options.verbose) Binance.options.log(msg);
2562 throw new Error(msg);
2563 }
2564 } else if (depth.U > context.snapshotUpdateId + 1) {
2565 /* In this case we have a gap between the data of the stream and the snapshot.
2566 This is an out of sync error, and the connection must be torn down and reconnected. */
2567 let msg = 'depthHandler: [' + symbol + '] The depth cache is out of sync.';
2568 msg += ' Symptom: Gap between snapshot and first stream data.';
2569 if (Binance.options.verbose) Binance.options.log(msg);
2570 throw new Error(msg);
2571 } else if (depth.u < context.snapshotUpdateId + 1) {
2572 /* In this case we've received data that we've already had since the snapshot.
2573 This isn't really an issue, and we can just update the cache again, or ignore it entirely. */
2574
2575 // do nothing
2576 } else {
2577 // This is our first legal update from the stream data
2578 updateDepthCache();
2579 }
2580 };
2581
2582 /**
2583 * Gets depth cache for given symbol

Callers 2

handleDepthStreamDataFunction · 0.85
updateSymbolDepthCacheFunction · 0.85

Calls 2

updateDepthCacheFunction · 0.85
logMethod · 0.80

Tested by

no test coverage detected