Uniswap Polygon UniswapV3 – How to Extract Amounts per Tick from Uniswap V3 Pool

polygonuniswapuniswapv3

I want to be extract the amounts of t0 and t1 from a Uniswap v3 Pool.

I was trying with some method which I found on other post, but I think is not working, or I am doing something wrong.

This is the method tried by me:

function extractAmounts(){
            
    const tick = -282260;
    const liquidity = 31494343572914561514 //liquidityNet
    const sqrtPriceCurrent = 58884551862568005367467
                
    const sqrtPriceLow = Math.pow(1.0001, -282260);
    const sqrtPriceHigh = Math.pow(1.0001, -282250);
                
    const sp = Math.max(Math.min(sqrtPriceCurrent, sqrtPriceHigh), sqrtPriceLow)
                
    const amount0 = liquidity * (sqrtPriceHigh - sp) / (sp * sqrtPriceHigh)
    const amount1 = liquidity * (sp - sqrtPriceLow)
                
    console.log(amount0, amount1)
                
}
extractAmounts();

The values printed in console for the tick(wrote manually by me) are not matching with the chart from Uniswap page (USDC locked value, in the image):

https://info.uniswap.org/#/polygon/pools/0xa374094527e1673a86de625aa59517c5de346d32
enter image description here
The values from the image is for example, and is not matching the values from code.

Someone can explain to me how to calculate the amounts for a tick(current/specific one)?

A pool example(polygon): https://polygonscan.com/address/0xa374094527e1673a86de625aa59517c5de346d32#readContract

Best Answer

There is an NPM library that does all these calculations. It's called univ3prices: https://www.npmjs.com/package/@thanpolas/univ3prices/v/3.0.2

Specifically, check univ3prices.getAmountsForCurrentLiquidity (gets amounts for the current tick space) and univ3prices.getAmountsForLiquidityRange (gets amounts for an arbitrary range)

If you are looking for the exact math, it could be found here: see section 3.3.3 in this whitepaper: atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf