fix(orderbook): handle undefined asks and bids from polymarket api - #1
Open
edc3000 wants to merge 1 commit into
Open
fix(orderbook): handle undefined asks and bids from polymarket api#1edc3000 wants to merge 1 commit into
edc3000 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
这个 PR 修复了什么问题?
修复了一个在生成 MyOrderbookSummary 时导致程序闪退的运行时错误:
TypeError: Cannot read properties of undefined (reading 'length')。问题原因
当 Polymarket 的订单簿(OrderBook)API 在极端行情或者是网络瞬间波动时,返回的数据结构中可能偶尔会直接缺失
asks和bids字段,而不是返回预期的空数组[]。这直接导致了代码在执行
raw.asks.length时触发未知类型异常:TypeError: Cannot read properties of undefined (reading 'length') at new MyOrderbookSummary (.../dist/data/orderbook.js:19:22) at Timeout._onTimeout (.../dist/index.js:32:33)从而使得整个终端进程崩溃。
修复方案
raw.asks与raw.bids的length属性前增加了可选安全链(?.)判断。raw.bids与raw.asks进行.reduce合计或通过 calculateVWAP 计算时,增加了默认空数组回退(|| [])。