UNPKG

@nktkas/hyperliquid

Version:

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

79 lines (78 loc) 3.22 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address, Hex, UnsignedDecimal, UnsignedInteger } from "../../_schemas.js"; /** * Transfer between sub-accounts (spot). * @see null */ export const SubAccountSpotTransferRequest = /* @__PURE__ */ (()=>{ return v.object({ /** Action to perform. */ action: v.object({ /** Type of action. */ type: v.literal("subAccountSpotTransfer"), /** Sub-account address. */ subAccountUser: Address, /** `true` for deposit, `false` for withdrawal. */ isDeposit: v.boolean(), /** Token identifier. */ token: v.string(), /** Amount to send (not in wei). */ amount: UnsignedDecimal }), /** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: UnsignedInteger, /** ECDSA signature components. */ signature: v.object({ /** First 32-byte component. */ r: v.pipe(Hex, v.length(66)), /** Second 32-byte component. */ s: v.pipe(Hex, v.length(66)), /** Recovery identifier. */ v: v.picklist([ 27, 28 ]) }), /** Expiration time of the action. */ expiresAfter: v.optional(UnsignedInteger) }); })(); // ============================================================ // Execution Logic // ============================================================ import { parse } from "../../../_base.js"; import { canonicalize } from "../../../signing/mod.js"; import { executeL1Action } from "./_base/mod.js"; /** Schema for action fields (excludes request-level system fields). */ const SubAccountSpotTransferActionSchema = /* @__PURE__ */ (()=>{ return v.object(SubAccountSpotTransferRequest.entries.action.entries); })(); /** * Transfer between sub-accounts (spot). * * Signing: L1 Action. * * @param config General configuration for Exchange API requests. * @param params Parameters specific to the API request. * @param opts Request execution options. * @return Successful response without specific data. * * @throws {ValidationError} 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 { subAccountSpotTransfer } 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 subAccountSpotTransfer({ transport, wallet }, { * subAccountUser: "0x...", * isDeposit: true, * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443", * amount: "1", * }); * ``` * * @see null */ export function subAccountSpotTransfer(config, params, opts) { const action = canonicalize(SubAccountSpotTransferActionSchema, parse(SubAccountSpotTransferActionSchema, { type: "subAccountSpotTransfer", ...params })); return executeL1Action(config, action, opts); } //# sourceMappingURL=subAccountSpotTransfer.js.map