'Fail with error 'PancakeLibrary: INSUFFICIENT_AMOUNT'

I'm trying to fork PANCAKESWAP, everything seem to work well, I can add liquidity, but whenever I decided to add a liquidity that has to do with BNB (i.e calling the addLiquidityETH function) transaction gets reverted with that error message.

After a little digging, I found out it only appears in a single place in the whole router.

// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
    require(amountA > 0, 'PancakeLibrary: INSUFFICIENT_AMOUNT'); // THIS IS WHERE TRIGGERS THE REVERT!!
    require(reserveA > 0 && reserveB > 0, 'PancakeLibrary: INSUFFICIENT_LIQUIDITY');
    amountB = amountA.mul(reserveB) / reserveA;
}

and this quote function got called in the _addLiquidity function as seen below.

// **** ADD LIQUIDITY ****
function _addLiquidity(
    address tokenA,
    address tokenB,
    uint amountADesired,
    uint amountBDesired,
    uint amountAMin,
    uint amountBMin
) internal virtual returns (uint amountA, uint amountB) {
    // create the pair if it doesn't exist yet
    if (IPancakeFactory(factory).getPair(tokenA, tokenB) == address(0)) {
        IPancakeFactory(factory).createPair(tokenA, tokenB);
    }
    (uint reserveA, uint reserveB) = PancakeLibrary.getReserves(factory, tokenA, tokenB);
    if (reserveA == 0 && reserveB == 0) {
        (amountA, amountB) = (amountADesired, amountBDesired);
    } else {
        uint amountBOptimal = PancakeLibrary.quote(amountADesired, reserveA, reserveB); // here calls the quote function
        if (amountBOptimal <= amountBDesired) {
            require(amountBOptimal >= amountBMin, 'PancakeRouter: INSUFFICIENT_B_AMOUNT');
            (amountA, amountB) = (amountADesired, amountBOptimal);
        } else {
            uint amountAOptimal = PancakeLibrary.quote(amountBDesired, reserveB, reserveA); // and here too
            assert(amountAOptimal <= amountADesired);
            require(amountAOptimal >= amountAMin, 'PancakeRouter: INSUFFICIENT_A_AMOUNT');
            (amountA, amountB) = (amountAOptimal, amountBDesired);
        }
    }
}

So far, these are the ONLY places that calls quote which will result to the transaction getting reverted, but I can't seem to find anything wrong I mean, AmountA is obviously > 0.

Any help would be really appreciated.

EDIT:

The error was from my end, I was debugging an old transaction where I didn't send BNB to the contract, and I assumed it was the same error I received for the future transaction, decided to check and found out the error I was receiving was INSUFFICIENT_A_AMOUNT, fixed it by setting amountETHMin and amountTokenMin to 0



Solution 1:[1]

The error was from my end, I was debugging an old transaction where I didn't send BNB to the contract, and I assumed it was the same error I received for the future transaction, decided to check and found out the error I was receiving was INSUFFICIENT_A_AMOUNT, fixed it by setting amountETHMin and amountTokenMin to 0

Solution 2:[2]

I've met this error, this is the demo java code to fix the bug.

    raw = RawTransaction.createTransaction(
        nonce,
        gasProvider.getGasPrice(),
        gasProvider.gasLimit,
        contractAddress,
        amountEth.toWei(),  //>>> add amount of bnb here, when create rawTransaction
        FunctionEncoder.encode(function)
    )
rawTransactionManager.signAndSend(raw)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Isaac Frank
Solution 2 huu duy