Formatting

Utilities for formatting prices and sizes according to Hyperliquid's tick and lot size rulesarrow-up-right.

Import

import { formatPrice, formatSize } from "@nktkas/hyperliquid/utils";

formatPrice

Formats a price according to Hyperliquid rules:

  • Maximum 5 significant figures

  • Maximum (6 - szDecimals) decimal places for perps, (8 - szDecimals) for spot

  • Integer prices are always allowed regardless of significant figures

import { formatPrice } from "@nktkas/hyperliquid/utils";

// Perpetual (default)
formatPrice("1234.5", 0); // "1234.5" ✓
formatPrice("1234.56", 0); // "1234.5" (truncated to 5 sig figs)
formatPrice("0.001234", 0); // "0.001234" ✓
formatPrice("0.0012345", 0); // "0.001234" (truncated to 5 sig figs)

// Spot market
formatPrice("0.0001234", 0, "spot"); // "0.0001234" ✓
formatPrice("0.00012345", 2, "spot"); // "0.000123" (max 6 decimals for szDecimals=2)

// Integer prices always allowed
formatPrice("123456", 0); // "123456" ✓

Parameters

price (required)

  • Type: string | number

The price to format.

szDecimals (required)

  • Type: number

Size decimals of the asset. Get from SymbolConverter.getSzDecimals() or meta response.

type (optional)

  • Type: "perp" | "spot"

  • Default: "perp"

Market type. Affects maximum decimal places (6 for perp, 8 for spot).

Throws

  • RangeError - if the formatted price becomes 0 after truncation

formatSize

Formats a size by truncating to szDecimals decimal places.

Parameters

size (required)

  • Type: string | number

The size to format.

szDecimals (required)

  • Type: number

Size decimals of the asset. Get from SymbolConverter.getSzDecimals() or meta response.

Throws

  • RangeError - if the formatted size becomes 0 after truncation (prevents accidentally closing entire position)

Example: Placing an Order

Last updated