0

I have code to call pancakeswap/uniswap router function to swap token. In my code, I have no problem calling swapExactETHforTokens when swapping WBNB to BUSD in BSC. my problem came when I tried calling swapExactTokensForETH function to swap BUSD to WBNB. It showed me this error

Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"reason":"cannot estimate gas; transaction may fail or may require manual gas limit","code":"UNPREDICTABLE_GAS_LIMIT","error":{"reason":"processing response error","code":"SERVER_ERROR","body":"{"jsonrpc":"2.0","id":817,"error":{"code":3,"message":"execution reverted: TransferHelper: TRANSFER_FROM_FAILED","data":"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000245472616e7366657248656c7065723a205452414e534645525f46524f4d5f4641494c454400000000000000000000000000000000000000000000000000000000"}}\n","error":{"code":3,"data":"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000245472616e7366657248656c7065723a205452414e534645525f46524f4d5f4641494c454400000000000000000000000000000000000000000000000000000000"},"requestBody":"{"method":"eth_estimateGas","params":[{"gasPrice":"0x2540be400","type":"0x0","from":"0xa715c85973f8e0282ee8c1fc36305be23928344f","to":"0xd99d1c33f9fc3444f8101754abc46c52416550d1","data":"0x18cbafe5000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000713d616652258ed00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000a715c85973f8e0282ee8c1fc36305be23928344f0000000000000000000000000000000000000000000000000000000060e5bd9a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000078867bbeef44f2326bf8ddd1941a4439382ef2a7000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd"}],"id":817,"jsonrpc":"2.0"}","requestMethod":"POST","url":"https://data-seed-prebsc-1-s1.binance.org:8545"},"method":"estimateGas","transaction":{"from":"0xA715c85973f8E0282eE8c1FC36305bE23928344f","gasPrice":{"type":"BigNumber","hex":"0x02540be400"},"to":"0xD99D1c33F9fC3444f8101754aBC46c52416550D1","data":"0x18cbafe5000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000713d616652258ed00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000a715c85973f8e0282ee8c1fc36305be23928344f0000000000000000000000000000000000000000000000000000000060e5bd9a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000078867bbeef44f2326bf8ddd1941a4439382ef2a7000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd","type":0,"accessList":null}}, tx={"data":"0x18cbafe5000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000713d616652258ed00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000a715c85973f8e0282ee8c1fc36305be23928344f0000000000000000000000000000000000000000000000000000000060e5bd9a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000078867bbeef44f2326bf8ddd1941a4439382ef2a7000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd","to":{},"from":"0xA715c85973f8E0282eE8c1FC36305bE23928344f","gasPrice":{"type":"BigNumber","hex":"0x02540be400"},"type":0,"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.4.0)

from the error i believe it is because it is unable to estimate gas. but I was wondering why would it not be a problem in swapExactETHforTokens function?

If the problem is estimating gas, what should I add in the code? I used PancakeSwap/sdk in the code as well.

down here is the piece of code that execute the swap. hope it makes the context a little bit clearer

const swap = async () => {
try {
  error = '';
  console.log(`Swap process`);
  console.log({ tokenFrom, tokenTo });

  const wallet = new ethers.Wallet($settings.privateKey, binance().bsc);
  const account = wallet.connect(binance().bsc);

  var amountOutMin = trade.minimumAmountOut(slippageTolerance).raw;
  amountOutMin = BigNumber.from(amountOutMin.toString());
  const path = [token0.address, token1.address];
  const to = account.address;
  const deadline = Math.floor(Date.now() / 1000) + 60 * 20;
  var amountIn = trade.inputAmount.raw;
  console.log(amountIn);
  //var amount1 = amountIn*100000000000000000;
  amountIn = BigNumber.from(amountIn.toString());
  console.log({ amountIn, amountOutMin });

  const uniswap = new ethers.Contract(
    '0xD99D1c33F9fC3444f8101754aBC46c52416550D1',
    [
      'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
      'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
      'function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
    ],
    account
  );

  var tx;
  if (token0.address === WETH[chainId].address) {
    console.log(`Swap exact ETH for tokens`)
    tx = await uniswap.swapExactETHForTokens(
      amountOutMin,
      path,
      to,
      deadline,
      { value: amountIn }
    );
  } else if (token1.address === WETH[chainId].address) {
    console.log(`Swap exact tokens for ETH`)
    tx = await uniswap.swapExactTokensForETH(
      amountIn,      //maybe in 18 decimal
      amountOutMin,    //need to get getamountout by calling getamountout
      path,
      to,
      deadline,
    );
  } else {
    console.log(`Swap exact tokens for tokens`)
    tx = await uniswap.swapExactTokensForTokens(
      amountIn,
      amountOutMin,
      path,
      to,
      deadline
    );
  }
  console.log(`Transaction hash: ${tx.hash}`);

  const receipt = await tx.wait();
  console.log(`Transaction was mined in block ${receipt.blockNumber}`);
} catch (error) {
  console.error(error);
  error = error.message;
}

};

6
0

I believe it is because you need to add .send() to the end of any call to the blockchain that changes state (and .call() when reading state)

for example:

tx = await uniswap.swapExactTokensForTokens(
      amountIn,
      amountOutMin,
      path,
      to,
      deadline
);

should be:

tx = await uniswap.swapExactTokensForTokens(
      amountIn,
      amountOutMin,
      path,
      to,
      deadline
).send();
1
  • The answer doesn't apply the script since it uses the ethers.io framework which doesn't have a .send() command for transactions.
    – Ismael
    Sep 12 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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