0

I am trying to add liquidity to uniswap pool using SwapRouter but I get a gas error. I am working on hardhat fork.

 const { ethers } = require('ethers');
 const dotenv = require('dotenv').config();
  const { IpcProvider } = require("@ethersproject/providers");
   IERC20 = require('./abis/abi.json');


 const uniswapV3RouterAddressV3 = '0xE592427A0AEce92De3Edee1F18E0157C05861564';
 const UNISWAP_V3_SwapRouterV3 = require("./abis/SwapRouterV3.json");


  // set up provider, primary and secondary addresses
const {toBytes32, toString, toWei, toEther, toRound } = require('./modules/utils');


 // set up primary and secondary addresses
 const {provider, acct1, acct2, testAcct, privateKey, signer, account } =                     require("./modules/accts");



// Get the Uniswap V3 router contract 
  const uniswapV3RouterContract = new ethers.Contract(uniswapV3RouterAddressV3,        UNISWAP_V3_SwapRouterV3 , account);

  const [tokenAAddress, tokenBAddress] = ['address', 'addrss']; // replace with token   addresses
 const [tokenA, tokenB] = [new ethers.Contract(tokenAAddress, IERC20, account), new   ethers.Contract(tokenBAddress, IERC20, account)];
   const [amountA, amountB] = [ethers.utils.parseUnits('100', '18'), ethers.utils.parseUnits('100', '18')]; // replace with token amounts
 const slippageTolerance = 1; // 1%
 const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes

  const start = async () => { console.log("add liqudity to the pool baby, baby...");

 params= [    
    tokenAAddress,
    tokenBAddress,
    100,
    acct1,
    deadline,
    amountB,
    amountB.sub(amountB.mul(slippageTolerance)),
    0,
    ethers.utils.hexlify(500),
    ethers.utils.parseUnits('1000000000', 'gwei')
    ]

   const tx = await uniswapV3RouterContract.connect(account).exactInputSingle(params);`


    console.log(`Adding liquidity to Uniswap V3 pool with transaction hash: ${tx}`);
   await tx.wait();
   console.log('Liquidity added successfully!');
     }

I updated the gas via hardhat config and added directly calling the function of the smart contract.

Please point me to the right direction. I am thankful in advance.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
jayyyo
  • 109
  • 4

1 Answers1

1

I think that is because you are passing the wrong params and metamask cannot estimate the gas. Before metamask runs the transaction, it makes a free static call to the Ethereum node to get the result without making any state change. this is the params struct:

struct ExactInputSingleParams {
    address tokenIn;
    address tokenOut;
    uint24 fee;
    address recipient;
    uint256 deadline;
    uint256 amountIn;
    uint256 amountOutMinimum;
    uint160 sqrtPriceLimitX96;
}

this has 8 params, but you are passing 10 params

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • I updated and still have gas issue: const tx = await uniswapV3RouterContract.exactInputSingle({ tokenIn: tokenAAddress, tokenOut: tokenBAddress, fee: 3000, recipient: acct1, deadline, amountIn: amountA, amountOutMinimum: amountB.sub(amountB.mul(slippageTolerance)), sqrtPriceLimitX96: 0 }); – jayyyo Mar 20 '23 at 16:12
  • type: 2, maxFeePerGas: BigNumber { _hex: '0x04961388c8', _isBigNumber: true }, maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true }, nonce: Promise { 427 }, gasLimit: Promise { [Circular *1] }, chainId: Promise { 31337 } – jayyyo Mar 20 '23 at 16:26