UNPKG

@nktkas/hyperliquid

Version:

Hyperliquid API SDK for all major JS runtimes, written in TypeScript.

59 lines 2.63 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { SpotAssetCtxSchema } from "../../info/_methods/_base/commonSchemas.js"; /** Subscription to context events for a specific spot asset. */ export const ActiveSpotAssetCtxRequest = /* @__PURE__ */ (() => { return v.pipe(v.object({ /** Type of subscription. */ type: v.pipe(v.literal("activeAssetCtx"), v.description("Type of subscription.")), /** Asset ID (e.g., @1). */ coin: v.pipe(v.string(), v.description("Asset ID (e.g., @1).")), }), v.description("Subscription to context events for a specific spot asset.")); })(); /** Event of active spot asset context. */ export const ActiveSpotAssetCtxEvent = /* @__PURE__ */ (() => { return v.pipe(v.object({ /** Asset ID (e.g., @1). */ coin: v.pipe(v.string(), v.description("Asset ID (e.g., @1).")), /** Context for a specific spot asset. */ ctx: v.pipe(SpotAssetCtxSchema, v.description("Context for a specific spot asset.")), }), v.description("Event of active spot asset context.")); })(); /** * Subscribe to context updates for a specific spot asset. * * @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. * @returns A request-promise that resolves with a {@link ISubscription} object to manage the subscription lifecycle. * * @throws {ValiError} 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 { activeSpotAssetCtx } from "@nktkas/hyperliquid/api/subscription"; * * const transport = new WebSocketTransport(); // only `WebSocketTransport` * * const sub = await activeSpotAssetCtx( * { transport }, * { coin: "@1" }, * (data) => console.log(data), * ); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions */ export function activeSpotAssetCtx(config, params, listener) { const payload = v.parse(ActiveSpotAssetCtxRequest, { type: "activeAssetCtx", ...params }); return config.transport.subscribe("activeSpotAssetCtx", payload, (e) => { if (e.detail.coin === payload.coin) { listener(e.detail); } }); } //# sourceMappingURL=activeSpotAssetCtx.js.map