Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liquidity for univ3prices.getAmountsForLiquidityRange #7

Open
sydneyhenrard opened this issue Aug 22, 2021 · 6 comments
Open

liquidity for univ3prices.getAmountsForLiquidityRange #7

sydneyhenrard opened this issue Aug 22, 2021 · 6 comments

Comments

@sydneyhenrard
Copy link

I tried to use univ3prices.getAmountsForLiquidityRange to get the number of token in ticks that are out of range

// the UNI/USDC pair: https://etherscan.io/address/0xd0fc8ba7e267f2bc56044a7715a489d851dc6d78#readContract
const currentTick = -242755
    const tickSpacing = 60
    const currentRange = univ3prices.utils.tickRange(currentTick, tickSpacing)
    console.log(currentRange) // [ -242760, -242700 ]
    const nextRange = univ3prices.utils.tickRange(currentRange[1], tickSpacing)
    console.log(nextRange) // [ -242700, -242640 ]
    const [amount0, amount1] = univ3prices.getAmountsForLiquidityRange(
        '424427182250808799309705',
        univ3prices.tickMath.getSqrtRatioAtTick(nextRange[0]),
        univ3prices.tickMath.getSqrtRatioAtTick(nextRange[1]),
        647424456336700945, // Where to get this value
    );
    console.log(amount0.toString()) // 361025137982278256382
    console.log(amount1.toString()) // 0

The amount of token 0 is pretty high but I guess it's because I used the liquidity of the pool. Where does this value should come from.

@thanpolas
Copy link
Owner

hi @sydneyhenrard,

I tried to use univ3prices.getAmountsForLiquidityRange to get the number of token in ticks that are out of range

Why would you try to do that?

The calculations are off because of the "out of range" ticks that you used... You'd have to calculate the sqrtPrice and liquidity value for that new tick range to get the correct liquidity...

What exactly is the use case?

@sydneyhenrard
Copy link
Author

sydneyhenrard commented Aug 22, 2021

How is this part of the graph calculated (USDC Locked)?
image

@sydneyhenrard
Copy link
Author

Let's say I want to use that method for next tick on the left. I get the liquidityNet from the tick and add it the liquidity of the current tick.

Each tick tracks Δ𝐿, the total amount of liquidity that should be kicked in or out when the tick is crossed. The tick only needs
to track one signed integer: the amount of liquidity added (or, if negative, removed) when the tick is crossed going left to right

For the first parameter I think I can use the sqrtPrice at the lower tick for new range.

Would it be correct?

@thanpolas
Copy link
Owner

I need to run a few tests to make sure this works, but I am assuming you'd need to use the getAmountsForLiquidityRange () function with the tick range starting from the very start end to the very end - so capturing the entire price-range.

@sydneyhenrard
Copy link
Author

I wonder if the tickStep in getAmountsForCurrentLiquidity is correctly handled. I read several times the whitepaper and I guess the liquidity needs to be updated every time the tick changes by adding the liquidityNet for the tick. I try to figure out the correct parameters but I have a hard time because the math is not my forte. Anyway your lib is very useful and complements the official SDK which is not so easy to use.

@menshenin88
Copy link

Could you also give an example how to use getAmountsForLiquidityRange() function to calculate total reserves? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants