I am unable to get this working with ethers.js. I know there are many similar questions for this specific topic, but none of the answers seem to work for me. My attempt is mostly based on Noob Troubleshooting failed BSC transaction using ethers.js and Pancakeswap v2 router.

My code is the following:

const ethers = require('ethers');

const addresses = {
    WBNB: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
    BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
    factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73',
    router: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
    recipient: '0xE00e552a0e18ca773a26f65cE957915D302CBE10'

const PRIVATE_KEY = ""

const mygasPrice = ethers.utils.parseUnits('5', 'gwei');

const WSS = "wss://*.bsc.quiknode.pro/*/"
const provider = new ethers.providers.WebSocketProvider(WSS);

const wallet = new ethers.Wallet(PRIVATE_KEY);
const account = wallet.connect(provider);

const router = new ethers.Contract(
        'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'

const init = async () => {
    console.log("Test TX");

    let tokenIn = addresses.WBNB, tokenOut = addresses.BUSD;

    const amountIn = ethers.utils.parseUnits('0.0005', 'ether');
    const amountOutMin = 0;

    console.log("Starting swap...");

    const tx = await router.swapExactTokensForTokens(
        [tokenIn, tokenOut],
        Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
            gasPrice: mygasPrice,
            gasLimit: 500000
    console.log("Swap done!");
    const receipt = await tx.wait();
    console.log("Transaction receipt");


When I run this code, it gets stuck.

Test TX
Starting swap...

The wallet generated from the private key is the same as the recipient address (0xE00e552a0e18ca773a26f65cE957915D302CBE10), and there is enough WBNB balance. I just tried to do a manual swap on pancakeswap V2 (WBNB to BUSD) and it worked flawlessly (https://bscscan.com/tx/0xe3351e82ed4a86e18e164ba47b44bb7fa74faaeda67f8b7e3486f6410e5b50b9).

I am currently out of ideas, and I don't know how to debug this further.

Thanks for your help.

2 Answers 2


You need to call swapExactETHForTokens for swapping WBNB to a token, NOT swapExactTokensForTokens. swapExactTokensForTokens is for swapping a non-BNB token to another non-BNB token. (At least, that is what I can think of.)

  • 1
    I don't think this is the answer. I know for a fact that you can call swapExactTokensForTokens to swap WBNB for other tokens. (I have done it in the past, using web3py). Also all other answers seem to agree with me on this. As I understand it, swapExactETHForTokens would be used to swap BNB, not WBNB.
    – lobis
    May 23, 2021 at 14:40
  • Yeah, seems I missed that. I thought you were trying to directly swap from BNB. I'm looking at the code again now to try and find a potential issue.
    – bru53001
    May 23, 2021 at 14:42
  • Thanks for your help! I am really lost on this one. It probably has something to do with the asynchronous stuff, which I am not at all familiar with.
    – lobis
    May 23, 2021 at 14:46

Looks like it was a problem with the BSC node (QuickNode). I have just started using this service but so far it looks very unreliable. Perhaps I am doing something else wrong, I would love to know what.

After changing the WSS to wss://bsc-ws-node.nariox.org:443 it works.

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.