I have a BEP20 Token let's call it Token "A" in my smart contract and I want to swap it for another BEP20 Token "B" using pancake swap In my smart contract how can I write a function that uses pancake swap to do this swap? all tutorials that I found were confusing

2 Answers 2


I think you're looking for pancakeswap router docs , play around on this. Also, you can utilize pancakeswap testnet.

  • Thanks. do I need Factory for this too?
    – Meysam
    Oct 2, 2022 at 15:11
  • nope. no need. The Router handles all of the swapping mechanism.
    – Lonewarp
    Oct 2, 2022 at 15:13

Using the Pancake router V2 you can swap tokens, look into


Here is the function you would need to call for swapping two tokens which you could call in your smart contract

        function swapTokensForExactTokens(
    uint amountOut,
    uint amountInMax,
    address[] calldata path,
    address to,
    uint deadline
    ) external virtual override ensure(deadline) returns (uint[] memory amounts) {}

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.