diff --git a/src/exchanges/lighter/gateway.ts b/src/exchanges/lighter/gateway.ts index be6529a..0700a6a 100644 --- a/src/exchanges/lighter/gateway.ts +++ b/src/exchanges/lighter/gateway.ts @@ -135,6 +135,7 @@ const FEED_STALE_TIMEOUT_MS = 8_000; const STALE_CHECK_INTERVAL_MS = 2_000; const POSITION_HTTP_MAX_STALE_MS = 60_000; const ACCOUNT_POLL_INTERVAL_MS = 5_000; +const ORDER_RESYNC_INTERVAL_MS = 10_000; const POSITION_EPSILON = 1e-12; const RESOLUTION_MS: Record = { @@ -189,6 +190,8 @@ export class LighterGateway { private readonly pollers: Pollers = { ticker: undefined, klines: new Map() }; private accountPoller: ReturnType | null = null; private accountPollInFlight = false; + private ordersResyncTimer: ReturnType | null = null; + private ordersResyncInFlight = false; private readonly klineCache = new Map(); private readonly accountEvent = createEvent(); private readonly ordersEvent = createEvent(); @@ -1574,6 +1577,38 @@ export class LighterGateway { this.accountPoller = setInterval(pollAccount, ACCOUNT_POLL_INTERVAL_MS); pollAccount(); } + + if (!this.ordersResyncTimer) { + const resyncOrders = () => { + const now = Date.now(); + if (now - this.lastOrdersUpdateAt < ORDER_RESYNC_INTERVAL_MS - 500) return; + if (this.ordersResyncInFlight) return; + this.ordersResyncInFlight = true; + this.requestOrdersSnapshot() + .catch((error) => this.logger("ordersResync", error)) + .finally(() => { + this.ordersResyncInFlight = false; + }); + }; + this.ordersResyncTimer = setInterval(resyncOrders, ORDER_RESYNC_INTERVAL_MS); + } + } + + private async requestOrdersSnapshot(): Promise { + const ws = this.ws; + if (!ws || ws.readyState !== WebSocket.OPEN) return; + try { + const auth = await this.ensureAuthToken(); + ws.send( + JSON.stringify({ + type: "subscribe", + channel: `account_all_orders/${Number(this.signer.accountIndex)}`, + auth, + }) + ); + } catch (error) { + this.logger("ordersResyncSnapshot", error); + } } private startStaleMonitor(): void {