* Used for /depth endpoint * @param {object} depth - information * @return {undefined}
(depth)
| 3456 | * @return {undefined} |
| 3457 | */ |
| 3458 | depthHandler(depth) { |
| 3459 | const symbol = depth.s; |
| 3460 | let obj; |
| 3461 | const context = this.depthCacheContext[symbol]; |
| 3462 | const updateDepthCache = () => { |
| 3463 | this.depthCache[symbol].eventTime = depth.E; |
| 3464 | for (obj of depth.b) { //bids |
| 3465 | if (obj[1] == 0) { |
| 3466 | delete this.depthCache[symbol].bids[obj[0]]; |
| 3467 | } else { |
| 3468 | this.depthCache[symbol].bids[obj[0]] = parseFloat(obj[1]); |
| 3469 | } |
| 3470 | } |
| 3471 | for (obj of depth.a) { //asks |
| 3472 | if (obj[1] == 0) { |
| 3473 | delete this.depthCache[symbol].asks[obj[0]]; |
| 3474 | } else { |
| 3475 | this.depthCache[symbol].asks[obj[0]] = parseFloat(obj[1]); |
| 3476 | } |
| 3477 | } |
| 3478 | context.skipCount = 0; |
| 3479 | context.lastEventUpdateId = depth.u; |
| 3480 | context.lastEventUpdateTime = depth.E; |
| 3481 | }; |
| 3482 | |
| 3483 | // This now conforms 100% to the Binance docs constraints on managing a local order book |
| 3484 | if (context.lastEventUpdateId) { |
| 3485 | const expectedUpdateId = context.lastEventUpdateId + 1; |
| 3486 | if (depth.U <= expectedUpdateId) { |
| 3487 | updateDepthCache(); |
| 3488 | } else { |
| 3489 | let msg = 'depthHandler: [' + symbol + '] The depth cache is out of sync.'; |
| 3490 | msg += ' Symptom: Unexpected Update ID. Expected "' + expectedUpdateId + '", got "' + depth.U + '"'; |
| 3491 | if (this.Options.verbose) this.Options.log(msg); |
| 3492 | throw new Error(msg); |
| 3493 | } |
| 3494 | } else if (depth.U > context.snapshotUpdateId + 1) { |
| 3495 | /* In this case we have a gap between the data of the stream and the snapshot. |
| 3496 | This is an out of sync error, and the connection must be torn down and reconnected. */ |
| 3497 | let msg = 'depthHandler: [' + symbol + '] The depth cache is out of sync.'; |
| 3498 | msg += ' Symptom: Gap between snapshot and first stream data.'; |
| 3499 | if (this.Options.verbose) this.Options.log(msg); |
| 3500 | throw new Error(msg); |
| 3501 | } else if (depth.u < context.snapshotUpdateId + 1) { |
| 3502 | /* In this case we've received data that we've already had since the snapshot. |
| 3503 | This isn't really an issue, and we can just update the cache again, or ignore it entirely. */ |
| 3504 | |
| 3505 | // do nothing |
| 3506 | } else { |
| 3507 | // This is our first legal update from the stream data |
| 3508 | updateDepthCache(); |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | /** |
| 3513 | * Gets depth cache for given symbol |
no test coverage detected