View Source Hyperliquid.Utils.Format (hyperliquid v0.2.2)
Formatting utilities for Hyperliquid prices and sizes.
Implements Hyperliquid's tick and lot size rules:
- Prices: Maximum 5 significant figures, max 6 (perp) or 8 (spot) - szDecimals decimals
- Sizes: Truncated to szDecimals decimal places
See: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/tick-and-lot-size
Summary
Functions
Format price according to Hyperliquid rules.
Format size according to Hyperliquid rules.
Get the maximum allowed price decimals for an asset.
Truncate to a certain number of decimal places.
Truncate to a certain number of significant figures.
Functions
Format price according to Hyperliquid rules.
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
Parameters
price: Price as string, integer, or floatsz_decimals: Size decimals for the assetopts: Options:perp- true for perpetual (default), false for spot
Examples
iex> Format.format_price("50000.123456", 5)
"50000"
iex> Format.format_price("0.0000123456789", 0, perp: false)
"0.00001234"
iex> Format.format_price(50000, 5)
"50000"
Format size according to Hyperliquid rules.
Rules:
- Truncate decimal places to szDecimals
Parameters
size: Size as string, integer, or floatsz_decimals: Size decimals for the asset
Examples
iex> Format.format_size("1.23456789", 5)
"1.23456"
iex> Format.format_size(0.001, 3)
"0.001"
Get the maximum allowed price decimals for an asset.
Parameters
asset_id: Asset indexsz_decimals: Size decimals for the asset
Returns
Maximum decimal places allowed for price
Truncate to a certain number of decimal places.
Examples
iex> Format.to_fixed_truncate("1.23456789", 5)
"1.23456"
iex> Format.to_fixed_truncate("100.999", 2)
"100.99"
Truncate to a certain number of significant figures.
Examples
iex> Format.to_precision_truncate("123456", 5)
"123450"
iex> Format.to_precision_truncate("0.00012345", 3)
"0.000123"