From f8a87f4557bc3864745307c6d568dc7a8febc5cf Mon Sep 17 00:00:00 2001 From: discountry Date: Fri, 3 Oct 2025 16:06:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20Lighter=20?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=99=A8=E7=9A=84=E8=AE=A2=E5=8D=95=E7=B0=BF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=92=8C=E9=99=90=E5=88=B6=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/exchanges/lighter/gateway.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/exchanges/lighter/gateway.ts b/src/exchanges/lighter/gateway.ts index d632e69..98c327f 100644 --- a/src/exchanges/lighter/gateway.ts +++ b/src/exchanges/lighter/gateway.ts @@ -541,8 +541,8 @@ export class LighterGateway { const snapshot: LighterOrderBookSnapshot = { market_id: this.marketId ?? 0, offset: message.order_book.offset ?? Date.now(), - bids: normalizeLevels(message.order_book.bids ?? []), - asks: normalizeLevels(message.order_book.asks ?? []), + bids: sortAndTrimLevels(normalizeLevels(message.order_book.bids ?? []), "bid"), + asks: sortAndTrimLevels(normalizeLevels(message.order_book.asks ?? []), "ask"), }; this.orderBook = snapshot; this.lastOrderBookOffset = snapshot.offset ?? incomingOffset ?? this.lastOrderBookOffset; @@ -564,11 +564,11 @@ export class LighterGateway { if (!update) return; if (Array.isArray(update.asks)) { const asks = normalizeLevels(update.asks); - this.orderBook.asks = mergeLevels(this.orderBook.asks ?? [], asks); + this.orderBook.asks = sortAndTrimLevels(mergeLevels(this.orderBook.asks ?? [], asks), "ask"); } if (Array.isArray(update.bids)) { const bids = normalizeLevels(update.bids); - this.orderBook.bids = mergeLevels(this.orderBook.bids ?? [], bids); + this.orderBook.bids = sortAndTrimLevels(mergeLevels(this.orderBook.bids ?? [], bids), "bid"); } this.orderBook.offset = update.offset ?? this.orderBook.offset; this.lastOrderBookOffset = Number(this.orderBook.offset ?? incomingOffset ?? this.lastOrderBookOffset); @@ -892,6 +892,22 @@ function normalizeLevels(raw: Array lvl.price != null && lvl.size != null); } +// Ensure correct side ordering and limit depth size +function sortAndTrimLevels( + levels: LighterOrderBookLevel[] | undefined, + side: "bid" | "ask", + limit: number = 200 +): LighterOrderBookLevel[] { + const list = Array.isArray(levels) ? levels.slice() : []; + list.sort((a, b) => { + const pa = Number(a.price); + const pb = Number(b.price); + if (!Number.isFinite(pa) || !Number.isFinite(pb)) return 0; + return side === "bid" ? pb - pa : pa - pb; + }); + return list.slice(0, Math.max(1, limit)); +} + function mapOrderType(type: OrderType): number { switch (type) { case "MARKET":