Creating Pangea Pool

This is an example of getting information of all pools currently deployed in Pangea. You must create a pool first and then deposit your assets in the pool for the swap to proceed normally.

Caution The created pool cannot be deleted, and it cannot be recreated with same parameters (token0/token1/swapFee/tickSpacing). Please test enough in the test network or local network.

import { ethers } from "ethers";
import {BigNumber} from "@ethersproject/bignumber";

/// MasterDeployer address
const masterDeployerAddress = "0x..."; 
const poolFactoryAddress = "0x....";

/// web3 Provider (metamask provider / jsonRPC provider / ...)
const provider = ethers.getDefaultProvider();

function createPool(
  token0Address:string, /// Token0 address
  token1Address:string, /// Token1 address
  swapFee:number,       /// Swap Fee Rate(unit : 1000 = 0.1%)
  amount0:BigNumberish, /// Have to set relative amount in order to calculate price
  amount1:BigNumberish  /// if amount0 : amount1 = 1 : 10, price = (√10 * 2^96) 
) {
    // requires token0Address < token1Address
    if(token0Address.toLowerCase() > token1Address.toLowerCase()){
        /// checksum address 
        throw new Error("invalid token order");
    }
    
    /// swapFee can be set 1%, 0.2%, 0.06%, 0.01%
    if (swapFee == 10_000 && swapFee == 2_000 && swapFee != 600 && swapFee != 100) throw new Error("invalid swap Fee");
    
    /// Sets proper tickSpacing in accordance with swapFee
    
    const tickSpacing = getTickSpacing(swapFee)
    
    /// Price calculation (√(token1/token0*2^196)) 
    const price = sqrtValue(
      BigNumber.from(2).pow(192).mul(givenAmount1).div(givenAmount0)
    );
    
    const masterDeployer = await MasterDeployer__factory.connect(
      masterDeployerAddress, provider);
    
    /// Deploys Pangea pool
    /// If the pool with same parameters exists, pool creation will fail

    const tx = await masterDeployer.deployPool(poolFactoryAddress, 
      ethers.utils.defaultAbiCoder.encode(
        ["address","address", "uint24","uint160","uint24"],
        [token0Address, token1Address, swapFee, price, tickSpacing]
    );
    const receipt = await tx.wait();
    
    /// Gets the address of the deployed pool
    /// unique by (token0Address, token1Address, swapFee, tickSpacing) 
    const poolFactory = await ConcentratedLiquidityPoolFactory__factory.connect(
      poolFactoryAddress, provider);
    const poolAddress = poolFactory.configAddress(
      ethers.utils.defaultAbiCoder.encode(
        ["address","address", "uint24","uint24"],
        [token0Address, token1Address, swapFee, tickSpacing]
    );
    console.log(`deployed pool address : ${poolAddress}`);   
}

/// calculates tick spacing in accordance with swap fee

function getTickSpacing(feeAmount: number) {
    const feeUnit = 100; // 0.01%
    return Math.round(feeAmount / feeUnit);
}

/// sqrt calculation

function sqrtValue(value) {
  const ONE = BigNumber.from(1);
  const TWO = BigNumber.from(2);

  let x = BigNumber.from(value);
  let z = x.add(ONE).div(TWO);
  let y = x;
  while (z.sub(y).isNegative()) {
    y = z;
    z = x.div(z).add(z).div(TWO);
  }
  return y;
}

Last updated