So I'm building something similar to this question Smartcontract that receive tokens and ether(native BNB) and swap it through pancakeSwap router?
I have my contract and its able to swap tokens-2-tokens, but I cannot figure out how to swap native BNB tokens.
I have this code which takes 2 tokens addresses and shows me the minimum swap amount, the way the address[] memory path;
is set up makes me think that I can send native BNB and swap these to the tokens.
But what is the contract address thats used and how do make it so that users can send BNB to the contract and receive tokens in returns.
function getAmountOutMin(address _tokenIn, address _tokenOut, uint256 _amountIn) external view returns (uint256) {
uint256 feeAmount = _amountIn.mul(_swapFee).div(100);
uint256 totalToSwap = _amountIn.sub(feeAmount);
address[] memory path;
if (_tokenIn == uniswap.WETH() || _tokenOut == uniswap.WETH()) {
path = new address[](2);
path[0] = _tokenIn;
path[1] = _tokenOut;
} else {
path = new address[](3);
path[0] = _tokenIn;
path[1] = uniswap.WETH();
path[2] = _tokenOut;
}
uint256[] memory amountOutMins = uniswap.getAmountsOut(totalToSwap, path);
return amountOutMins[path.length -1];
}