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

token0Price and token1Price are incorrect #185

Open
NoUJoe opened this issue Aug 14, 2023 · 1 comment
Open

token0Price and token1Price are incorrect #185

NoUJoe opened this issue Aug 14, 2023 · 1 comment

Comments

@NoUJoe
Copy link

NoUJoe commented Aug 14, 2023

See: Uniswap/v3-sdk#182
for full breakdown of the issue. What it boils down to is token0Price and token1Price are incorrectly calculated. I've had a look through, and located the main problem.

let Q192 = 2 ** 192
export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] {

let Q192 = 2 ** 192 type is number, when it should be BigInt or BigDecimal. A lot of precision is lost due to it being a number. It is converted to a BigDecimal within the function but it the precision is already lost by this point. This causes the resulting prices to be incorrect.

This is the most glaring problem, there is a couple of other minor things too. The resulting prices are rounded to 34 significant digits. This is obviously necessary for prices that are small, with a lot of decimal places, but this does mean that large integer prices often have trailing zeros. e.g. 340256786836388081269117234195560700000. I know this isn't the biggest deal as 34 digits of precision is quite a lot, but it seems like this is implicitly happening and not explicitly.

And finally, I've noticed that the token prices are sometimes "0". This is because the prices are only updated in handleSwap ()

@algoWiz007
Copy link

@NoUJoe , I believe that the variables token0Price and token1Price are intended for tracking token prices on an hourly or daily basis.
To obtain the prices for newly added pools, you can derive them by performing calculations based on the supplied liquidity token amounts. There are other approaches as well that you can get the token price - something like fetching the price from the smart contract or you can use Oracle Library.

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

2 participants