UNPKG

@nktkas/hyperliquid

Version:

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

133 lines (132 loc) 5.56 kB
import * as v from "valibot"; // ============================================================ // API Schemas // ============================================================ import { Address, Cloid, Hex, UnsignedDecimal, UnsignedInteger } from "../../_schemas.js"; /** * Modify multiple orders. * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders */ export const BatchModifyRequest = /* @__PURE__ */ (()=>{ return v.object({ /** Action to perform. */ action: v.object({ /** Type of action. */ type: v.literal("batchModify"), /** Order modifications. */ modifies: v.array(v.object({ /** Order ID or Client Order ID. */ oid: v.union([ UnsignedInteger, Cloid ]), /** New order parameters. */ order: v.object({ /** Asset ID. */ a: UnsignedInteger, /** Position side (`true` for long, `false` for short). */ b: v.boolean(), /** Price. */ p: v.pipe(UnsignedDecimal, v.check((input)=>Number(input) > 0, "Value must be greater than zero")), /** Size (in base currency units). */ s: UnsignedDecimal, /** Whether the order is reduce-only. */ r: v.boolean(), /** Order type (`limit` for limit orders, `trigger` for stop-loss/take-profit orders). */ t: v.union([ v.object({ /** Limit order parameters. */ limit: v.object({ /** * Time-in-force. * - `"Gtc"`: Remains active until filled or canceled. * - `"Ioc"`: Fills immediately or cancels any unfilled portion. * - `"Alo"`: Adds liquidity only. * - `"FrontendMarket"`: Similar to Ioc, but add a note that this is market order. */ tif: v.picklist([ "Gtc", "Ioc", "Alo", "FrontendMarket" ]) }) }), v.object({ /** Trigger order parameters. */ trigger: v.object({ /** Whether the order is a market order. */ isMarket: v.boolean(), /** Trigger price. */ triggerPx: v.pipe(UnsignedDecimal, v.check((input)=>Number(input) > 0, "Value must be greater than zero")), /** Indicates whether it is take profit or stop loss. */ tpsl: v.picklist([ "tp", "sl" ]) }) }) ]), /** Client Order ID. */ c: v.optional(Cloid) }) })), /** * Always place the resulting orders, even if the cancels did not succeed. * * Omit the field otherwise; the default behavior requires each new order to be a non-trigger order with TIF * `Alo`, or a non-executable order with TIF `Gtc`. */ a: v.optional(v.literal(true)) }), /** 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 BatchModifyActionSchema = /* @__PURE__ */ (()=>{ return v.object(BatchModifyRequest.entries.action.entries); })(); /** * Modify multiple orders. * * 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 variant of {@link OrderResponse} without error statuses. * * @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 { batchModify } 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 batchModify({ transport, wallet }, { * modifies: [ * { * oid: 123, * order: { * a: 0, * b: true, * p: "31000", * s: "0.2", * r: false, * t: { limit: { tif: "Gtc" } }, * }, * }, * ], * }); * ``` * * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders */ export function batchModify(config, params, opts) { const action = canonicalize(BatchModifyActionSchema, parse(BatchModifyActionSchema, { type: "batchModify", ...params })); return executeL1Action(config, action, opts); } //# sourceMappingURL=batchModify.js.map