Uniswap / v3-core

🦄 🦄 🦄 Core smart contracts of Uniswap v3
https://uniswap.org
Other
4.38k stars 2.68k forks source link

Price discrepancy between tick price and SqrtPriceX96 #578

Open sh1ng opened 2 years ago

sh1ng commented 2 years ago

https://etherscan.io/tx/0xcce64adbee9cca48db599f6cb60d34b8cc52afa4603ece1441c7986329f18e0f

tick - 202475
SqrtPriceX96 - 1974045567390486984838358761822072

The below code is in python, but I got the same in golang

>>> math.pow(1.0001, 202475)
620780237.5507622
>>> (1974045567390486984838358761822072. * math.pow(2., -96)) ** 2
620804961.6538478
>>> 

and to get the human-readable price we need to invert it and mul by 1e12

>>> 1e12/math.pow(1.0001, 202475)
1610.87602264115
>>> 1e12/(1974045567390486984838358761822072. * math.pow(2., -96)) ** 2
1610.8118680881066

I expect that you use an approximation somewhere. Could you provide more details? What value is used to perform a swap?

zemse commented 2 years ago

Tick is a 24-bit value which I believe is being maintained for twat observations. While SqrtPriceX96 contains more precision and is used for swap calculations, and based on it the tick is calculated and stored in the observation slots.

reuptaken commented 1 year ago

I found similar issue in transaction https://etherscan.io/tx/0x8bd6db6ee086f571da01b65101ca3368086f6184a662693e1c57d671b40bfbd6#eventlog

Price is supposedly in 203999 tick, when after calculating it, it goes into tick 204000.

What's interesting, this kind of discrepancy is relatively new. I've checked thousands of swaps and never encountered one before ~ August 2022.