1

So i have a contract that swaps a specific token to BNB when called, it just uses the simple uniswap router interface.

It works when swapping the tokens to BNB with the below code, (tokenToSwap) is the address of the token that im swapping

address[] memory path = new address[](2);
path[0] = address(tokenToSwap);
path[1] = uniswap.WETH();

But when i change the code the following code to swap this into BUSD instead:

address[] memory path = new address[](3);
path[0] = address(tokenToSwap);
path[1] = uniswap.WETH();
path[2] = address(tokenOut);

This code fails and i get the following error message: execution reverted: PancakeRouter: INVALID_PATH

The path i am using is the BUSD testnet path address tokenOut = 0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7;

What am i missing, and why am i getting the error message

-- Edited to include all code (interfaces have been omitted)

contract ACT_SWAP is Context, Ownable {

IUniswapV2Router02 uniswap;

address tokenToSwap;
address SwaptokenOut;

function SetTokenIn(address TokenIn) public onlyOwner() {
    tokenToSwap = TokenIn;
}

function SetTokenOut(address TokenOut) public onlyOwner() {
    SwaptokenOut = TokenOut;
}

constructor() {
    uniswap = IUniswapV2Router02(address(0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3));
}

receive() external payable {}

function swapTokensForETH(uint amountIn) external {
    // need to have called approve on this contract first
    IERC20(tokenToSwap).transferFrom(msg.sender, address(this), amountIn);
    address[] memory path = new address[](3);
    path[0] = address(tokenToSwap);
    path[1] = uniswap.WETH(); // returns address of Wrapped Ether
    path[2] = address(SwaptokenOut); // returns address of Wrapped Ether
    IERC20(tokenToSwap).approve(address(uniswap), amountIn);
    uniswap.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amountIn, 
        0, 
        path, 
        address(this), 
        block.timestamp
    );
}

}
2
  • Please, can you share all of your smart contract code? Mar 5, 2022 at 13:35
  • @Kerry99 - Edited the question to include all code
    – TheDev
    Mar 5, 2022 at 13:39

1 Answer 1

2

You error is at this line:

uniswap.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amountIn, 
        0, 
        path, 
        address(this), 
        block.timestamp
    );

The function swapExactTokensForETHSupportingFeeOnTransferTokens(...) must be use when you swap your tokens into ETH/WETH/BNB/WBNB. In fact the logic about this function check if you're swapping passing from the native coin (or its peg) of that blockchain. You can see these conditions at line 391 following this link. To solve this problem you must use the function called swapExactTokensForTokensSupportingFeeOnTransferTokens() that allow you to swap tokens passing thorw with native coin or its pegs. For more information check this piece of code that I modified:

function swapTokensForETH(uint amountIn) external {
        // need to have called approve on this contract first
        IERC20(tokenToSwap).transferFrom(msg.sender, address(this), amountIn);
        address[] memory path = new address[](3);
        path[0] = tokenToSwap;
        path[1] = uniswap.WETH(); // returns address of Wrapped BNB
        path[2] = SwaptokenOut;
        IERC20(tokenToSwap).approve(address(uniswap), amountIn);
        uniswap.swapExactTokensForTokensSupportingFeeOnTransferTokens(
            amountIn, 
            0, 
            path, 
            address(this), 
            block.timestamp
        );
    }
1
  • Works perfectly - Thank you, that had been driving me crazy for days...
    – TheDev
    Mar 5, 2022 at 19:19

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.