@alcorexchange/alcor-swap-sdk
Version:
**npm** ``` npm i @alcorexchange/alcor-swap-sdk ``` **yarn** ``` yarn add @alcorexchange/alcor-swap-sdk ``` ## Usage ### Import:
15 lines (14 loc) • 743 B
JavaScript
import invariant from "tiny-invariant";
import { TickMath } from "./tickMath";
/**
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
* @param tick the target tick
* @param tickSpacing the spacing of the pool
*/
export function nearestUsableTick(tick, tickSpacing) {
invariant(Number.isInteger(tick) && Number.isInteger(tickSpacing), "INTEGERS");
invariant(tickSpacing > 0, "TICK_SPACING");
invariant(tick >= TickMath.MIN_TICK && tick <= TickMath.MAX_TICK, "TICK_BOUND");
const rounded = Math.round(tick / tickSpacing) * tickSpacing;
if (rounded < TickMath.MIN_TICK) return rounded + tickSpacing;else if (rounded > TickMath.MAX_TICK) return rounded - tickSpacing;else return rounded;
}