From aa36b0cfdc84b6c4cbe1b9d500d658af2094005b Mon Sep 17 00:00:00 2001 From: discountry Date: Sun, 21 Dec 2025 16:15:44 +0800 Subject: [PATCH] Implement depth level normalization in StandxGateway for improved order book handling. Refactor depth data processing to utilize the new normalizeDepthLevels function, ensuring consistent bid and ask sorting. Update symbol handling to accommodate fallback logic. --- src/exchanges/standx/gateway.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/exchanges/standx/gateway.ts b/src/exchanges/standx/gateway.ts index d7e51e6..c17dd82 100644 --- a/src/exchanges/standx/gateway.ts +++ b/src/exchanges/standx/gateway.ts @@ -231,6 +231,16 @@ function extractOrders(payload: unknown): StandxOrder[] { return []; } +function normalizeDepthLevels(levels: [string, string][], side: "bid" | "ask"): [string, string][] { + const sorted = [...levels].sort((a, b) => { + const priceA = Number(a?.[0] ?? 0); + const priceB = Number(b?.[0] ?? 0); + if (!Number.isFinite(priceA) || !Number.isFinite(priceB)) return 0; + return side === "bid" ? priceB - priceA : priceA - priceB; + }); + return sorted; +} + export class StandxGateway { private readonly token: string; private readonly baseUrl: string; @@ -695,15 +705,18 @@ export class StandxGateway { } if (channel === "depth_book") { const data = message.data as StandxDepthBook | undefined; - if (!data?.symbol) return; + const rawSymbol = data?.symbol ?? message?.symbol; + if (!rawSymbol) return; + const bids = normalizeDepthLevels((data.bids ?? []).map(([price, qty]) => [String(price), String(qty)]), "bid"); + const asks = normalizeDepthLevels((data.asks ?? []).map(([price, qty]) => [String(price), String(qty)]), "ask"); const depth: AsterDepth = { lastUpdateId: Number(message.seq ?? Date.now()), - bids: (data.bids ?? []).map(([price, qty]) => [String(price), String(qty)]), - asks: (data.asks ?? []).map(([price, qty]) => [String(price), String(qty)]), + bids, + asks, eventTime: Date.now(), - symbol: data.symbol, + symbol: rawSymbol, }; - this.emitDepth(data.symbol, depth); + this.emitDepth(rawSymbol, depth); return; } if (channel === "price") { @@ -913,10 +926,12 @@ export class StandxGateway { params: { symbol }, }); if (!data?.symbol) return; + const bids = normalizeDepthLevels((data.bids ?? []).map(([price, qty]) => [String(price), String(qty)]), "bid"); + const asks = normalizeDepthLevels((data.asks ?? []).map(([price, qty]) => [String(price), String(qty)]), "ask"); const depth: AsterDepth = { lastUpdateId: Date.now(), - bids: (data.bids ?? []).map(([price, qty]) => [String(price), String(qty)]), - asks: (data.asks ?? []).map(([price, qty]) => [String(price), String(qty)]), + bids, + asks, eventTime: Date.now(), symbol: data.symbol, };