I'm trying to figure out how to convert a uniswap ratio into wei (and eventually ether).

The uniswap docs commonly mention sqrtPriceX96:

A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)

However, I can't figure out how to convert it into wei (and eventually ether).

Here is my C# Code:

BigInteger sqrRtPrice = BigInteger.Parse("1407006288156847466476392762276105");
            var tt = (sqrRtPrice * sqrRtPrice) * (BigInteger)(1e18) / (BigInteger)(1e18) / BigInteger.Pow(((BigInteger)2), 192);
            var eth = Nethereum.Util.UnitConversion.Convert.FromWei(tt);

In this example, eth becomes 0.000000000315379099 which is close to what Uniswap says 0.000315432, but I don't understand why mine is off by 6 decimal places. Both tokens in the pool I'm looking at (WETH-DAI) are 18 decimals long. I'm trying to convert Uniswap sqrtPriceX96 to the price I see on the Uniswap display, but I don't understand why I'm a few decimal places off.

I got my current formula from here, but I also tried the formula here and had the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.