@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
171 lines (170 loc) • 7.59 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Address, Hex, Integer, Percent, UnsignedDecimal, UnsignedInteger } from "../../_schemas.js";
/**
* Deploying HIP-1 and HIP-2 assets.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-1-and-hip-2-assets
*/ export const SpotDeployRequest = /* @__PURE__ */ (()=>{
return v.object({
/** Action to perform. */ action: v.variant("type", [
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Register token parameters. */ registerToken2: v.object({
/** Token specifications. */ spec: v.object({
/** Token name. */ name: v.string(),
/** Number of decimals for token size. */ szDecimals: UnsignedInteger,
/** Number of decimals for token amounts in wei. */ weiDecimals: UnsignedInteger
}),
/** Maximum gas allowed for registration. */ maxGas: UnsignedInteger,
/** Optional full token name. */ fullName: v.optional(v.string())
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** User genesis parameters. */ userGenesis: v.object({
/** Token identifier. */ token: UnsignedInteger,
/** Array of tuples: [user address, genesis amount in wei]. */ userAndWei: v.array(v.tuple([
Address,
UnsignedDecimal
])),
/** Array of tuples: [existing token identifier, genesis amount in wei]. */ existingTokenAndWei: v.array(v.tuple([
UnsignedInteger,
UnsignedDecimal
])),
/** Array of tuples: [user address, blacklist status] (`true` for blacklist, `false` to remove existing blacklisted user). */ blacklistUsers: v.optional(v.array(v.tuple([
Address,
v.boolean()
])))
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Genesis parameters. */ genesis: v.object({
/** Token identifier. */ token: UnsignedInteger,
/** Maximum token supply. */ maxSupply: UnsignedDecimal,
/** Set hyperliquidity balance to 0. */ noHyperliquidity: v.optional(v.literal(true))
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Register spot parameters. */ registerSpot: v.object({
/** Tuple containing base and quote token indices. */ tokens: v.tuple([
UnsignedInteger,
UnsignedInteger
])
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Register hyperliquidity parameters. */ registerHyperliquidity: v.object({
/** Spot index (distinct from base token index). */ spot: UnsignedInteger,
/** Starting price for liquidity seeding. */ startPx: UnsignedDecimal,
/** Order size as a float (not in wei). */ orderSz: UnsignedDecimal,
/** Total number of orders to place. */ nOrders: UnsignedInteger,
/** Number of levels to seed with USDC. */ nSeededLevels: v.optional(UnsignedInteger)
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Set deployer trading fee share parameters. */ setDeployerTradingFeeShare: v.object({
/** Token identifier. */ token: UnsignedInteger,
/** The deployer trading fee share. Range is 0% to 100%. */ share: Percent
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Enable quote token parameters. */ enableQuoteToken: v.object({
/** The token ID to convert to a quote token. */ token: UnsignedInteger
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/** Disable quote token parameters. */ disableQuoteToken: v.object({
/** Token identifier to disable as quote token. */ token: UnsignedInteger
})
}),
v.object({
/** Type of action. */ type: v.literal("spotDeploy"),
/**
* Request link of a HyperCore spot token to an ERC-20 contract on the HyperEVM.
* Must be sent by the spot deployer. The link is finalized by the EVM deployer via {@link finalizeEvmContract}.
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/hypercore-less-than-greater-than-hyperevm-transfers
*/ requestEvmContract: v.object({
/** Token identifier to link. */ token: UnsignedInteger,
/** ERC-20 contract address on the HyperEVM. */ address: Address,
/**
* Difference in wei decimals between Core and EVM spot.
* E.g. Core PURR has 5 weiDecimals but EVM PURR has 18, so this would be `13`.
* Range: `[-2, 18]` inclusive.
*/ evmExtraWeiDecimals: v.pipe(Integer, v.minValue(-2), v.maxValue(18))
})
})
]),
/** 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";
/** Schema for action fields (excludes request-level system fields). */ const SpotDeployActionSchema = /* @__PURE__ */ (()=>{
return v.variant("type", SpotDeployRequest.entries.action.options);
})();
/**
* Deploying HIP-1 and HIP-2 assets.
*
* 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 { spotDeploy } 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 spotDeploy({ transport, wallet }, {
* registerToken2: {
* spec: {
* name: "USDC",
* szDecimals: 8,
* weiDecimals: 8,
* },
* maxGas: 1000000,
* fullName: "USD Coin",
* },
* });
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-1-and-hip-2-assets
*/ export function spotDeploy(config, params, opts) {
const action = canonicalize(SpotDeployActionSchema, parse(SpotDeployActionSchema, {
type: "spotDeploy",
...params
}));
return executeL1Action(config, action, opts);
}
//# sourceMappingURL=spotDeploy.js.map