mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 17:28:08 +00:00
feat: 在 Lighter 适配器中添加订单簿序列跟踪,优化 WebSocket 消息处理逻辑
This commit is contained in:
@@ -184,6 +184,10 @@ export class LighterGateway {
|
|||||||
private readonly tickerPollMs: number;
|
private readonly tickerPollMs: number;
|
||||||
private readonly klinePollMs: number;
|
private readonly klinePollMs: number;
|
||||||
|
|
||||||
|
// Track last applied order book sequence to drop stale WS messages
|
||||||
|
private lastOrderBookOffset: number = 0;
|
||||||
|
private lastOrderBookTimestamp: number = 0;
|
||||||
|
|
||||||
constructor(options: LighterGatewayOptions) {
|
constructor(options: LighterGatewayOptions) {
|
||||||
this.displaySymbol = options.symbol;
|
this.displaySymbol = options.symbol;
|
||||||
this.marketSymbol = (options.marketSymbol ?? options.symbol).toUpperCase();
|
this.marketSymbol = (options.marketSymbol ?? options.symbol).toUpperCase();
|
||||||
@@ -514,6 +518,14 @@ export class LighterGateway {
|
|||||||
|
|
||||||
private handleOrderBookSnapshot(message: any): void {
|
private handleOrderBookSnapshot(message: any): void {
|
||||||
if (!message?.order_book) return;
|
if (!message?.order_book) return;
|
||||||
|
const incomingOffset = Number(message.offset ?? message.order_book?.offset ?? 0);
|
||||||
|
const incomingTs = Number(message.timestamp ?? 0);
|
||||||
|
if (this.lastOrderBookOffset && incomingOffset && incomingOffset < this.lastOrderBookOffset) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (incomingOffset === this.lastOrderBookOffset && incomingTs && incomingTs <= this.lastOrderBookTimestamp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const snapshot: LighterOrderBookSnapshot = {
|
const snapshot: LighterOrderBookSnapshot = {
|
||||||
market_id: this.marketId ?? 0,
|
market_id: this.marketId ?? 0,
|
||||||
offset: message.order_book.offset ?? Date.now(),
|
offset: message.order_book.offset ?? Date.now(),
|
||||||
@@ -521,11 +533,21 @@ export class LighterGateway {
|
|||||||
asks: normalizeLevels(message.order_book.asks ?? []),
|
asks: normalizeLevels(message.order_book.asks ?? []),
|
||||||
};
|
};
|
||||||
this.orderBook = snapshot;
|
this.orderBook = snapshot;
|
||||||
|
this.lastOrderBookOffset = snapshot.offset ?? incomingOffset ?? this.lastOrderBookOffset;
|
||||||
|
this.lastOrderBookTimestamp = incomingTs || Date.now();
|
||||||
this.emitDepth();
|
this.emitDepth();
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleOrderBookUpdate(message: any): void {
|
private handleOrderBookUpdate(message: any): void {
|
||||||
if (!this.orderBook) return;
|
if (!this.orderBook) return;
|
||||||
|
const incomingOffset = Number(message.offset ?? message.order_book?.offset ?? 0);
|
||||||
|
const incomingTs = Number(message.timestamp ?? 0);
|
||||||
|
if (this.lastOrderBookOffset && incomingOffset && incomingOffset < this.lastOrderBookOffset) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (incomingOffset === this.lastOrderBookOffset && incomingTs && incomingTs <= this.lastOrderBookTimestamp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const update = message?.order_book;
|
const update = message?.order_book;
|
||||||
if (!update) return;
|
if (!update) return;
|
||||||
if (Array.isArray(update.asks)) {
|
if (Array.isArray(update.asks)) {
|
||||||
@@ -537,6 +559,8 @@ export class LighterGateway {
|
|||||||
this.orderBook.bids = mergeLevels(this.orderBook.bids ?? [], bids);
|
this.orderBook.bids = mergeLevels(this.orderBook.bids ?? [], bids);
|
||||||
}
|
}
|
||||||
this.orderBook.offset = update.offset ?? this.orderBook.offset;
|
this.orderBook.offset = update.offset ?? this.orderBook.offset;
|
||||||
|
this.lastOrderBookOffset = Number(this.orderBook.offset ?? incomingOffset ?? this.lastOrderBookOffset);
|
||||||
|
this.lastOrderBookTimestamp = incomingTs || Date.now();
|
||||||
this.emitDepth();
|
this.emitDepth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user