ethereumsolidityethers.jserc20uniswap

How to calculate sqrtPricex96 for uniswap pool creation?


Hello everyone

I'm trying to create a uniswap pool for my erc20 token (SwapToken) / wETH on sepolia just to figure out how things are working on uniswap V3. I'm having a hard time calculating the sqrtPriceX96 for createAndInitializePoolIfNecessary function in NonfungiblePositionManager contract.

the ratio that i'm going to have in the pool is : 100 SWAP / 0.1 WETH

what is the calculation process of sqrtPriceX96 in javascript? ** I also appreciate it if you check my code and correct me if i have done anything wrong

My code :

const { ethers } = require("ethers");
require("dotenv").config({ path: "../.env" });
const providerUrl = process.env.SEPOLIA_RPC_URL;
const privateKey = process.env.SEPOLIA_PRIVATE_KEY;
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey, provider);

const swapTokenAbi = require("./abi/SwapTokenABI.json");
const wEthAbi = require("./abi/wEthAbi.json");
const positionManagerAbi = require("./abi/NonfungiblePositionManagerABI.json");

const swapTokenAddress = "0x51ffBe766d3b7B9Aa670CaBa326231A053210983";
const wEthAddress = "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14";
const positionManagerAddress = "0x1238536071E1c677A632429e3655c799b22cDA52";

const swapTokenContract = new ethers.Contract(
  swapTokenAddress,
  swapTokenAbi,
  wallet
);

const wEthContract = new ethers.Contract(wEthAddress, wEthAbi, wallet);

const positionManagerContract = new ethers.Contract(
  positionManagerAddress,
  positionManagerAbi,
  wallet
);

const fee = 3000;
const swapTokenAmount = 100;
const wEthAmount = 0.1;
const conversion = swapTokenAmount / wEthAmount;
const sqrtPriceX96 = "???"

async function createPool() {
  let approveTx = await swapTokenContract.approve(
    positionManagerAddress,
    ethers.utils.parseEther(swapTokenAmount.toString())
  );
  console.log(` Swap approve transaction hash : ${approveTx.hash}`);
  let approveReceipt = await approveTx.wait();
  console.log(` Transaction confirmed in block ${approveReceipt.blockNumber}`);

  approveTx = await wEthContract.approve(
    positionManagerAddress,
    ethers.utils.parseEther(wEthAmount.toString())
  );
  console.log(` WETH approve transaction hash : ${approveTx.hash}`);
  approveReceipt = await approveTx.wait();
  console.log(` Transaction confirmed in block ${approveReceipt.blockNumber}`);

  const params = {
    token0: swapTokenAddress,
    token1: wEthAddress,
    fee: fee,
    sqrtPriceX96: "",
  };

  const createPoolTx =
    await positionManagerContract.createAndInitializePoolIfNecessary(params);
  console.log(` Create Pool transaction hash : ${approveTx.hash}`);
  const createPoolReceipt = await approveTx.wait();
  console.log(
    ` Create Pool transaction confirmed in block ${createPoolReceipt.blockNumber}`
  );
}

I tried searching the web and reading the uniswap documents


Solution

  • I found the answer and share it here in case anyone else had the same problem.

    I would appreciate your correction if you found the solution wrong.

    This is how you can calculate the sqrtPriceX96 :

    const yourToken_amount = 1000; // Token0 amount with 0 decimals
    const WETH_amount = 1; // Token1 amount with 0 decimals
    const SqrtPriceX96 = BigInt(Math.sqrt(WETH_amount / yourToken_amount) * 2 ** 96);
    

    and to convert the sqrtPriceX96 back to the price :

    const price = Number(SqrtPriceX96) ** 2 / 2 ** 192;