Utilities

Helper functions for price calculations and conversions.

Source: sdk/utils.ts

Price Encoding

encodePrice

Converts a decimal price ratio to sqrtPriceX96 format.

function encodePrice(ratio: string): BigNumber

Parameters:

  • ratio - Decimal price ratio as a string (token1/token0)

Returns:

  • BigNumber - sqrtPriceX96 format

Example:

import { encodePrice } from './sdk/utils';

// 1 WVC = 0.5 USDT (USDT is token0, WVC is token1)
const sqrtPriceX96 = encodePrice('0.5');

// For more precision
const sqrtPriceX96Precise = encodePrice('2000.50');

Note: The ratio parameter is a string to preserve precision for very large or very small numbers.


decodePrice

Converts sqrtPriceX96 to a decimal price ratio.

Parameters:

  • price - Price in sqrtPriceX96 (Q64.96) format

Returns:

  • string - Decimal price ratio as a string

Example:

Note: Returns a string to preserve precision.


Custom Tick Spacing

withCustomTickSpacing

Temporarily overrides the Uniswap SDK's tick spacing for a fee tier while executing a function.

Parameters:

  • fee - Fee tier (e.g., 500, 3000, 10000)

  • tickSpacing - Custom tick spacing to use

  • f - Function to execute with the custom tick spacing

Returns:

  • Promise<T> - Result of the function execution

Example:

Use Case:

VinuSwap allows custom tick spacing per pool, unlike Uniswap V3 which has fixed tick spacing per fee tier. This utility lets you use the Uniswap SDK with VinuSwap's custom configurations.


FixedMathBN

A bignumber.js instance configured for high-precision decimal math.

Configuration:

  • DECIMAL_PLACES: 40 - 40 decimal places of precision

  • EXPONENTIAL_AT: 999999 - Prevents scientific notation for large numbers

Example:


Usage Examples

Complete Price Conversion Flow

Working with Uniswap SDK

Price Calculations with FixedMathBN

Relationship to Uniswap SDK

The VinuSwap SDK utilities are designed to work alongside the Uniswap V3 SDK. For additional functionality like:

  • Tick math (nearestUsableTick, priceToClosestTick, etc.)

  • Path encoding for multi-hop swaps

  • Liquidity calculations

  • Position management

Use the @uniswap/v3-sdk package directly, with withCustomTickSpacing when needed:

Last updated