Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swapExactETHForTokens.estimateGas Causes Abrupt Code Termination with High amountOutMin #4785

Open
paramkr2 opened this issue Jul 14, 2024 · 0 comments
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@paramkr2
Copy link

paramkr2 commented Jul 14, 2024

Ethers Version

6.13.1

Search Terms

No response

Describe the Problem

When attempting to estimate gas for a swapExactETHForTokens transaction using ethers.js v6, the code crashes abruptly if the calculated amountOutMin is higher than expected. This behavior is unexpected and makes it difficult to handle errors gracefully in the application.

Code Snippet

async generatePathandEstimate2(tokenAddress, amount) {
  try {
    const path = [this.wethAddress, tokenAddress];
    const amountsOut = await this.router.getAmountsOut(amount, path);
    const amountOutMin = (amountsOut[1] * 99n) / 100n; // 1% slippage
    return { path, amountOutMin };
  } catch (error) {
    console.log('PathNotFound');
  }

  console.log('Trying With intermediate token');
  const intermediates = await this.pathFinder.getTokensForAddress(tokenAddress);
  console.log(intermediates);
  for (let token of intermediates) {
    try {
      const path = [this.wethAddress, token, tokenAddress];
      const amountsOut = await this.router.getAmountsOut(amount, path);
      const amountOutMin = (amountsOut[2] * 101n) / 100n; // 1% slippage
      return { path, amountOutMin };
    } catch (error) {
      console.log('PathNotFound');
    }
  }
  throw 'Path Not Found';
}

async estimateGas(tokenAddress, amount) {
  try {
    amount = ethers.parseEther(amount.toString(), 18);
    this.wethAddress = await this.router.WETH();

    const { path, amountOutMin } = await this.generatePathandEstimate2(tokenAddress, amount);
    console.log(`estimateGas method`, path, amountOutMin);

    const deadline = Math.floor(Date.now() / 1000) + 1200;
    const gasEstimate = await this.router.swapExactETHForTokens.estimateGas(
      amountOutMin,
      path,
      this.wallet.address,
      deadline,
      { value: amount }
    );
    console.log('Estimated Gas:', gasEstimate.toString());
    return true;
  } catch (err) {
    return false;
  }
}

Contract ABI

No response

Errors

Program Stops Bruptly

Environment

node.js (older than v12)

Environment (Other)

Windows 10

@paramkr2 paramkr2 added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants