0

I'm trying to make a swap on the Binance testnet using Pancakeswap. I keep getting an INSUFFICIENT_OUTPUT_AMOUNT error and I have no idea why. Here is my code:

const ethers = require('ethers');

const WBNB = "0xae13d989dac2f0debff460ac112a837c89baa7cd"; 
const BUSD = "0xaB1a4d4f1D656d2450692D237fdD6C7f9146e814";

const router = "0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3";

const provider = new ethers.providers.JsonRpcProvider("my.provider.url");
const wallet = new ethers.Wallet("myPrivateKey");
const signer = wallet.connect(provider);

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

const busdContract = new ethers.Contract(
    BUSD,
    [
        'function approve(address spender, uint256 amount) external returns (bool)'
    ],
    signer
)

async function main() {

    const BUSDamountIn = ethers.utils.parseUnits('100', 18);
    let amounts = await routerContract.getAmountsOut(BUSDamountIn, [BUSD, WBNB]);
    const WBNBamountOutMin = 0;

    console.log(ethers.utils.formatEther(BUSDamountIn));
    console.log(ethers.utils.formatEther(WBNBamountOutMin));

    const approveTx = await busdContract.approve(
        router,
        BUSDamountIn
    );
    let reciept = await approveTx.wait();
    console.log(reciept);

    const swapTx = await routerContract.swapExactTokensForTokens(
        BUSDamountIn,
        WBNBamountOutMin,
        [BUSD, WBNB],
        wallet.address,
        Date.now() + 1000 * 60 * 10,
        {gasLimit: 250000}
    )

    receipt = await swapTx.wait();
    console.log(receipt);
}

main().then().finally(() => {});

The first transaction that approves the token spending amount to the router works fine, but the swap itself fails.

Ethers.js throws the following error:

Error: transaction failed [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ]

On the bsc scanner the error code is:

Fail with error 'PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT'

The INSUFFICIENT_OUTPUT_AMOUNT error does not make any sense to me because the WBNBamountOutMin is set to 0 so any amount out should be ok.

Here is the example transaction: https://testnet.bscscan.com/tx/0xa87783c39e3787fbbf99236da631abe3991318523d1b3f0e87660eda1a9dfdce

Can anyone explain to me why this swap does not work?

1 Answer 1

0

This means that the pool doesn't have enough liquidity to return the output token (or even not liquidity at all).

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.