I need help with math of Uniswap V3 virtual and real reserves. Need to find the true reserves of a pair.
Example: let's say we have this DAI/WETH pair: 0x60594a405d53811d3BC4766596EFD80fd545A270
balance of DAI was 2591437269710515203595384
and balance of WETH was 2838497576725882781612
As we can see here, reserve's of tokens in this pool disbalanced:
- DAI value in pool:
2,594,028.706980225
$ (43 %) - WETH value in pool:
3,461,888.414526421
$ (57 %)
If I'm not mistaken, because of concentrated liquidity we have a different virtual reserve's in every "concentrated" zone. Also, those virtual reserve's should be balanced (50/50 - value) by x*y=k. Does anyone know how to find those virtual and/or real reserve's?
I've successfully calculated a price of both tokens:
token0Price = sqrtRatioX96 ** 2 / 2 ** 192 = 2267580935849786082246943435 **2 / 2 **192 = 5.141923300629392e+54 / 6.277101735386681e+57 = 0.0008191556417896165
token1Price = 2 ** 192 / sqrtRatioX96 ** 2 = 6.277101735386681e+57 / 2267580935849786082246943435 **2 = 6.277101735386681e+57 / 5.141923300629392e+54 = 1220.7692274636495
And prices are fine.
Reserves token0:
x = L / sqrt(P)
x = 776218541774739337116335 / sqrt(0.0008191556417896165)
x = 776218541774739337116335 / 0.02862089519
x = 2.7120694046143822e+25
x = 27120694046143822000000000
Reserves token1
y = L * sqrt(P)
# Suppose that sqrt(P) is sqrt(token1Price) then:
y = 776218541774739337116335 * sqrt(1220.7692274636495)
y = 776218541774739337116335 * 34.9395081171
y = 2.7120694040982028e+25
y = 27120694040982028000000000
Comparing x and y:
x = 27120694046143822000000000
y = 27120694040982028000000000
It looks like 50/50, but not by it's token reserves value. I'm not sure, that's my math is correct.
x
andy
are very rarely needed; liquidity is almost always what you want to calculate and use. Real reserves in a pool are complex to calculate because of the non-uniform liquidity. See my answer here stackoverflow.com/a/71815432/2435820token0Price
andtoken1Price
are successfully calculated usingsqrtRatioX96 ** 2 / (2 ** 192)
and(2 ** 192 )/ sqrtRatioX96 ** 2
respectively. But, in V3, how to get thesqrtRatioX96
, as we're havingsqrt_price_x96
derived fromslot0
asslot0. sqrtPriceX96
, so is there a way to calculatesqrtRatioX96
usingsqrtPriceX96
?sqrtRatioX96
andsqrtPriceX96
are used as synonyms in the Uniswap v3 code, they're just different names for the same thing.