Web3.js BNB – How to Send BNB from One Wallet to Another Using Web3?

bscmetamasknodejssmart-contract-walletsweb3js

I am sending BNB from one to another wallet but I a getting an error when I execute my code while I have 0.009 BNB in my wallet :

Error: Returned error: insufficient funds for gas * price + value

Here is my code :

const http = require("http");
const pvtkey="***********************************";

const web3 = new Web3("https://bsc-dataseed.binance.org");

http.createServer(async (req, res) => {
  if(req.url != '/favicon.ico')
    {
        const signedTx = await  web3.eth.accounts.signTransaction({
            to: '***********************',
            value: '1000000000000000',
            gas: 2000000,
            common: {
              customChain: {
                name: 'custom-chain',
                chainId: 56,
                networkId: 56
              }
            }
        }, pvtkey);

        web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
            if (!error) {
              console.log("🎉 The hash of your transaction is: ", hash);
            } else {
              console.log("âť—Something went wrong while submitting your transaction:", error)
            }
           });

    res.end();
  }
    })  
  .listen(8080);```

Best Answer

This means there's not enough BNB in your wallet to pay for the transaction value sent + fee (max gas limit * gas price).

You've set 2000000 gas limit, this means you need 2000000 * 5gwei = 0.01 BNB for fee + 0.001 BNB to be sent, total of 0.011 BNB needs to be in your wallet.

There's no need for 2,000,000 gas in a simple transfer, set gas: 21000 which is the minimum tx gas cost, should be enough.

Related Topic