exchange.IO("uniswapV3", ...)
Performs concentrated liquidity (Uniswap V3) calculations, including conversions between tick, price, and sqrtPriceX96, as well as conversions between liquidity and token amounts. It is applicable to protocols that use the same formulas, such as Uniswap v3/v4, PancakeSwap v3, Aerodrome Slipstream, and SushiSwap v3.
exchange.IO(k, fn, ...args)Examples
Read the price of a Uniswap V3 pool and calculate the token amounts required to provide liquidity around the current price.
javascript
function main() {
var C = exchange.IO("contracts")
var T = C.tokens, U = C.uniswapV3
exchange.IO("abi", U.factory, "uniswapV3Factory")
var pool = exchange.IO("api", U.factory, "getPool", T.USDC.address, T.WETH.address, 500)
exchange.IO("abi", pool, "uniswapV3Pool")
var s = exchange.IO("api", pool, "slot0")
// In this pool, token0 is USDC (6 decimals) and token1 is WETH (18 decimals): the price
// indicates how much WETH 1 USDC is worth
var p = exchange.IO("uniswapV3", "sqrtPriceToPrice", s.sqrtPriceX96, 6, 18)
Log("tick:", s.tick, "ETH price:", (1 / p).toFixed(2), "USDC")
// Set the range to ±5% centered on the current price, with ticks rounded down to tickSpacing
// (10 for the 0.05% fee tier pool)
var lower = exchange.IO("uniswapV3", "priceToTick", p * 0.95, 6, 18, 10)
var upper = exchange.IO("uniswapV3", "priceToTick", p * 1.05, 6, 18, 10) + 10
// The liquidity that can be provided when depositing at most 1000 USDC and 0.4 WETH, and the
// amounts actually used
var liq = exchange.IO("uniswapV3", "liquidityForAmounts", s.sqrtPriceX96, lower, upper,
exchange.IO("toUnits", "1000", 6), exchange.IO("toUnits", "0.4", 18))
var used = exchange.IO("uniswapV3", "amountsForLiquidity", s.sqrtPriceX96, lower, upper, liq)
Log("Range tick:", lower, "~", upper, "Liquidity:", liq)
Log("Actual deposit:", exchange.IO("fromUnits", used.amount0, 6), "USDC +",
exchange.IO("fromUnits", used.amount1, 18), "WETH")
}Returns
| Type | Description |
number / string / object | Price-related functions return numeric values (floating-point numbers, used for display and price level estimation); tick-related functions return integers; sqrtPriceX96, liquidity, and amount-related functions return decimal strings (exact values). |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of the |
fn | string | Yes | The name of the calculation function, for example |
args | string / number | Yes | The parameters of the calculation function. The precision parameters |
See Also
Remarks
The conversions between tick and sqrtPriceX96, and between liquidity and token amounts, are bit-for-bit ports of the contract's TickMath and LiquidityAmounts algorithms, and the calculation results are fully consistent with the on-chain contracts; price conversions use floating-point arithmetic and are subject to floating-point precision errors.
The token0 and token1 in a pool are sorted by contract address, and the price direction is the amount of token1 that 1 token0 can be exchanged for. You need to confirm the pool's token0 before performing calculations.
Calculation functions for fn (each followed by its arguments):
tickToPrice(tick, decimals0, decimals1): Calculates the price corresponding to the tick (the amount of token1 that 1 token0 can be exchanged for).priceToTick(price, decimals0, decimals1[, tickSpacing]): Calculates the largest tick not higher than the given price; when tickSpacing is passed in, the result is rounded down to an integer multiple of it.sqrtPriceToPrice(sqrtPriceX96, decimals0, decimals1): Converts the sqrtPriceX96 in the pool's slot0 to a price.tickToSqrtPrice(tick): Calculates the sqrtPriceX96 corresponding to the tick (consistent with the result of the contract's TickMath.getSqrtRatioAtTick).sqrtPriceToTick(sqrtPriceX96): Calculates the tick in which the sqrtPriceX96 is located (consistent with the result of the contract's TickMath.getTickAtSqrtRatio).nearestUsableTick(tick, tickSpacing): Rounds the tick to the nearest integer multiple of tickSpacing.amountsForLiquidity(sqrtPriceX96, tickLower, tickUpper, liquidity): Calculates the token amounts {amount0, amount1} corresponding to the liquidity within the range at the current price (consistent with the contract's calculation when removing liquidity).liquidityForAmounts(sqrtPriceX96, tickLower, tickUpper, amount0, amount1): Calculates the maximum liquidity that can be provided without exceeding the given token amounts.