import { type PerpsSDKClient, type ProviderGetQuoteParams, type QuoteListener, WsProviderBase, type WsProviderFactory } from '@lifi/perps-sdk'; import type { Subscription } from '@lifi/perps-types'; import type { Address } from 'viem'; /** * Resolves a Lighter auth token for the given L1 address, or `undefined` when * no source can produce one. Called on the initial subscribe and again on * every reconnect, so it must return a token still valid a few minutes out. * * The default resolver — the co-registered `lighterProvider` plugin's * `resolveAuthToken` — satisfies that: it never yields an expired token. * Stored read-only tokens past expiry are filtered out and otherwise carry a * ~10-year lifetime; near-expiry standard tokens are re-created within their * renew buffer; and a server-rejected token self-heals on the next resolve. A * supplied override owns its own freshness. * * @public */ export type LighterAuthTokenResolver = (address: Address) => Promise; interface SubState { /** Raw Lighter WS channel name (e.g. `order_book/5`, `account_all_orders/42`). */ channel: string; /** L1 address that triggered this sub (auth channels only). */ address?: Address; /** Whether this channel needs an `auth` field on each subscribe send. */ needsAuth: boolean; } /** * Options for {@link lighterWsProvider} / {@link LighterWsProvider}. * * @public */ export interface LighterWsProviderOptions { /** REST base URL for `/api/v1/account` lookups. Defaults to mainnet. */ restUrl?: string; /** * Override for auth-token resolution on authenticated channels * (orderUpdates, positions). Defaults to the co-registered `lighterProvider` * plugin's `resolveAuthToken`; supply this only for a standalone WS client * with no Lighter REST plugin on the same {@link PerpsSDKClient}. */ resolveAuthToken?: LighterAuthTokenResolver; } /** * Lighter realtime WS provider (extends {@link WsProviderBase}): subscribes to * Lighter's WS channels (orderbook, marketsContext, orders, positions), attaching auth * tokens to gated channels. Construct via {@link lighterWsProvider}. * * @public */ export declare class LighterWsProvider extends WsProviderBase { private readonly api; private readonly resolveAuthTokenOverride; private readonly client; private readonly orderbooks; private marketsContext; /** * `address → market_id → Position`. `subscribed/...` frames reseed it and * `update/...` frames upsert into it (zero size deletes), so every emission * is a full open-position snapshot per the `PositionsEvent` contract. */ private readonly positionsByAddress; private readonly registry; private readonly accountIndexCache; private readonly accountIndexPromises; constructor(wsUrl?: string, providerKey?: string, options?: LighterWsProviderOptions, client?: PerpsSDKClient); /** * Auth-token resolver for gated channels: the explicit `resolveAuthToken` * override if supplied, else the co-registered Lighter plugin's * `resolveAuthToken`. `undefined` when neither is available. */ private authTokenResolver; /** * Subscribe to a live quote stream resolved through the registered Lighter * market registry. Returns an unsubscribe function; quote subscription * requires the provider to be constructed through `lighterWsProvider` with a * bound {@link PerpsSDKClient}. */ subscribeQuote(params: ProviderGetQuoteParams, onQuote: QuoteListener): Promise<() => void>; protected openChannel(sub: Subscription): Promise<() => void>; protected onClose(): void; protected sendSubscribe({ channel, needsAuth, address, }: SubState): Promise; protected toKey(sub: Subscription): string; private resolveChannel; private resolveAccountIndex; private fetchAccountIndex; protected handleMessage(raw: string): void; private dispatch; private handleAccountOrders; private handleUserStats; private handleAccountTrades; private handleAccountPositions; /** * Reverse-lookup the L1 address from an auth-channel message. The subscribe * payload uses `/` (e.g. `account_all_orders/42`) but the server sends * responses with `:` (e.g. `account_all_orders:42`) — both forms are * accepted here so reconnect resubscriptions and live updates both route * correctly. */ private addressFromChannel; private handleMarketStats; private handleOrderBook; private handleTrades; private marketIdFromChannel; } /** * `WsProviderFactory` constructor for Lighter — register with * `new PerpsWsClient(client, { wsProviders: { lighter: lighterWsProvider() } })`. * * Bare construction is the default path: authenticated channels resolve their * auth token through the `lighterProvider()` plugin co-registered on the same * {@link PerpsSDKClient}, so no wiring is needed. Pass `resolveAuthToken` only * as the exception — a standalone WS client with no Lighter REST plugin on * that client. * * Closes over the per-instance options (`resolveAuthToken` override, `restUrl`) * so `PerpsWsClient` can call the returned factory with just * `({ provider, wsUrl, client })` at subscribe time. * * @public */ export declare const lighterWsProvider: (options?: LighterWsProviderOptions) => WsProviderFactory; export {}; //# sourceMappingURL=LighterWsProvider.d.ts.map