UNPKG

@nktkas/hyperliquid

Version:

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

92 lines 4.33 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address } from "../../_schemas.js"; import { ErrorResponse, HyperliquidChain, Nonce, Signature, SignatureChainId, SuccessResponse, } from "./_base/schemas.js"; /** * Approve an agent to sign on behalf of the master account. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet */ export const ApproveAgentRequest = /* @__PURE__ */ (() => { return v.pipe(v.object({ /** Action to perform. */ action: v.pipe(v.object({ /** Type of action. */ type: v.pipe(v.literal("approveAgent"), v.description("Type of action.")), /** Chain ID in hex format for EIP-712 signing. */ signatureChainId: SignatureChainId, /** HyperLiquid network type. */ hyperliquidChain: HyperliquidChain, /** Agent address. */ agentAddress: v.pipe(Address, v.description("Agent address.")), /** Agent name or null for unnamed agent (default: null). */ agentName: v.pipe(v.optional(v.nullable(v.string()), null), v.description("Agent name or null for unnamed agent.")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: Nonce, }), v.description("Action to perform.")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: Nonce, /** ECDSA signature components. */ signature: Signature, }), v.description("Approve an agent to sign on behalf of the master account.")); })(); /** * Successful response without specific data or error response. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet */ export const ApproveAgentResponse = /* @__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 ApproveAgentParameters = /* @__PURE__ */ (() => { return v.omit(v.object(ApproveAgentRequest.entries.action.entries), ["type", "signatureChainId", "hyperliquidChain", "nonce"]); })(); /** EIP-712 types for the {@linkcode approveAgent} function. */ export const ApproveAgentTypes = { "HyperliquidTransaction:ApproveAgent": [ { name: "hyperliquidChain", type: "string" }, { name: "agentAddress", type: "address" }, { name: "agentName", type: "string" }, { name: "nonce", type: "uint64" }, ], }; /** * Approve an agent to sign on behalf of the master account. * * @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 { approveAgent } 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 approveAgent( * { transport, wallet }, * { agentAddress: "0x...", agentName: "..." }, * ); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet */ export function approveAgent(config, params, opts) { const action = v.parse(ApproveAgentParameters, params); return executeUserSignedAction(config, { type: "approveAgent", ...action }, ApproveAgentTypes, opts); } //# sourceMappingURL=approveAgent.js.map