Search code examples
javascriptsolidityethers.jserc20uniswap

Error adding liquidity to Uniswap V2 while deploying ERC20


This is my deploy script. My contract has ERC20 and uniswapV2 interfaces. The token gets deployed and uniswapV2 pair is created in constructor ofthe contract.

const { ethers } = require("hardhat");

async function main() {
  // Get the ABCD contract factory and the deployer's wallet
  const ABCDFactory = await ethers.getContractFactory("ABCD");
  const [deployer] = await ethers.getSigners();

  // Deploy the ABCD contract
  const abcd = await ABCDFactory.deploy();
  console.log('MyToken deployed to:', abcd.address);

  // Mint the initial supply of tokens and transfer them to the deployer's address
  const initialSupply = await abcd.totalSupply();
  await abcd.transfer(deployer.address, initialSupply);

  // Approve the Uniswap router to spend the initial supply of tokens
  const uniswapV2Router = await ethers.getContractAt("IUniswapV2Router02", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D");
  await abcd.approve(uniswapV2Router.address, initialSupply);

  // Add liquidity to the Uniswap pair with custom rewards
  const tokenAmount = ethers.utils.parseEther("10000");
  const ethAmount = ethers.utils.parseEther("1");
  //const customTokenAmount = ethers.utils.parseEther("10000"); // Replace with your custom token amount
  //const customETHAmount = ethers.utils.parseEther("1"); // Replace with your custom ETH amount
  await uniswapV2Router.addLiquidityETH(abcd.address, tokenAmount, tokenAmount, ethAmount, deployer.address, Date.now() + 1000 * 60 * 10, { value: ethAmount });
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

And I am getting an error cannot estimate gas fee when transferring tokens from my account to uniswapv2router. Error:

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ]
...
...
...
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'execution reverted: TransferHelper: TRANSFER_FROM_FAILED',
  code: 'UNPREDICTABLE_GAS_LIMIT',
  method: 'estimateGas',
  transaction: {
    from: 'MyWalletAddress###############',
    to: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
    value: BigNumber { value: "1000000000000000000" },
    data: '0xf305d7190000000000000000000000004a7052a43ebb5999da739b9dfdd991a6d33ee54100000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000048ea6198ae098a507ea7bf551ba93236ec4d1c37000000000000000000000000000000000000000000000000000001870cf4c57f',
    accessList: null
  },

Appreciate your help!

I tried the above script and token is deploying fine with Uniswap V2 pair created.


Solution

  • When sending transactions without specifying the gas price & limit, the library will ask the RPC server for estimated gas config. The RPC server will simulate the transaction and respond the estimated values.

    When the script try to get the estimated gas for the addLiquidityETH transaction, the approve transaction is not confirmed yet, or it can be confirmed but the RPC server is not yet update with the latest block. Because of that, when simulating, the allowance is still zero, making the simulation fails.

    You can set the gas config manually, skipping the simulation part, or use transaction.wait to wait for several blocks after confirming the approve transaction.