@somnia-chain/markets-sdk


@somnia-chain/markets-sdk / index / roundPriceToTick

Function: roundPriceToTick()

roundPriceToTick(price, tickSize, direction): bigint

Defined in: packages/sdk/src/units.ts:728

Snap a raw price onto the venue's tickSize, refusing to produce a price the pool rejects outright. snapRawToGrid with the zero guard a price needs and a quantity must not have.

Details

The pool checks price != 0 before it checks tick alignment, so a price under one tick that floors to 0 reverts InvalidPrice — and reverts it for a reason the caller did not choose. A positive price below one tick therefore comes back as one whole tick, the nearest price that exists on the grid.

A non-positive price is a caller mistake rather than a rounding question, so it throws instead of being nudged onto the grid.

direction belongs to the value: a resting limit rounds toward the caller, a crossing limit rounds away so it still sweeps. See snapRawToGrid.

Parameters

price

bigint

tickSize

bigint

direction

RawGridDirection

Returns

bigint

Example

Align a resting buy limit that may sit below one tick

ts
import { roundPriceToTick } from "@somnia-chain/markets-sdk";

const tickSize = 1_000_000_000_000_000n; // 1e15, an 18-decimal venue

roundPriceToTick(2_500_000_000_000_000n, tickSize, "down"); // 2e15, floored
roundPriceToTick(2_500_000_000_000_000n, tickSize, "up"); // 3e15, ceiled
roundPriceToTick(400_000_000_000_000n, tickSize, "down"); // 1e15, never 0n

Throws

when tickSize or price is not positive.