@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
107 lines • 4.54 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Address, UnsignedInteger } from "../../_schemas.js";
import { Nonce, Signature } from "./_base/schemas.js";
/**
* Cancel order(s).
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
*/
export const CancelRequest = /* @__PURE__ */ (() => {
return v.pipe(v.object({
/** Action to perform. */
action: v.pipe(v.object({
/** Type of action. */
type: v.pipe(v.literal("cancel"), v.description("Type of action.")),
/** Orders to cancel. */
cancels: v.pipe(v.array(v.object({
/** Asset ID. */
a: v.pipe(UnsignedInteger, v.description("Asset ID.")),
/** Order ID. */
o: v.pipe(UnsignedInteger, v.description("Order ID.")),
})), v.description("Orders to cancel.")),
}), v.description("Action to perform.")),
/** Nonce (timestamp in ms) used to prevent replay attacks. */
nonce: Nonce,
/** ECDSA signature components. */
signature: Signature,
/** Vault address (for vault trading). */
vaultAddress: v.pipe(v.optional(Address), v.description("Vault address (for vault trading).")),
/** Expiration time of the action. */
expiresAfter: v.pipe(v.optional(UnsignedInteger), v.description("Expiration time of the action.")),
}), v.description("Cancel order(s)."));
})();
/**
* Response for order cancellation.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
*/
export const CancelResponse = /* @__PURE__ */ (() => {
return 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("cancel"), v.description("Type of response.")),
/** Specific data. */
data: v.pipe(v.object({
/** Array of statuses or error messages. */
statuses: v.pipe(v.array(v.union([
v.literal("success"),
v.object({
/** Error message. */
error: v.pipe(v.string(), v.description("Error message.")),
}),
])), v.description("Array of statuses or error messages.")),
}), v.description("Specific data.")),
}), v.description("Response details.")),
}), v.description("Response for order cancellation."));
})();
// ============================================================
// Execution Logic
// ============================================================
import { executeL1Action } from "./_base/execute.js";
/** Schema for user-provided action parameters (excludes system fields). */
const CancelParameters = /* @__PURE__ */ (() => {
return v.omit(v.object(CancelRequest.entries.action.entries), ["type"]);
})();
/**
* Cancel order(s).
*
* @param config - General configuration for Exchange API requests.
* @param params - Parameters specific to the API request.
* @param opts - Request execution options.
*
* @returns Successful variant of {@link CancelResponse} without error statuses.
*
* @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 { cancel } 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 cancel(
* { transport, wallet },
* {
* cancels: [
* { a: 0, o: 123 },
* ],
* },
* );
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
*/
export function cancel(config, params, opts) {
const action = v.parse(CancelParameters, params);
return executeL1Action(config, { type: "cancel", ...action }, opts);
}
//# sourceMappingURL=cancel.js.map