UNPKG

@nktkas/hyperliquid

Version:

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

92 lines 4.18 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address, UnsignedDecimal } from "../../_schemas.js"; import { ErrorResponse, HyperliquidChain, Nonce, Signature, SignatureChainId, SuccessResponse, } from "./_base/schemas.js"; /** * Initiate a withdrawal request. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request */ export const Withdraw3Request = /* @__PURE__ */ (() => { return v.pipe(v.object({ /** Action to perform. */ action: v.pipe(v.object({ /** Type of action. */ type: v.pipe(v.literal("withdraw3"), v.description("Type of action.")), /** Chain ID in hex format for EIP-712 signing. */ signatureChainId: SignatureChainId, /** HyperLiquid network type. */ hyperliquidChain: HyperliquidChain, /** Destination address. */ destination: v.pipe(Address, v.description("Destination address.")), /** Amount to withdraw (1 = $1). */ amount: v.pipe(UnsignedDecimal, v.description("Amount to withdraw (1 = $1).")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ time: Nonce, }), v.description("Action to perform.")), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: Nonce, /** ECDSA signature components. */ signature: Signature, }), v.description("Initiate a withdrawal request.")); })(); /** * Successful response without specific data or error response. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request */ export const Withdraw3Response = /* @__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 Withdraw3Parameters = /* @__PURE__ */ (() => { return v.omit(v.object(Withdraw3Request.entries.action.entries), ["type", "signatureChainId", "hyperliquidChain", "time"]); })(); /** EIP-712 types for the {@linkcode withdraw3} function. */ export const Withdraw3Types = { "HyperliquidTransaction:Withdraw": [ { name: "hyperliquidChain", type: "string" }, { name: "destination", type: "string" }, { name: "amount", type: "string" }, { name: "time", type: "uint64" }, ], }; /** * Initiate a withdrawal request. * * @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 { withdraw3 } 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 withdraw3( * { transport, wallet }, * { destination: "0x...", amount: "1" }, * ); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request */ export function withdraw3(config, params, opts) { const action = v.parse(Withdraw3Parameters, params); return executeUserSignedAction(config, { type: "withdraw3", ...action }, Withdraw3Types, opts); } //# sourceMappingURL=withdraw3.js.map