0

I am creating a smartcontract on BSC which receive specific BSC tokens and transfer it to smartcontract. Then using Pancakeswap router to swap that token to MyOwnToken but I am unable to integrate to receive native BNB, though it works for WBNB.

Any minimal example how to do this?

below is code I am using...

//receiving token from user to smartcontract

IBEP20(fromToken).safeTransferFrom(msg.sender, address(this), _amount);

//swapping token

function swap(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn,
        uint256 _amountOutMin,
        address _to
    ) private {
        //next we need to allow the pancakeswap router to spend the token we just sent to this contract
        //by calling IBEP20 approve you allow the pancakeswap contract to spend the tokens in this contract
        IBEP20(_tokenIn).safeApprove(PANCAKESWAP_ROUTER, _amountIn);

        //path is an array of addresses.
        //this path array will have 3 addresses [tokenIn, WBNB, tokenOut]
        //the if statement below takes into account if token in or token out is WBNB.  then the path is only 2 addresses
        address[] memory path;
        if (_tokenIn == WBNB || _tokenOut == WBNB) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = WBNB;
            path[2] = _tokenOut;
        }

        //then we will call swapExactTokensForTokens
        //for the deadline we will pass in block.timestamp
        //the deadline is the latest time the trade is valid for
        IPancakeRouter(PANCAKESWAP_ROUTER).swapExactTokensForTokens(
            _amountIn,
            _amountOutMin,
            path,
            _to,
            block.timestamp
        );
    }
0
1

you need to use swapExactETHForTokens, not 100% sure as im typing this on my phone but i believe the syntax is :

IPancakeRouter.swapExactETHForTokens({msg.value})(
amountOutMin,
path, //path is WBNB->YourToken
to,
deadline
);
5
  • (and ofc your function needs to be payable)
    – Foxxxey
    Sep 24 at 20:03
  • can I do it with params without payable? because I also want other tokens as input which isn't payable.
    – hashgoal
    Sep 25 at 4:56
  • well the function that calls swapExactETHForTokens can be non payable if wanted, but it's going to require ETH (BNB in your case) anyways, so you'd need to use BNB already stored in the contract.
    – Foxxxey
    Sep 25 at 12:19
  • I want to user to send a BNB as deposit and later swap it tokens.
    – hashgoal
    Sep 25 at 16:03
  • Then you need a recieve() that provides a way for the contract to keep track of who deposited what (a mapping(address => uint256) will do), and your actual swap function can be non payable, but it has to check that the user isnt trying to swap more than what he deposited, and in that case in IPancakeRouter.swapExactETHForTokens({msg.value})( //... ); just replace msg.value with amount
    – Foxxxey
    Sep 25 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.