UNPKG

@nktkas/hyperliquid

Version:

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

95 lines 5.01 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address, Hex, UnsignedInteger } from "../../_schemas.js"; import { ErrorResponse, HyperliquidChainSchema, SignatureSchema, SuccessResponse } from "./_base/commonSchemas.js"; /** * Delegate or undelegate native tokens to or from a validator. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator */ export const TokenDelegateRequest = /* @__PURE__ */ (() => { return v.pipe(v.object({ /** Action to perform. */ action: v.pipe(v.object({ /** Type of action. */ type: v.pipe(v.literal("tokenDelegate"), v.description("Type of action.")), /** Chain ID in hex format for EIP-712 signing. */ signatureChainId: v.pipe(Hex, v.description("Chain ID in hex format for EIP-712 signing.")), /** HyperLiquid network type. */ hyperliquidChain: v.pipe(HyperliquidChainSchema, v.description("HyperLiquid network type.")), /** Validator address. */ validator: v.pipe(Address, v.description("Validator address.")), /** Amount for delegate/undelegate (float * 1e8). */ wei: v.pipe(UnsignedInteger, v.minValue(1), v.description("Amount for delegate/undelegate (float * 1e8).")), /** `true` for undelegate, `false` for delegate. */ isUndelegate: v.pipe(v.boolean(), v.description("`true` for undelegate, `false` for delegate.")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: v.pipe(UnsignedInteger, v.description("Nonce (timestamp in ms) used to prevent replay attacks.")), }), v.description("Action to perform.")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: v.pipe(UnsignedInteger, v.description("Nonce (timestamp in ms) used to prevent replay attacks.")), /** ECDSA signature components. */ signature: v.pipe(SignatureSchema, v.description("ECDSA signature components.")), }), v.description("Delegate or undelegate native tokens to or from a validator.")); })(); /** * Successful response without specific data or error response. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator */ export const TokenDelegateResponse = /* @__PURE__ */ (() => { return v.pipe(v.union([SuccessResponse, ErrorResponse]), v.description("Successful response without specific data or error response.")); })(); // ============================================================ // Execution Logic // ============================================================ import { executeUserSignedAction } from "./_base/execute.js"; /** Schema for user-provided action parameters (excludes system fields). */ const TokenDelegateParameters = /* @__PURE__ */ (() => { return v.omit(v.object(TokenDelegateRequest.entries.action.entries), ["type", "signatureChainId", "hyperliquidChain", "nonce"]); })(); /** EIP-712 types for the {@linkcode tokenDelegate} function. */ export const TokenDelegateTypes = { "HyperliquidTransaction:TokenDelegate": [ { name: "hyperliquidChain", type: "string" }, { name: "validator", type: "address" }, { name: "wei", type: "uint64" }, { name: "isUndelegate", type: "bool" }, { name: "nonce", type: "uint64" }, ], }; /** * Delegate or undelegate native tokens to or from a validator. * * @param config - General configuration for Exchange API requests. * @param params - Parameters specific to the API request. * @param opts - Request execution options. * * @returns Successful response without specific data. * * @throws {ValiError} When the request parameters fail validation (before sending). * @throws {TransportError} When the transport layer throws an error. * @throws {ApiRequestError} When the API returns an unsuccessful response. * * @example * ```ts * import { HttpTransport } from "@nktkas/hyperliquid"; * import { tokenDelegate } from "@nktkas/hyperliquid/api/exchange"; * import { privateKeyToAccount } from "npm:viem/accounts"; * * const wallet = privateKeyToAccount("0x..."); // viem or ethers * const transport = new HttpTransport(); // or `WebSocketTransport` * * await tokenDelegate( * { transport, wallet }, * { validator: "0x...", isUndelegate: true, wei: 1 * 1e8 }, * ); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator */ export function tokenDelegate(config, params, opts) { const action = v.parse(TokenDelegateParameters, params); return executeUserSignedAction(config, { type: "tokenDelegate", ...action }, TokenDelegateTypes, opts); } //# sourceMappingURL=tokenDelegate.js.map