0

I am trying to run the scripts for uniswap V3. I am able to mint the positions and check the position but I cannot increase or decrease the liquidity by calling these functions. I get the following error:

    Error: invalid BigNumber value (argument="value", value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.7.0)
        at Logger.makeError (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\logger\src.ts\index.ts:269:28)
        at Logger.throwError (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\logger\src.ts\index.ts:281:20)
        at Logger.throwArgumentError (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\logger\src.ts\index.ts:285:21)
        at Function.BigNumber.from (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\bignumber\src.ts\bignumber.ts:289:23)
        at NumberCoder.encode (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\abi\src.ts\coders\number.ts:25:27)
        at C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\abi\src.ts\coders\array.ts:71:19
        at Array.forEach (<anonymous>)
        at pack (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\abi\src.ts\coders\array.ts:54:12)
        at TupleCoder.encode (C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\abi\src.ts\coders\tuple.ts:54:20)
        at C:\Users\moham\OneDrive\Documents\OptimusFox\hardhat\node_modules\@ethersproject\abi\src.ts\coders\array.ts:71:19 {
      reason: 'invalid BigNumber value',
      code: 'INVALID_ARGUMENT',
      argument: 'value',
      value: undefined

here is my script

    // Uniswap contract addresses
    WETH_ADDRESS= '0x5FbDB2315678afecb367f032d93F642f64180aa3'
    FACTORY_ADDRESS= '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512'
    SWAP_ROUTER_ADDRESS= '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0'
    NFT_DESCRIPTOR_ADDRESS= '0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9'
    POSITION_DESCRIPTOR_ADDRESS= '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9'
    POSITION_MANAGER_ADDRESS= '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707'
    
    // Pool addresses
    USDT_USDC_500= '0x1FA8DDa81477A5b6FA1b2e149e93ed9C7928992F'
    
    // Token addresses
    TETHER_ADDRESS= '0x0165878A594ca255338adfa4d48449f69242Eb8F'
    USDC_ADDRESS= '0xa513E6E4b8f2a923D98304ec87F64353C4D5C853'
    WRAPPED_BITCOIN_ADDRESS= '0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6'
    
    const artifacts = {
      NonfungiblePositionManager: require("@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json"),
      Usdt: require("../artifacts/contracts/Tether.sol/Tether.json"),
      Usdc: require("../artifacts/contracts/UsdCoin.sol/UsdCoin.json"),
      UniswapV3Pool: require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json"),
    };
    
    const { Contract } = require("ethers")
    const { Token } = require('@uniswap/sdk-core')
    const { Pool, Position, nearestUsableTick } = require('@uniswap/v3-sdk')
    const { ethers } = require("hardhat")
    
    async function getPoolData(poolContract) {
      const [tickSpacing, fee, liquidity, slot0] = await Promise.all([
        poolContract.tickSpacing(),
        poolContract.fee(),
        poolContract.liquidity(),
        poolContract.slot0(),
      ])
      // console.log("Tick Spacing", await poolContract.tickSpacing() )
      // console.log("Fee",await poolContract.fee())
      // console.log("Liquidity", await poolContract.liquidity())
      // console.log("Slot 0",await poolContract.slot0() )
    
      return {
        tickSpacing: tickSpacing,
        fee: fee,
        liquidity: liquidity,
        sqrtPriceX96: slot0[0],
        tick: slot0[1],
      }
    }
    
    async function main() {
      const [owner, signer2] = await ethers.getSigners();
      const provider = waffle.provider;
    
      const usdtContract = new Contract(TETHER_ADDRESS,artifacts.Usdt.abi,provider)
      const usdcContract = new Contract(USDC_ADDRESS,artifacts.Usdc.abi,provider)
    
      await usdtContract.connect(signer2).approve(POSITION_MANAGER_ADDRESS, ethers.utils.parseEther('1000'))
      await usdcContract.connect(signer2).approve(POSITION_MANAGER_ADDRESS, ethers.utils.parseEther('1000'))
    
      const poolContract = new Contract(USDT_USDC_500, artifacts.UniswapV3Pool.abi, provider)
    
      const poolData = await getPoolData(poolContract)
      // console.log("POOL DATA", poolData);
    
      const UsdtToken = new Token(31337, TETHER_ADDRESS, 18, 'USDT', 'Tether')
      const UsdcToken = new Token(31337, USDC_ADDRESS, 18, 'USDC', 'UsdCoin')
    
      const pool = new Pool(
        UsdtToken,
        UsdcToken,
        poolData.fee,
        poolData.sqrtPriceX96.toString(),
        poolData.liquidity.toString(),
        poolData.tick
      )
    
      const position = new Position({
        pool: pool,
        liquidity: ethers.utils.parseEther('1'),
        tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 2,
        tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 2,
      })
    
      const { amount0: amount0Desired, amount1: amount1Desired} = position.mintAmounts
    
      params = {
        token0: TETHER_ADDRESS,
        token1: USDC_ADDRESS,
        fee: poolData.fee,
        tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 2,
        tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 2,
        amount0Desired: amount0Desired.toString(),
        amount1Desired: amount1Desired.toString(),
        amount0Min: 0,
        amount1Min: 0,
        recipient: signer2.address,
        deadline: Math.floor(Date.now() / 1000) + (60 * 10)
      }
    
      const nonfungiblePositionManager = new ethers.Contract(
        POSITION_MANAGER_ADDRESS,
        artifacts.NonfungiblePositionManager.abi,
        provider
      )
    
      const tx = await nonfungiblePositionManager.connect(signer2).mint(
        params,
        { gasLimit: '1000000' }
      )
    
    
      const receipt = await tx.wait()
      const event1 = await receipt.events[6];
      // console.log("EVENT1-----------", event1)
      let value = event1.args[0]
      let value1 = event1.args[1] - 10000
      let value2 = event1.args[2]- 10000
      let value3 = event1.args[3]- 10000
      console.log("VALUE---------", value)
      console.log("VALUE1---------", value1)
      console.log("VALUE-2--------", value2)
      console.log("VALUE--3-------", value3)
    
      const deadline = Math.floor(Date.now()/1000) + (60 * 10)
      const increaseLiquidity_ = await nonfungiblePositionManager.connect(signer2).increaseLiquidity(
        {value,
        value1,
        value2 ,
        value3,
        deadline}, 
        {gasLimit: '10000000000'}
      )
      
    
    }
    
    /*
    npx hardhat run --network localhost scripts/04_addLiquidity.js
    */
    
    main()
      .then(() => process.exit(0))
      .catch((error) => {
        console.error(error);
        process.exit(1);
      });

My addLiquidity script:

// Uniswap contract addresses
WETH_ADDRESS= '0x5FbDB2315678afecb367f032d93F642f64180aa3'
FACTORY_ADDRESS= '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512'
SWAP_ROUTER_ADDRESS= '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0'
NFT_DESCRIPTOR_ADDRESS= '0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9'
POSITION_DESCRIPTOR_ADDRESS= '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9'
POSITION_MANAGER_ADDRESS= '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707'

// Pool addresses
USDT_USDC_500= '0x1FA8DDa81477A5b6FA1b2e149e93ed9C7928992F'

// Token addresses
TETHER_ADDRESS= '0x0165878A594ca255338adfa4d48449f69242Eb8F'
USDC_ADDRESS= '0xa513E6E4b8f2a923D98304ec87F64353C4D5C853'
WRAPPED_BITCOIN_ADDRESS= '0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6'

const artifacts = {
  NonfungiblePositionManager: require("@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json"),
  Usdt: require("../artifacts/contracts/Tether.sol/Tether.json"),
  Usdc: require("../artifacts/contracts/UsdCoin.sol/UsdCoin.json"),
  UniswapV3Pool: require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json"),
};


const { Contract } = require("ethers")
const { Token } = require('@uniswap/sdk-core')
const { Pool, Position, nearestUsableTick } = require('@uniswap/v3-sdk')
const { ethers } = require("hardhat")

async function getPoolData(poolContract) {
  const [tickSpacing, fee, liquidity, slot0] = await Promise.all([
    poolContract.tickSpacing(),
    poolContract.fee(),
    poolContract.liquidity(),
    poolContract.slot0(),
  ])
  console.log("HERE")
  // console.log("Tick Spacing", await poolContract.tickSpacing() )
  // console.log("Fee",await poolContract.fee())
  // console.log("Liquidity", await poolContract.liquidity())
  // console.log("Slot 0",await poolContract.slot0() )

  return {
    tickSpacing: tickSpacing,
    fee: fee,
    liquidity: liquidity,
    sqrtPriceX96: slot0[0],
    tick: slot0[1],
  }
}

async function main() {
  console.log("here")
  console.log(ethers)
  const owner = await ethers.getSigners();
  
  const provider = waffle.provider;
  
  const usdtContract = new Contract(TETHER_ADDRESS,artifacts.Usdt.abi,provider)
  const usdcContract = new Contract(USDC_ADDRESS,artifacts.Usdc.abi,provider)
  
  await usdtContract.connect(signer2).approve(POSITION_MANAGER_ADDRESS, ethers.utils.parseEther('1000'))
  await usdcContract.connect(signer2).approve(POSITION_MANAGER_ADDRESS, ethers.utils.parseEther('1000'))

  const poolContract = new Contract(USDT_USDC_500, artifacts.UniswapV3Pool.abi, provider)

  const poolData = await getPoolData(poolContract)
  // console.log("POOL DATA", poolData);
  
  const UsdtToken = new Token(31337, TETHER_ADDRESS, 18, 'USDT', 'Tether')
  const UsdcToken = new Token(31337, USDC_ADDRESS, 18, 'USDC', 'UsdCoin')

  const pool = new Pool(
    UsdtToken,
    UsdcToken,
    poolData.fee,
    poolData.sqrtPriceX96.toString(),
    poolData.liquidity.toString(),
    poolData.tick
  )

  const position = new Position({
    pool: pool,
    liquidity: ethers.utils.parseEther('1'),
    tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 2,
    tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 2,
  })

  const { amount0: amount0Desired, amount1: amount1Desired} = position.mintAmounts

  params = {
    token0: TETHER_ADDRESS,
    token1: USDC_ADDRESS,
    fee: poolData.fee,
    tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 2,
    tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 2,
    amount0Desired: amount0Desired.toString(),
    amount1Desired: amount1Desired.toString(),
    amount0Min: 0,
    amount1Min: 0,
    recipient: signer2.address,
    deadline: Math.floor(Date.now() / 1000) + (60 * 10)
  }

  const nonfungiblePositionManager = new ethers.Contract(
    POSITION_MANAGER_ADDRESS,
    artifacts.NonfungiblePositionManager.abi,
    provider
  )

  const tx = await nonfungiblePositionManager.connect(signer2).mint(
    params,
    { gasLimit: '1000000' }
  )


  const receipt = await tx.wait()
  const event1 = await receipt.events[6];
  // console.log("EVENT1-----------", event1)
  let value = event1.args[0]
  let value1 = event1.args[1] - 10000
  let value2 = event1.args[2]- 10000
  let value3 = event1.args[3]- 10000
  console.log("VALUE---------", value)
  console.log("VALUE1---------", value1)
  console.log("VALUE-2--------", value2)
  console.log("VALUE--3-------", value3)

  const deadline = Math.floor(Date.now()/1000) + (60 * 10)
  const increaseLiquidity_ = await nonfungiblePositionManager.connect(signer2).decreaseLiquidity(
    {value,
    value1,
    value2 ,
    value3,
    deadline}, 
    {gasLimit: '10000000000'}
  )
  // console.log("receipt", receipt)
  // nonfungiblePositionManager.on("IncreaseLiquidity", (tokenId, liquidity, amount0, amount1) =>{
  //   console.log("JEREREE")
  // let info ={
  //   tokenId: tokenId,
  //   liquidity: liquidity,
  //   amount0: ethers.utils.formatEther(amount0, 6),
  //   amount1: ethers.utils.formatEther(amount1, 6)
  // };
  // console.log("HERE",JSON.stringify(info, null, 4));
// })

}

/*
npx hardhat run --network localhost scripts/04_addLiquidity.js
*/

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
3
  • can you post your 04_addLiquidity.js file?
    – immaxkent
    Jan 9, 2023 at 11:34
  • Added. can you have a look at it, please? Jan 10, 2023 at 7:54
  • @Nunce.ether Usually, that error means a variable wasn't initialized properly. Check if the returned data matches the way it is being used.
    – Ismael
    Jan 12, 2023 at 1:42

0

Your Answer

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

Browse other questions tagged or ask your own question.