Hi All,
I have created a token swap contract which takes in a token and swaps this for another (just like PancakeSwap and the others) But I'm stuck on how to do this with native tokens, I can swap other tokens to tokens, and everything works fine but not BNB to tokens?
What am I missing?
My function i use is below.
function SwapTokensForTokens(address _tokenIn, uint amountIn, uint256 _amountOutMin) external {
IERC20(_tokenIn).transferFrom(msg.sender, address(this), amountIn);
IERC20(_tokenIn).approve(address(uniswap), amountIn);
address[] memory path;
if (_tokenIn == uniswap.WETH()) {
path = new address[](2);
path[0] = _tokenIn;
path[1] = tokenToSwap;
} else {
path = new address[](3);
path[0] = _tokenIn;
path[1] = uniswap.WETH();
path[2] = tokenToSwap;
}
uniswap.swapExactTokensForTokensSupportingFeeOnTransferTokens(
amountIn,
_amountOutMin,
path,
msg.sender,
block.timestamp
);
}