0

I tried this code for many contracts, and sending ETH and approving were done well, but on the addliquidityETH section, I get an error: reverted with 'ds-math-sub-underflow'. I can't understand why.

const rpcUrl = "http://127.0.0.1:8555"; // anvil --fork-url http://127.0.0.1:8545 --port 8555
const privateKey = ""; // Replace with your actual private key
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

const routerAddress = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'; // Replace with the actual router contract address.
const deadline = Math.floor(Date.now() / 1000) + 60000; // Set the deadline to be 10 minutes from the current time
const abi =[]; //i use ERC-20 standard token ABI
const wallet = new ethers.Wallet(privateKey, provider);

async function impersonateAccount_addLiq(tokenAddress, contractOwner, amountETH) {
    try {
      // Impersonate the account
      await provider.send("anvil_impersonateAccount", [contractOwner]);
  
      // Create a signer for the impersonated account
      const signer = provider.getSigner(contractOwner);
      const router = new ethers.Contract(routerAddress, ['function addLiquidityETH(address,uint256,uint256,uint256,address,uint256)'], signer);
      const tokenContract = new ethers.Contract(tokenAddress, abi, signer);
  
      const [tokenDecimals, ownerTokenBalance, ownerBalanceWei] = await Promise.all([
        tokenContract.decimals(),
        tokenContract.balanceOf(contractOwner),
        provider.getBalance(contractOwner)
      ]);
  
    //   const ownerTokenBalanceWithDecimals = ethers.utils.parseUnits(ownerTokenBalance.toString(), tokenDecimals);
      const ownerTokenBalanceWithDecimals = ownerTokenBalance; // No need to add decimals here

    //   const minAmountTokenWithDecimals = ethers.utils.parseUnits("1", tokenDecimals);
      const minAmountTokenWithDecimals = ownerTokenBalance;
      const amountToLiquidity = ethers.utils.parseEther(amountETH);
  
      const sendEthTx = {
        to: contractOwner,
        value: ethers.utils.parseEther("2"),
      };
      const sendResult = await wallet.sendTransaction(sendEthTx);
  
      const approveTx = await tokenContract.approve(routerAddress, ethers.constants.MaxUint256); // Approve the maximum amount
      await approveTx.wait();
  
      const allowance = await tokenContract.allowance(contractOwner, routerAddress);
      console.log("Allowance for Router:", allowance.toString());
  
      const gasLimit = ethers.BigNumber.from(3000000); // Increase the gas limit if needed

      console.log("Gas Limit: ", gasLimit.toString());
      console.log(ownerTokenBalanceWithDecimals.toString(),minAmountTokenWithDecimals.toString())
      console.log('ownerTokenBalance :',ownerTokenBalance.toString())
      console.log('tokenAddress :',tokenAddress)
      console.log('contractOwner :',contractOwner)
      console.log('amountToLiquidity :',amountToLiquidity.toString())

      const tx = await router.addLiquidityETH(
        tokenAddress,
        ownerTokenBalanceWithDecimals,
        minAmountTokenWithDecimals,
        amountToLiquidity,
        contractOwner,
        deadline,
        {
          gasLimit: gasLimit,
        }
      );
      console.log(`Added liquidity successfully`);
    } catch (error) {
      console.error("An error occurred on impersonateAccount function:", error);
    }
  }
  
  impersonateAccount(tokenAddress, contractOwner, amountETH);

and here is one example of my run-with error.

Allowance for Router: 115792089237316195423570985008687907853269984665640564039457584007913129639935
ownerTokenBalance : 1000000000000000000
tokenAddress : 0x1995757da1Eb6ae05Fc6fe31f6ECdEdcb439Fb28
contractOwner : 0x0fd680D18Df75FD8d409c4c3349B250715fB1046
amountToLiquidity : 1000000000000000000
Error: reverted with 'ds-math-sub-underflow'

any help please.

1 Answer 1

0

solved issue was on router abi and missing value on addliquidityETH after set correct abi for router and add value to tx was fixed

        {
          gasLimit: gasLimit,
          value:amountToLiquidity
        }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.