1

Hey I’ve been getting this error trying to execute a swap with my telegram bot.

I’ve tried to figure it out myself but I’m sure posting here will get to a solution quicker than I can.

This is the error I receive inside performSwap function.

Token Address: 0x5919dea604631016c15c805e3d948a0384879892

Error in performSwap: ContractExecutionError: Error happened while trying to execute a function inside a smart contractat Web3RequestManager._isReverted (/workspaces/Web3Tgbot/node_modules/web3-core/lib/commonjs/web3_request_manager.js:311:19)at Web3RequestManager._processJsonRpcResponse (/workspaces/Web3Tgbot/node_modules/web3-core/lib/commonjs/web3_request_manager.js:257:42)at Web3RequestManager.<anonymous> (/workspaces/Web3Tgbot/node_modules/web3-core/lib/commonjs/web3_request_manager.js:156:29)

W.js:156:29)at Generator.next (<anonymous>)at fulfilled (/workspaces/Web3Tgbot/node_modules/web3-core/lib/commonjs/web3_request_manager.js:21:58)at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {innerError: [Eip838ExecutionError: execution reverted] {innerError: undefined,code: -32000,receipt: undefined,data: undefined},code: 310,receipt: undefined}
 async function performSwap(ctx) {
  console.log("Inside performSwap function.");
  const tokenAddress = ctx.session.swap.tokenAddress;
  console.log("Token Address:", tokenAddress);
  const tokenContract = new web3.eth.Contract(erc20Array, tokenAddress);
  
  const amountETH = ctx.session.swap.amount;
  const amountInWei = web3.utils.toWei(amountETH.toString(), "ether");

  const userInputETHAmount = parseFloat(amountETH);
  //Check the bot's ETH balance
  const balance = await web3.eth.getBalance(BOT_ADDRESS);

  if (BigInt(balance) < BigInt(amountInWei)) {
    ctx.reply("Insufficient ETH balance.");
    return;
  }

  if (isNaN(userInputETHAmount) || userInputETHAmount <= 0) {
    ctx.reply("Please enter a valid positive number for ETH amount.");
    return;
  }

  // Calculate the minimum token amount you'd accept
  const expectedAmountOut = await dexRouter.methods
    .getAmountsOut(amountInWei, [BOT_ADDRESS, tokenAddress])
    .call();
  const amountsOutMin = calculateSlippageAmount(
    expectedAmountOut[1].toString(),
    5,
  );

  let swapTransaction;
  console.log("Preparing the swap transaction...");
  try {
    console.log(amountsOutMin, BOT_ADDRESS, tokenAddress);
    swapTransaction = dexRouter.methods.swapExactETHForTokens(
      amountsOutMin, // The minimum amount of tokens you'd accept as output
      [BOT_ADDRESS, tokenAddress], // Path from ETH to your target token
      BOT_ADDRESS, // The recipient of the output tokens
      Math.floor(Date.now() / 1000) + 60 * 20, // Deadline (timestamp in seconds, usually set to 20 minutes from the current time)
    );
    console.log("Swap transaction prepared successfully.", swapTransaction);
  } catch (error) {
    if (error.data) {
      const decodedError = web3.eth.abi.decodeParameter("string", error.data);
      console.error("Revert Reason:", decodedError);
      console.error("Error preparing swap transaction:", error);
      ctx.reply(`Error creating swap transaction: ${decodedError}`);
    } else {
      ctx.reply("Unknown error during transaction preparation.");
    }
    return;
  }
  
  ////////////// ethersJS
  const gasBuffer = 1.1;
const gasEstimate = await swapTransaction.estimateGas({
  from: BOT_ADDRESS,
  value: amountInWei,
});
console.log(`Gas Estimate: ${gasEstimate.toString()}`);
  
  ///////////

  const gasWithBuffer = Math.ceil(gasEstimate * gasBuffer);
  console.log("Gas with Buffer:", gasWithBuffer); 

0

Your Answer

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

Browse other questions tagged or ask your own question.