Hyperliquid JavaScript SDK example: rest-rounding.ts

Hyperliquid REST rounding JavaScript example for the Siebly Hyperliquid SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Hyperliquid/Rest/rest-rounding.ts

What this example covers

  • Hyperliquid REST API JavaScript example.
  • Uses the Siebly Hyperliquid SDK package @siebly/hyperliquid-api instead of hand-written HTTP request plumbing.
  • Source path: Hyperliquid/Rest/rest-rounding.ts.
  • Imports SDK symbols including RestClient.
  • Calls SDK methods such as getPerpMetadata(), resolveAsset().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { RestClient } from '@siebly/hyperliquid-api'; // or// import { RestClient } from '@siebly/hyperliquid-api'; /** * Hyperliquid tick/lot rules, for when you send raw submitOrder() wires. * * submitMarketOrder() already rounds size down and builds the IOC protection * price. Use this example when you need the same rules for a manual wire. * * Prices: 5 significant figures, at most 6 (perp) or 8 (spot) minus szDecimals. * Integer prices are always allowed. * * npx tsx examples/Rest/rest-rounding.ts */const client = new RestClient({ testnet: true }); function roundPrice(  px: number,  szDecimals: number,  maxDecimals: number,): number {  if (px > 100_000) {    return Math.round(px);  }  const decimals = maxDecimals - szDecimals;  return Number(Number(px.toPrecision(5)).toFixed(decimals));} async function main() {  try {    const meta = await client.getPerpMetadata();    const szDecimals = Object.fromEntries(      meta.universe.map((asset) => [asset.name, asset.szDecimals]),    );    console.log('sample szDecimals', {      BTC: szDecimals.BTC,      ETH: szDecimals.ETH,      OP: szDecimals.OP,    });     const coin = 'OP';    const maxDecimals = 6;    const px = roundPrice(      1.2345678,      Number(szDecimals[coin] ?? 0),      maxDecimals,    );    const sz = Number((12.345678).toFixed(Number(szDecimals[coin] ?? 0)));    console.log('rounded px/sz for', coin, px, sz);     const asset = await client.resolveAsset(coin);    console.log('resolved asset:', asset);  } catch (e) {    console.error('request failed: ', e);  }} main(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.