@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
82 lines • 3.65 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Address, UnsignedInteger } from "../../_schemas.js";
import { ErrorResponse, Nonce, Signature } from "./_base/schemas.js";
/** Create a sub-account. */
export const CreateSubAccountRequest = /* @__PURE__ */ (() => {
return v.pipe(v.object({
/** Action to perform. */
action: v.pipe(v.object({
/** Type of action. */
type: v.pipe(v.literal("createSubAccount"), v.description("Type of action.")),
/** Sub-account name. */
name: v.pipe(v.string(), v.minLength(1), v.description("Sub-account name.")),
}), v.description("Action to perform.")),
/** Nonce (timestamp in ms) used to prevent replay attacks. */
nonce: Nonce,
/** ECDSA signature components. */
signature: Signature,
/** Expiration time of the action. */
expiresAfter: v.pipe(v.optional(UnsignedInteger), v.description("Expiration time of the action.")),
}), v.description("Create a sub-account."));
})();
/** Response for creating a sub-account. */
export const CreateSubAccountResponse = /* @__PURE__ */ (() => {
return v.pipe(v.union([
v.pipe(v.object({
/** Successful status. */
status: v.pipe(v.literal("ok"), v.description("Successful status.")),
/** Response details. */
response: v.pipe(v.object({
/** Type of response. */
type: v.pipe(v.literal("createSubAccount"), v.description("Type of response.")),
/** Sub-account address. */
data: v.pipe(Address, v.description("Sub-account address.")),
}), v.description("Response details.")),
}), v.description("Successful response for creating a sub-account")),
ErrorResponse,
]), v.description("Response for creating a sub-account."));
})();
// ============================================================
// Execution Logic
// ============================================================
import { executeL1Action } from "./_base/execute.js";
/** Schema for user-provided action parameters (excludes system fields). */
const CreateSubAccountParameters = /* @__PURE__ */ (() => {
return v.omit(v.object(CreateSubAccountRequest.entries.action.entries), ["type"]);
})();
/**
* Create a sub-account.
*
* @param config - General configuration for Exchange API requests.
* @param params - Parameters specific to the API request.
* @param opts - Request execution options.
*
* @returns Response for creating a sub-account.
*
* @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 { createSubAccount } from "@nktkas/hyperliquid/api/exchange";
* import { privateKeyToAccount } from "npm:viem/accounts";
*
* const wallet = privateKeyToAccount("0x..."); // viem or ethers
* const transport = new HttpTransport(); // or `WebSocketTransport`
*
* const data = await createSubAccount(
* { transport, wallet },
* { name: "..." },
* );
* ```
*/
export function createSubAccount(config, params, opts) {
const action = v.parse(CreateSubAccountParameters, params);
return executeL1Action(config, { type: "createSubAccount", ...action }, opts);
}
//# sourceMappingURL=createSubAccount.js.map