@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
129 lines • 6.08 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsdClassTransferTypes = exports.UsdClassTransferResponse = exports.UsdClassTransferRequest = void 0;
exports.usdClassTransfer = usdClassTransfer;
const v = __importStar(require("valibot"));
// ============================================================
// API Schemas
// ============================================================
const _schemas_js_1 = require("../../_schemas.js");
const schemas_js_1 = require("./_base/schemas.js");
/**
* Transfer funds between Spot account and Perp account.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
*/
exports.UsdClassTransferRequest = (() => {
return v.pipe(v.object({
/** Action to perform. */
action: v.pipe(v.object({
/** Type of action. */
type: v.pipe(v.literal("usdClassTransfer"), v.description("Type of action.")),
/** Chain ID in hex format for EIP-712 signing. */
signatureChainId: schemas_js_1.SignatureChainId,
/** HyperLiquid network type. */
hyperliquidChain: schemas_js_1.HyperliquidChain,
/** Amount to transfer (1 = $1). */
amount: v.pipe(_schemas_js_1.UnsignedDecimal, v.description("Amount to transfer (1 = $1).")),
/** `true` for Spot to Perp, `false` for Perp to Spot. */
toPerp: v.pipe(v.boolean(), v.description("`true` for Spot to Perp, `false` for Perp to Spot.")),
/** Nonce (timestamp in ms) used to prevent replay attacks. */
nonce: schemas_js_1.Nonce,
}), v.description("Action to perform.")),
/** Nonce (timestamp in ms) used to prevent replay attacks. */
nonce: schemas_js_1.Nonce,
/** ECDSA signature components. */
signature: schemas_js_1.Signature,
}), v.description("Transfer funds between Spot account and Perp account."));
})();
/**
* Successful response without specific data or error response.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
*/
exports.UsdClassTransferResponse = (() => {
return v.pipe(v.union([schemas_js_1.SuccessResponse, schemas_js_1.ErrorResponse]), v.description("Successful response without specific data or error response."));
})();
// ============================================================
// Execution Logic
// ============================================================
const execute_js_1 = require("./_base/execute.js");
/** Schema for user-provided action parameters (excludes system fields). */
const UsdClassTransferParameters = /* @__PURE__ */ (() => {
return v.omit(v.object(exports.UsdClassTransferRequest.entries.action.entries), ["type", "signatureChainId", "hyperliquidChain", "nonce"]);
})();
/** EIP-712 types for the {@linkcode usdClassTransfer} function. */
exports.UsdClassTransferTypes = {
"HyperliquidTransaction:UsdClassTransfer": [
{ name: "hyperliquidChain", type: "string" },
{ name: "amount", type: "string" },
{ name: "toPerp", type: "bool" },
{ name: "nonce", type: "uint64" },
],
};
/**
* Transfer funds between Spot account and Perp account.
*
* @param config - General configuration for Exchange API requests.
* @param params - Parameters specific to the API request.
* @param opts - Request execution options.
*
* @returns Successful response without specific data.
*
* @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 { usdClassTransfer } 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 usdClassTransfer(
* { transport, wallet },
* { amount: "1", toPerp: true },
* );
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
*/
function usdClassTransfer(config, params, opts) {
const action = v.parse(UsdClassTransferParameters, params);
return (0, execute_js_1.executeUserSignedAction)(config, { type: "usdClassTransfer", ...action }, exports.UsdClassTransferTypes, opts);
}
//# sourceMappingURL=usdClassTransfer.js.map