I want to swap a token to BNB

function swapExactTokensForETHSupportingFeeOnTransferTokens(
  uint amountIn,
  uint amountOutMin,
  address[] calldata path,
  address to,
  uint deadline
) external;

I do not know what to put on calldata path, I've tried this

path
{
  tokenContractAddres,
  wBNBAdress
}

And when I do a manual swap on pancakeSwap and check the input data , it basically does this, I've search around , it looks like I have to approve it first, when I look at the transaction hash it shows that approve takes an address (the pancake router) and a uint256

So my questions are:

  • In the approve function what goes in the uint256 argument?
  • After the approve function, how do I get the calldata path for the swapExactTokensForETHSupportingFeeOnTransferTokens function?
2

There are 2 best solutions below

0
On
  1. The approve() function about ERC20 token allow you to set the address of spendere (typeof: address) and amount that this address can spend (typeof:uint256). Having said that, in the approve function you must to insert:

    • address of your smart contract, because he is the spender of your ERC20 tokens;
    • value: the amount of ERC20 tokens that the address (specified above) can spend.
  2. For path to swap the tokens with BNB, you can use this:

     address[] memory path = new address[](2);
     path[0] = address(ERC20_TOKEN_ADDRESS);
     path[1] = UniswapV2Router02.WETH();
    

UniswapV2Router02.WETH(): this function permit you to have directly the address of native coin (in this case BNB).

0
On

The swapExactTokensForETHSupportingFeeOnTransferTokens requires the last item of the path to be ETH always!

In your case, you should use the function swapExactTokensForTokensSupportingFeeOnTransferTokens and the path parameter having the exact order of conversions that will be execute to get to your final token. In that case you need to guarantee that those pairs are available in Uniswap in order to define the path.

Example 1: You have the token A and you want to swap it for token B, but Uniswap doesn't have a liquidity pool with the pair A-B, but it has A-ETH and B-ETH. In that case you'll have the path parameter with the following addresses [A, ETH, B].

Example 2: You have the token A and you want to swap it for token B and Uniswap has a liquidity pool with the pair A-B. You simply pass the path argument with [A, B].