I want to get the amounts of the tokens for a tick range which is higher or lower than the current tick.
For example I have current tick: -282986 with range [-282990, -282980], I want to get the amounts for the tick range [-282980, -282970] (next higher tick range), but I have no success.
I am using this formula:
if(sqrtPrice <= sqrtPriceLower){
amount0 = (liquidity * ((sqrtPriceUpper - sqrtPriceLower) / (sqrtPriceLower * sqrtPriceUpper))) / Math.pow(10, 18);
}else if(sqrtPrice >= sqrtPriceUpper){
amount1 = (liquidity * ( sqrtPriceUpper - sqrtPriceLower) ) / Math.pow(10, 6);
}else{
amount0 = (liquidity * ((sqrtPriceUpper - sqrtPrice) / (sqrtPrice * sqrtPriceUpper))) / Math.pow(10, 18);
amount1 = (liquidity * (sqrtPrice - sqrtPriceLower) ) / Math.pow(10, 6);
}
I have read somewhere that for this situations I need to take in calculation the liquidityNet, but I have no idea how to use it, if the information is right.
Someone can help me withe formula for ranges which are out of the current tick?
Best Answer
You can use the
@uniswap/v3-core
and@uniswap/v3-periphery
libraries as shown in the following contract, the uniswap v3 libraries are all open source so feel free to dive into the solidity implementation code.Now to get the
amount{0&1}
per unit of liquidity minted given the current price corresponds exactly with the current tick(this is a close enough approximation in any case i.e. at most 0.01% off) you can callUniswapV3Liquidity.getAmountsPerUnitOfLiquidity(-282986, -282980, -282970)
which yields the result: