I've created two Bep20 tokens (old/new) and I was wondering if I could write a smart contract that exchanges/swaps old tokens for new ones.

Is there a simple way to exchange two Bep20 tokens using a smart contract?

Any guidance, related links would help a ton. (Hope the question is not too flat).

  • 1
    What did you try? The obvious solution of sending the old tokens to the contract, and the contract minting new tokens should work.
    – Ismael
    Commented Jul 13, 2021 at 21:58

1 Answer 1


Refer the below code

// swap 1:1 
function swap(uint256 amount) external {
        IERC20(tokenA).transferFrom(msg.sender, address(0), amount);  // I am buring the token A by sending it to zero address, you can modify the to address
        IERC20(tokenB).transfer(msg.sender, amount);
        emit Swapped(msg.sender, amount);

Hope it helps!

  • Thanks! I've found a different solution as well
    – patrick
    Commented Jul 15, 2021 at 14:15

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.