I'm trying to create a trading bot and I noticed that even when a transaction is 100% gonna fail, the swapExactTokensForTokens still pushes through hence paying gas fee. My question is, how does Pancakeswap know that a transaction is gonna fail before executing it? How do we catch errors so that we don't have to pay gas fees for a transaction that's gonna fail?

Pancakeswap Error

2 Answers 2


If i'm understanding your question correctly, you should be able to trigger this with revert, require, assert. The error INSUFFICIENT_OUTPUT_AMOUNT is like a string that is being returned when something is being violated on the contract side.

  • I'm trying to do a pre-blockchain check so that I don't have to pay for gas fees just to know if a transaction will fail or not. I'm not sure how uniswap does it though and I'm finding it hard to find information online. Commented Jan 21, 2022 at 20:15
  • Usually if a transaction will revert, it'll show up in metamask before the tx is submitted. This is the difference between the different error statements. For example, in the case of assert there is 0 gas refunded, whereas in the case of require all remaining gas is refunded if you try to force an invalid tx through. Not sure if this is what you meant!
    – Medici
    Commented Jan 23, 2022 at 10:03
  • I actually found out that estimateGas throws different exceptions and I think that has similar effect to how uniswap/pancakeswap check for errors before the transaction. I just thought that function would just return the estimated gas basing on its name so it was a bit misleading. Commented Jan 24, 2022 at 10:28

When calling swapTokensForExactTokens (see interface below) make sure that amountInMin is using getAmountsOut(uint amountIn, address[] memory path), as that is what the contract is checking against

enter image description here

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.