Reputation: 51
I am working through extracting price series from Uniswap V3 pools.
I am following the previously discussed method of using ticks to get the price. Most of the time it works without an issue but I noticed there are some odd trades where you get weird numbers.
For an example looking at sUSD-WETH pool, here is a transaction. In it 312.39 sUSD is converted to 0.0917 WETH.
If I simply divide the amounts I get that price of 1 WETH is approximately 3406.65 sUSD which makes sense.
However, if I follow the formula of 1.0001 ** tick * (10 ** (difference in decimals))
I get 1 WETH is 3.4025681E+38 sUSD which is rather odd. Does anyone know why this happens?
sUSD address: 0x57ab1ec28d129707052df4df418d58a2d46d5f51
wETH address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
V3 Pool: 0x6e2a6e92d1c0df4fbf7b35e9aebf2e681c9e6f5f
TX hash: 0x47ce367f7c5321dcf4059cf9feaefe6a9d6d0c52fc30b879cee25fa21eedc0c5`
Upvotes: 2
Views: 2398
Reputation: 154
So the price in a pool will actually have 2 prices. This is due to the need to balance the xy = K computations.
Biggest thing to keep in mind is that these values are in the other token amounts. So like for 1ETH it's usdc value would be (rn) 1419.05....
with the standard math you will get the price of token0 in value of token1, so it's valuable to know which token is token0. Token0 will Always be the Lesser in value of the two token addresses.
[token0, token1] = tokenA.toLowerCase() < tokenB.toLowerCase() ? [tokenA, tokenB] : [tokenB, tokenA]
you can reverse by dividing by 1 (which gives you the price of token1 in value of token0)
let buyOneOfToken0 = ((sqrtPriceX96 / 2**96)**2) / (10**DecimalToken1 / 10**DecimalToken0);
let buyOneOfToken1 = (1 / buyOneOfToken0).toFixed(DecimalToken0);
Upvotes: 1