@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
84 lines (83 loc) • 3.43 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Hex, UnsignedInteger } from "../../_schemas.js";
/**
* Create a vault.
* @see null
*/ export const CreateVaultRequest = /* @__PURE__ */ (()=>{
return v.object({
/** Action to perform. */ action: v.object({
/** Type of action. */ type: v.literal("createVault"),
/** Vault name. */ name: v.pipe(v.string(), v.minLength(3), v.maxLength(50)),
/** Vault description. */ description: v.pipe(v.string(), v.minLength(10), v.maxLength(250)),
/** Initial balance (float * 1e6). */ initialUsd: v.pipe(UnsignedInteger, v.minValue(100 * 1e6)),
/** Nonce (timestamp in ms). Equal to the envelope nonce; injected by the SDK. */ nonce: UnsignedInteger
}),
/** 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";
import { globalNonceManager } from "./_base/_nonce.js";
/** Schema for action fields (excludes request-level system fields). */ const CreateVaultActionSchema = /* @__PURE__ */ (()=>{
return v.object(CreateVaultRequest.entries.action.entries);
})();
/**
* Create a vault.
*
* 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 Response for creating a vault.
*
* @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 { createVault } 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 createVault({ transport, wallet }, {
* name: "...",
* description: "...",
* initialUsd: 100 * 1e6,
* });
* ```
*
* @see null
*/ export function createVault(config, params, opts) {
const action = canonicalize(CreateVaultActionSchema, parse(CreateVaultActionSchema, {
type: "createVault",
...params,
nonce: 0
}));
return executeL1Action({
...config,
nonceManager: async (addr)=>// Patch action.nonce in-place so the body matches the envelope nonce.
action.nonce = await (config.nonceManager?.(addr) ?? globalNonceManager.getNonce(`${addr}:${config.transport.isTestnet}`))
}, action, opts);
}
//# sourceMappingURL=createVault.js.map