1

The uniswap docs commonly mention sqrtPriceX96:

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

Calculate in JS: https://github.com/Uniswap/v3-sdk/blob/2c8aa3a653831c6b9e842e810f5394a5b5ed937f/src/utils/encodeSqrtRatioX96.ts

how to calculate sqrtPriceX96 in solidity?

uint160 sqrtPriceX96 = ???;

3
  • Calculate from what?
    – kfx
    Commented Feb 17, 2022 at 7:01
  • @kfx I create pool. NonfungiblePositionManager(payable(Manager)) .createAndInitializePoolIfNecessary( address(MY_TOKEN), wMaticAddress, 3000, sqrtPriceX96 ); Commented Feb 17, 2022 at 15:44
  • 1
    If you create a pool, you need to pass the initial price as the argument. Assuming you know what initial price you want, the easiest way would be to calculate the sqrtPriceX96 offline and pass that as a constant in Solidity code. Otherwise look at the SqrtPriceMath library for some examples how do similar things.
    – kfx
    Commented Feb 17, 2022 at 19:46

3 Answers 3

0

What variables are available to you? The easiest way is just to ask Uniswap V3 what the current sqrtPriceX96 is.

2
  • I create pool. NonfungiblePositionManager(payable(Manager)) .createAndInitializePoolIfNecessary( address(MY_TOKEN), wMaticAddress, 3000, sqrtPriceX96 ); and sqrtPriceX96? Commented Feb 17, 2022 at 15:43
  • kfx has a good answer, the easiest way is just to pass it from offline
    – Meriadoc
    Commented Feb 18, 2022 at 2:56
0

This should work with all decimals:

function getPrice(uint160 sqrtRatioX96, uint dec0, uint dec1)
external
pure
returns (uint256 price)
{
uint256 dec = dec1<=dec0 ? (18-dec1)+dec0 :dec0;
uint256 numerator1 =uint256(sqrtRatioX96) *uint256(sqrtRatioX96);  
uint256 numerator2 =10**dec; 
price = FullMath.mulDiv(numerator1, numerator2, 1 << 192);
}
3
  • He is asking how to get sqrtPriceX96 and you have that in the parameters of function, this is wrong
    – Blissful
    Commented Mar 31 at 22:16
  • User asked how to calculate sqrtRatioX96 in Solidity, so I assumed he wants to derive the actual token price from sqrtRatio, theres no use of calculating sqrtRatio in contract as it uses more gas. You get sqrtRatioX96 from V3 pair contract, and this is how you convert it to price that can be used in solidity, adjusted to 18 decimals. If you want to see how sqrtRatio is calculated you can refer to Uni docs. Someone also posted an answer here. ethereum.stackexchange.com/questions/98685/…
    – Fićo
    Commented Apr 7 at 18:46
  • 1
    Think the question is not properly formed, user probably wants to know how to calculate initial sqrtRatio for initialization of pool.
    – Blissful
    Commented Apr 8 at 14:06
-1

This way:

const sqrtPriceToPrice = (sqrtPriceX96, token0Decimals, token1Decimals) => {
    let mathPrice = Number(sqrtPriceX96) ** 2 / 2 ** 192;
    const decimalAdjustment = 10 ** (token0Decimals - token1Decimals);
    const price = mathPrice * decimalAdjustment;
    return price;
};

2
  • 1
    He is asking how to get sqrtPriceX96 and you have that in the parameters of function, this is wrong
    – Blissful
    Commented Mar 31 at 22:17
  • He can revert the math if he wants. You know that you also loses one point when you down vote an awnser, right? Commented Apr 5 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.