1
function swapTokenForToken(uint256 amountIn, address tokenIn, address tokenOut, uint256 amountOutMin) public payable returns (uint256){
        address[] memory path = new address[](2);

        path[0] = tokenIn;
        path[1] = tokenOut;

        uint256[] memory amounts = PancakeLibrary.getAmountsOut(Pancakeswap_Factory_Address, amountIn, path);

        require(amounts[amounts.length - 1] >= amountOutMin, 'BSCBB: INSUFFICIENT_OUTPUT_AMOUNT');

        assert(IERC20(tokenIn).transfer(PancakeLibrary.pairFor(Pancakeswap_Factory_Address, path[0], path[1]), amounts[0]));

        (address input, address output) = (path[0], path[1]);
        (address token0,) = PancakeLibrary.sortTokens(input, output);
        uint amountOut = amounts[1];
        (uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));

        IPancakePair(PancakeLibrary.pairFor(Pancakeswap_Factory_Address, input, output)).swap(
                amount0Out, amount1Out, address(this), new bytes(0)
        );

        return amountOut;
    }

I am trying to swap two tokens utilising the following code. The only problem is that the transactions revert, with error code: Pancake: INSUFFICIENT_OUTPUT_AMOUNT. What am I doing wrong?

Thanks.

Edit: It behaves quite strangely, as the code works with some high liquidity tokens, but stops working with less known tokens.

3

This function takes a parameter uint256 amountOutMin. This parameter basically says that if the amount of token you get out of the swap is less than that amount, the swap must be cancelled.

If you get an INSUFFICIENT_OUTPUT_AMOUNT error, it means that the token that you would have got from the swap was lower than the amountOutMin that you set. It is generally due to price change between when the transaction was sent and when it was mined, or high slippage if your swap is big compared to the liquidity pool size.

7
  • The problem isn't that. I set amountOutMin 0 when testing. I've seen that the INSUFFICIENT_OUTPUT_AMOUNT comes from PancakePair: require(amount0Out > 0 || amount1Out > 0, 'Pancake: INSUFFICIENT_OUTPUT_AMOUNT'); Mar 22 at 19:28
  • If this is the error you get, it means that the getAmountsOut function calculated an amountOut of 0. My guess based on what you are describing is that the reserves amounts for that pair are very close to zero. Check the reserves of the pairs that give you this error.
    – Undead8
    Mar 22 at 19:50
  • unfortunately, the problem isn't that. I've found that the function does not work when interacting with tokens with fixed burns (example: RFI forks). Can this help you narrowing down the problem? Mar 22 at 20:14
  • Will I have to use that only for tokens with extra fees, or can I directly use it for all tokens? Mar 22 at 20:19
  • Thanks for your valuable help. I have been struggling with this issue for quite a while now. I'll try fixing my contract and if it does work I'll accept your answer! I have one last question. Can I distinguish between "normal" tokens and tokens with extra fees? Mar 22 at 20:22

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.