When I try to invoke
IUniswapV2Router02.swapExactTokensForETHSupportingFeeOnTransferTokens(), I am getting this below error. Can someone please help what is the issue.
Fail with error ‘TransferHelper: TRANSFER_FROM_FAILED’
Below is code snippet.
function swapTokensForBNB(uint256 tokenAmount) private {
// generate the uniswap pair path of token -> wBNB
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
// make the swap
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0, // accept any amount of BNB
path,
address(this),
block.timestamp
);
}
function _approve(address owner, address spender, uint256 amount) private {
require(owner != address(0), "BEP20: approve from the zero address");
require(spender != address(0), "BEP20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
When I dig into uniswap router code, i could see that the below stack trace
IUniswapV2Router02.swapExactTokensForETHSupportingFeeOnTransferTokens()
=>
TransferHelper.safeTransferFrom(path[0], msg.sender, PancakeLibrary.pairFor(factory, path[0], path[1]), amountIn);
=>
function safeTransferFrom(address token, address from, address to, uint value) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
}