@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
60 lines (59 loc) • 2.32 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Address } from "../../_schemas.js";
/**
* Subscription to TWAP states updates for a specific user.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/ export const TwapStatesRequest = /* @__PURE__ */ (()=>{
return v.object({
/** Type of subscription. */ type: v.literal("twapStates"),
/** User address. */ user: Address,
/** DEX name (empty string for main dex). */ dex: v.optional(v.string())
});
})();
// ============================================================
// Execution Logic
// ============================================================
import { parse } from "../../../_base.js";
/**
* Subscribe to TWAP states updates 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 { twapStates } from "@nktkas/hyperliquid/api/subscription";
*
* const transport = new WebSocketTransport();
*
* const sub = await twapStates(
* { transport },
* { user: "0x..." },
* (data) => console.log(data),
* );
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
*/ export function twapStates(config, params, listener, options) {
const payload = parse(TwapStatesRequest, {
type: "twapStates",
...params,
dex: params.dex ?? ""
});
return config.transport.subscribe(payload.type, payload, (e)=>{
if (e.detail.user === payload.user && e.detail.dex === payload.dex) {
listener(e.detail);
}
}, options);
}
//# sourceMappingURL=twapStates.js.map