@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
70 lines (69 loc) • 2.56 kB
TypeScript
/**
* Price and size formatting per Hyperliquid tick and lot size rules.
*
* @module
*/
/// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/utils/_format.ts" />
import { HyperliquidError } from "../_base.js";
/**
* Thrown when a price or size value cannot be formatted to a valid decimal.
*
* @example
* ```ts
* import { formatPrice, FormatError } from "@nktkas/hyperliquid/utils";
*
* try {
* formatPrice("not a number", 0);
* } catch (error) {
* if (error instanceof FormatError) {
* console.error(error.message);
* }
* }
* ```
*/
export declare class FormatError extends HyperliquidError {
constructor(message: string, options?: ErrorOptions);
}
/**
* Format price according to Hyperliquid {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/tick-and-lot-size | rules}:
* - Maximum 5 significant figures
* - Maximum 6 (for perp) or 8 (for spot) - `szDecimals` decimal places
* - Integer prices are always allowed regardless of significant figures
*
* @param price The price to format (as string or number).
* @param szDecimals The size decimals of the asset.
* @param type The market type: "perp" for perpetuals or "spot" for spot markets. Default: `"perp"`.
* @return Formatted price string
*
* @throws {FormatError} If the price is not a valid finite number, or is truncated to 0.
*
* @example
* ```ts
* import { formatPrice } from "@nktkas/hyperliquid/utils";
*
* formatPrice("97123.456789", 0); // → "97123" (perp, szDecimals=0)
* formatPrice("1.23456789", 5); // → "1.2" (perp, szDecimals=5)
* formatPrice("0.0000123456789", 0, "spot"); // → "0.00001234" (spot, 8-decimal ceiling)
* ```
*/
export declare function formatPrice(price: string | number, szDecimals: number, type?: "perp" | "spot"): string;
/**
* Format size according to Hyperliquid {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/tick-and-lot-size | rules}:
* - Truncate decimal places to `szDecimals`
*
* @param size The size to format (as string or number).
* @param szDecimals The size decimals of the asset.
* @return Formatted size string
*
* @throws {FormatError} If the size is not a valid finite number, or is truncated to 0.
*
* @example
* ```ts
* import { formatSize } from "@nktkas/hyperliquid/utils";
*
* formatSize("1.23456789", 5); // → "1.23456"
* formatSize("0.123456789", 2); // → "0.12"
* formatSize("100", 0); // → "100"
* ```
*/
export declare function formatSize(size: string | number, szDecimals: number): string;