@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
123 lines (122 loc) • 4.61 kB
TypeScript
/// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/api/subscription/_methods/userEvents.ts" />
import * as v from "valibot";
import type { TwapHistoryResponse } from "../../info/_methods/twapHistory.js";
import type { UserFillsResponse } from "../../info/_methods/userFills.js";
import type { UserTwapSliceFillsResponse } from "../../info/_methods/userTwapSliceFills.js";
/**
* Subscription to user events for a specific user.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/
export declare const UserEventsRequest: v.ObjectSchema<{
/** Type of subscription. */
readonly type: v.LiteralSchema<"userEvents", undefined>;
/** User address. */
readonly user: v.SchemaWithPipe<readonly [v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, undefined>, v.TransformAction<string, `0x${string}`>]>, v.LengthAction<`0x${string}`, 42, undefined>]>;
}, undefined>;
export type UserEventsRequest = v.InferOutput<typeof UserEventsRequest>;
/**
* Event of one of possible user events.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/
export type UserEventsEvent = {
/** Array of user trade fills. */
fills: UserFillsResponse;
} | {
/** Funding update details. */
funding: {
/** Timestamp of the funding event (in ms since epoch). */
time: number;
/** Asset symbol (e.g., BTC). */
coin: string;
/**
* Amount transferred in USDC.
* @pattern ^-?[0-9]+(\.[0-9]+)?$
*/
usdc: string;
/**
* Signed position size.
* @pattern ^-?[0-9]+(\.[0-9]+)?$
*/
szi: string;
/**
* Applied funding rate.
* @pattern ^-?[0-9]+(\.[0-9]+)?$
*/
fundingRate: string;
/** Number of samples. */
nSamples: number | null;
};
} | {
/** Liquidation details. */
liquidation: {
/** Unique liquidation ID. */
lid: number;
/**
* Address of the liquidator.
* @pattern ^0x[a-fA-F0-9]{40}$
*/
liquidator: `0x${string}`;
/**
* Address of the liquidated user.
* @pattern ^0x[a-fA-F0-9]{40}$
*/
liquidated_user: `0x${string}`;
/**
* Notional position size that was liquidated.
* @pattern ^[0-9]+(\.[0-9]+)?$
*/
liquidated_ntl_pos: string;
/**
* Account value at time of liquidation.
* @pattern ^[0-9]+(\.[0-9]+)?$
*/
liquidated_account_value: string;
};
} | {
/** Array of non-user initiated order cancellations. */
nonUserCancel: {
/** Asset symbol (e.g., BTC). */
coin: string;
/** Order ID. */
oid: number;
}[];
} | {
/** Array of user's TWAP history. */
twapHistory: TwapHistoryResponse;
} | {
/** Array of user's TWAP slice fills. */
twapSliceFills: UserTwapSliceFillsResponse;
};
import type { ISubscription } from "../../../transport/mod.js";
import type { SubscriptionConfig, SubscriptionOptions } from "./_base/mod.js";
/** Request parameters for the {@linkcode userEvents} function. */
export type UserEventsParameters = Omit<v.InferInput<typeof UserEventsRequest>, "type">;
/**
* Subscribe to non-order events for a specific user.
*
* @param config General configuration for Subscription API subscriptions.
* @param params Parameters specific to the API subscription.
* @param listener A callback function to be called when the event is received.
* @param options Options to control the subscription lifecycle.
* @return A request-promise that resolves with a {@link ISubscription} object to manage the subscription lifecycle.
*
* @throws {ValidationError} When the request parameters fail validation (before sending).
* @throws {TransportError} When the transport layer throws an error.
*
* @example
* ```ts
* import { WebSocketTransport } from "@nktkas/hyperliquid";
* import { userEvents } from "@nktkas/hyperliquid/api/subscription";
*
* const transport = new WebSocketTransport();
*
* const sub = await userEvents(
* { transport },
* { user: "0x..." },
* (data) => console.log(data),
* );
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/
export declare function userEvents(config: SubscriptionConfig, params: UserEventsParameters, listener: (data: UserEventsEvent) => void, options?: SubscriptionOptions): Promise<ISubscription>;