nodejs web3 signe bep20 token gasprice too high

28 Views Asked by At

I want sign bep20 token using web3 but i get error this error

insufficient funds for gas * price + value: address ____ have 4960958883943891 want 69411784877494906285795002945479676105 (supplied gas 21000)

const Tx = require('ethereumjs-tx').Transaction
const { Web3 } = require('web3');
const newLocal = 'https://bsc-dataseed1.binance.org:443'
const web3 = new Web3(new Web3.providers.HttpProvider(newLocal));


const txObject = {
                    nonce:web3.utils.toHex(Number(txCount)),
                    to:addressTo,
                    value:web3.utils.toHex(web3.utils.toWei(balancetoSendToEther,'ether')),
                    gasLimit:web3.utils.toHex(21000),
                    gasPrice:web3.utils.toHex(web3.utils.toWei(gasPriceToEther,'gwei')),
                    maxPriorityFeePerGas: web3.utils.toWei("3", "gwei"),
                    maxFeePerGas: web3.utils.toWei("3", "gwei"),
                    common: {
                        customChain: {
                          name: 'custom-chain',
                          chainId: 56,
                          networkId: 56
                        }
                      }
           }
            
           
            
            // Sign the transaction
            const tx = new Tx(txObject)
            tx.sign(_private_key)
            
            const serializedTransaction = tx.serialize()
            const raw = '0x'+ serializedTransaction.toString('hex')

why gasprice is hight => 69411784877494906285795002945479676105

1

There are 1 best solutions below

0
Moutairou On

I finally managed to reduce the gasprice by changing the version of web3 to 1.7.5. Here is the code

async function send_funds(balancetoSend) {   
 const signedTx = await web3.eth.accounts.signTransaction({
    to: addressTo,
    value: '1000000000000000',
    gas: 21000,
    common: {
      customChain: {
        name: 'custom-chain',
        chainId: 56,
        networkId: 56
      }
    }   }, private_key);

  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)
    }   }); }