import type { ISubscription } from '@nktkas/hyperliquid'; import type { UserEventsWsEvent } from '@nktkas/hyperliquid'; import type { ClearinghouseState, OpenOrder } from './types.js'; export interface WsEventMap { /** L2 order book snapshot for a specific coin */ l2Book: { coin: string; time: number; levels: [ Array<{ px: string; sz: string; n: number; }>, Array<{ px: string; sz: string; n: number; }> ]; }; /** Mid prices for all assets updated */ allMids: { mids: Record; }; /** * Asset contexts (funding / mark / oracle / OI / premium / impact) for EVERY dex — native and all * HIP-3 deployers — in a single push. `ctxs` is `[dexName, ctx[]]` tuples; `ctx[i]` aligns * positionally with that dex's `meta.universe[i]` (same join as the REST `metaAndAssetCtxs`), so a * consumer must hold the static universe to map index → coin. Lets a consumer drop the weight-20 * per-dex `metaAndAssetCtxs` REST poll entirely. Fields typed are the ones we read; the SDK emits a * structurally-assignable superset. */ allDexsAssetCtxs: { ctxs: Array<[ string, Array<{ funding: string; openInterest: string; dayNtlVlm: string; premium: string | null; oraclePx: string; markPx: string; midPx?: string | null; prevDayPx?: string; impactPxs?: readonly string[] | null; }> ]>; }; /** * Clearinghouse state (positions + margin) for EVERY dex for one user, in a single push — the WS * equivalent of `getUserStateAll`'s inputs. `clearinghouseStates` is `[dexName, state]` tuples * (dexName '' = native/main). Feed it to `client.userStateAllFromWs(...)` to get the same merged, * coin-canonicalized `ClearinghouseState` the REST path returns, with no REST weight. */ allDexsClearinghouseState: { user: string; clearinghouseStates: Array<[string, ClearinghouseState & { withdrawable?: string; }]>; }; /** User spot balances (the shared collateral pool for unified accounts). */ spotState: { balances: Array<{ coin: string; token: number; total: string; hold: string; entryNtl?: string; }>; }; /** Complete open-order snapshot for one user + dex (empty dex = native/main). */ openOrders: { user: string; dex: string; orders: OpenOrder[]; }; /** Order status changed (filled, canceled, rejected, etc.) */ orderUpdate: { order: { coin: string; side: 'B' | 'A'; limitPx: string; sz: string; oid: number; timestamp: number; origSz: string; cloid?: string; reduceOnly?: boolean; }; status: string; statusTimestamp: number; }; /** Trade fill received */ userFill: { coin: string; px: string; sz: string; side: 'B' | 'A'; time: number; closedPnl: string; fee: string; /** Token the fee is denominated in. Spot buys typically pay fee in the base asset (e.g. "HYPE") rather than "USDC"; consumers must convert using `px` to get a USD value. */ feeToken: string; oid: number; crossed: boolean; /** Frontend-display direction, e.g. "Open Long" / "Close Short" / "Buy". Distinguishes opens from closes. */ dir: string; /** Signed position size BEFORE this fill — lets a consumer tell a reduce/close from an open. */ startPosition: string; /** * Present only when this fill was a forced liquidation (otherwise undefined). A consumer that * hedges its own fills MUST branch on this: a liquidation force-closes the leg, so the correct * response is to flatten the opposite hedge, NOT to place a new same-size hedge. */ liquidation?: { liquidatedUser?: string; markPx: string; method: 'market' | 'backstop'; }; }; /** User event (fills, funding, liquidation, non-user cancels) */ userEvent: UserEventsWsEvent; /** WebSocket connected */ connected: undefined; /** WebSocket disconnected */ disconnected: { reason?: string; }; /** WebSocket error */ error: { error: Error; }; } export type WsEventType = keyof WsEventMap; export type WsEventHandler = (data: WsEventMap[E]) => void; export declare class WebSocketManager { private transport; private client; private subscriptions; private handlers; private _connected; private closing; private verbose; constructor(verbose?: boolean); private log; get connected(): boolean; connect(): Promise; close(): Promise; on(event: E, handler: WsEventHandler): void; off(event: E, handler: WsEventHandler): void; private emit; removeAllListeners(): void; private ensureClient; private trackSub; /** * Subscribe to mid prices for all assets. Fires on every price update. */ subscribeAllMids(): Promise; /** * Subscribe to L2 order book snapshots for a specific coin. * * `fast: true` = 5 levels every 0.5s; without it the API degrades to 20 levels * every 5s (per the 2026-07 network-upgrade announcement). Every consumer here * reads top-of-book only (chase / mm quoting / mid fallback), so fast wins; * anything needing depth should use the REST l2Book, which stays full-depth. */ subscribeL2Book(coin: string): Promise; /** * Subscribe to asset contexts for ALL dexes (native + every HIP-3 deployer) in one stream. Replaces * the weight-20-per-dex `metaAndAssetCtxs` REST poll for the dynamic ctx fields (funding/mark/oracle/ * OI/premium/impact). The static universe (coin name / szDecimals / maxLeverage) is NOT in this * stream — keep refreshing that on a slow cadence and join positionally. */ subscribeAllDexsAssetCtxs(): Promise; /** * Subscribe to clearinghouse state (positions + margin) across ALL dexes for a user, in one stream. * Replaces per-cycle `getUserStateAll` REST polling. Pair the emitted event with * `client.userStateAllFromWs(...)` to reconstruct the merged, coin-canonicalized state. */ subscribeAllDexsClearinghouseState(user: `0x${string}`): Promise; /** * Subscribe to a user's spot balances (the unified-account collateral pool). Replaces * `getSpotBalances` REST polling in the collateral gate. */ subscribeSpotState(user: `0x${string}`): Promise; /** Subscribe to complete open-order snapshots for one dex. */ subscribeOpenOrders(user: `0x${string}`, dex?: string): Promise; /** * Subscribe to order lifecycle events (fill, cancel, reject, etc.). * This is the most important subscription for trading automations. */ subscribeOrderUpdates(user: `0x${string}`): Promise; /** * Subscribe to trade fills for a user. */ subscribeUserFills(user: `0x${string}`): Promise; /** * Subscribe to all user events (fills, funding, liquidations, non-user cancels). * This is the only way to get liquidation alerts. */ subscribeUserEvents(user: `0x${string}`): Promise; /** * Start all subscriptions needed for the automation runtime: * - allMids (price feed) * - allDexsAssetCtxs (funding / mark / oracle contexts) * - allDexsClearinghouseState + spotState (positions, margin, balances) * - openOrders for main + requested HIP-3 dexes * - orderUpdates (order lifecycle) * - userFills (trade fills) * - userEvents (liquidations, funding payments, system cancels) */ subscribeAll(user: `0x${string}`, dexNames?: string[]): Promise; } export declare function getWebSocket(verbose?: boolean): WebSocketManager; export declare function resetWebSocket(): void; //# sourceMappingURL=ws.d.ts.map