import { MAX_SQRT_PRICE, MAX_TICK_INDEX, MIN_SQRT_PRICE, MIN_TICK_INDEX } from '../type/clmm' export class TickUtil { /** * Get min tick index. * @param tick_spacing tick spacing * @returns min tick index */ static getMinIndex(tickSpacing: number): number { return MIN_TICK_INDEX + (Math.abs(MIN_TICK_INDEX) % tickSpacing) } /** * Get max tick index. * @param tick_spacing - tick spacing * @returns max tick index */ // eslint-disable-next-line camelcase static getMaxIndex(tickSpacing: number): number { return MAX_TICK_INDEX - (MAX_TICK_INDEX % tickSpacing) } } /** * Get nearest tick by current tick. * * @param tickIndex * @param tickSpacing * @returns */ export function getNearestTickByTick(tickIndex: number, tickSpacing: number): number { const mod = Math.abs(tickIndex) % tickSpacing if (tickIndex > 0) { if (mod > tickSpacing / 2) { return tickIndex + tickSpacing - mod } return tickIndex - mod } if (mod > tickSpacing / 2) { return tickIndex - tickSpacing + mod } return tickIndex + mod } /** * Get the default sqrt price limit for a swap. * * @param a2b - true if the swap is A to B, false if the swap is B to A. * @returns The default sqrt price limit for the swap. */ export function getDefaultSqrtPriceLimit(a2b: boolean): string { return a2b ? MIN_SQRT_PRICE : MAX_SQRT_PRICE } export function getTickSide(afterTick: number, lowerTick: number, upperTick: number) { if (afterTick >= lowerTick && afterTick <= upperTick) { return 'inRange' } if (afterTick < lowerTick) { return 'left' } return 'right' }