@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
106 lines (105 loc) • 3.22 kB
TypeScript
/**
* Typed event target for Hyperliquid WebSocket messages.
*
* The frame types below are a trusted contract with the server: handlers
* consume them without re-validating the shape.
*
* @module
*/
/// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/transport/websocket/_events.ts" />
/**
* Confirmation frame of a `subscribe` / `unsubscribe` request.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/
export interface SubscribeUnsubscribeResponse {
method: "subscribe" | "unsubscribe";
/** Subscription payload echoed by the server: normalized, possibly with server-added fields. */
subscription: unknown;
}
/**
* Response frame of a `post` request.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests
*/
export interface PostResponse {
id: number;
response: {
type: "info";
payload: {
type: string;
data: unknown;
};
} | {
type: "action";
payload: {
status: "ok" | "err";
response: {
type: string;
data?: unknown;
} | string;
};
} | {
type: "error";
/** Error message, e.g. "Cannot track more than 15 total users." */
payload: string;
};
}
/**
* Block summary pushed by the explorer RPC.
*
* @see null
*/
interface BlockDetails {
blockTime: number;
hash: string;
height: number;
numTxs: number;
proposer: string;
}
/**
* Transaction details pushed by the explorer RPC.
*
* @see null
*/
interface TxDetails {
action: {
type: string;
[key: string]: unknown;
};
block: number;
error: string | null;
hash: string;
time: number;
user: string;
}
/** Base system events and dynamic channel events for the Hyperliquid WebSocket API. */
interface HyperliquidEventMap {
subscriptionResponse: CustomEvent<SubscribeUnsubscribeResponse>;
post: CustomEvent<PostResponse>;
/** Error text; any embedded `{…}` body is valid JSON. */
error: CustomEvent<string>;
pong: CustomEvent<undefined>;
explorerBlock_: CustomEvent<BlockDetails[]>;
explorerTxs_: CustomEvent<TxDetails[]>;
[key: string]: CustomEvent<any>;
}
/**
* Listens for WebSocket messages and re-dispatches them as typed Hyperliquid events.
*
* @example
* ```ts ignore
* const hlEvents = new HyperliquidEventTarget(socket);
* hlEvents.addEventListener("l2Book", (event) => {
* event.detail; // data of every '{"channel":"l2Book","data":{...}}' frame
* });
* ```
*/
export interface HyperliquidEventTarget {
addEventListener<K extends keyof HyperliquidEventMap>(type: K, listener: ((event: HyperliquidEventMap[K]) => void) | EventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof HyperliquidEventMap>(type: K, listener: ((event: HyperliquidEventMap[K]) => void) | EventListenerObject | null, options?: boolean | EventListenerOptions): void;
}
export declare class HyperliquidEventTarget extends EventTarget {
constructor(socket: WebSocket);
}
export {};