1

im new in web3js and i got a problem. Would be nice, if you could help me. Ive been trying to solve it myself for days...

Im trying to simulate a buy transaction (after its working i will do the sell part), to check if buys (and sells) would success. but im getting this error: "Error: Returned error: execution reverted: PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT"

but i have enough bnb in my wallet. What am i doing wrong? here is my code:

var Web3 = require("web3");
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed.binance.org/'));

var panabi = JSON.parse(fs.readFileSync('panabi.json', 'utf-8'));

var MY_ADDRESS = "0x....";
var WBNB_ADDRESS = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"; // spend

var TokenAddress = "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3"; //Token to buy
var PANCAKESWAP_ROUTER_ADDRESS = "0x10ED43C718714eb63d5aA57B78B54704E256024E";

let contract = new web3.eth.Contract(panabi, PANCAKESWAP_ROUTER_ADDRESS);

// First step: Execute a buy for specific token. Since I don't want to send that 
// transaction to blockchain, I'm just calling that function and see its result

var tx = new web3.eth.getTransactionCount(MY_ADDRESS, (err, result) => { console.log(result) });

var batch = new web3.eth.BatchRequest();
var callback = batch.add(
    contract.methods.swapExactETHForTokens(
        web3.utils.toHex(0), //slippage
        [WBNB_ADDRESS, TokenAddress],
        MY_ADDRESS,
        web3.utils.toHex(Math.round(Date.now() / 1000) + 60 * 20)
    )
        .call
        .request({
            gasLimit: web3.utils.toHex(2000000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('7', 'gwei')),
            value: web3.utils.toHex(web3.utils.toWei('0.01', 'ether')),
            nonce: web3.utils.toHex(tx)
        }, 'latest', callback)
);
console.log(callback); // this line should log response of your query```
1

As you can see from the pancakeswap contract code, this error is triggered by this:

require(amountIn > 0, 'PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT');

The contract thinks that the amount that you are trying to swap is 0. Your transaction does include a value of 0.01 BNB to swap and this is what the contract should use as amountIn. I am not sure why it sees it as 0...

Maybe try replacing:

value: web3.utils.toHex(web3.utils.toWei('0.01', 'ether')),

By:

value: "10000000000000000",
3
  • unfortunately its not working. do you have another idea how to simulate a buy on the mainnet? Nov 29 '21 at 4:07
  • Maybe that batch requests cannot pass a value... Instead of doing a batch request, try a single call to the contract like so: web3js.readthedocs.io/en/v1.5.0-rc.0/…
    – Undead8
    Nov 29 '21 at 18:39
  • Batch requests in web3js are kind of buggy...
    – Undead8
    Nov 29 '21 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.