UNPKG

@nktkas/hyperliquid

Version:

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

78 lines (77 loc) 3.35 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address, Hex, Integer, UnsignedInteger } from "../../_schemas.js"; /** * Add or remove margin from isolated position. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin */ export const UpdateIsolatedMarginRequest = /* @__PURE__ */ (()=>{ return v.object({ /** Action to perform. */ action: v.object({ /** Type of action. */ type: v.literal("updateIsolatedMargin"), /** Asset ID. */ asset: UnsignedInteger, /** Position side (`true` for long, `false` for short). */ isBuy: v.boolean(), /** Amount to adjust (float * 1e6). */ ntli: Integer }), /** 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 ]) }), /** Vault address (for vault trading). */ vaultAddress: v.optional(Address), /** 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 UpdateIsolatedMarginActionSchema = /* @__PURE__ */ (()=>{ return v.object(UpdateIsolatedMarginRequest.entries.action.entries); })(); /** * Add or remove margin from isolated position. * * 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 { updateIsolatedMargin } 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 updateIsolatedMargin({ transport, wallet }, { * asset: 0, * isBuy: true, * ntli: 1 * 1e6, * }); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin */ export function updateIsolatedMargin(config, params, opts) { const action = canonicalize(UpdateIsolatedMarginActionSchema, parse(UpdateIsolatedMarginActionSchema, { type: "updateIsolatedMargin", ...params })); return executeL1Action(config, action, opts); } //# sourceMappingURL=updateIsolatedMargin.js.map