1

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];  
}  
1
  • Please, can you add more details about your questions? Commented Apr 13, 2022 at 21:59

1 Answer 1

0

Sending the native token on an EVM chain is done without an address ( a different concept). It's all in the msg.value. It's similar to sending ETH on EThereum: https://blockchain-academy.hs-mittweida.de/courses/solidity-coding-beginners-to-intermediate/lessons/solidity-2-sending-ether-receiving-ether-emitting-events/topic/sending-ether-send-vs-transfer-vs-call/

To send the native token on most of these apps, you have to wrap the native token first: https://weth.io/

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.