I am using web3 to transfer tokens by using web3.eth.sendSignedTransaction.

When I am setting the parameter rawTx like

var rawTx = {

     "from"      : walletbase,       
     "nonce"     : value, 
     "gasPrice"  : web3.utils.toHex(2000000000), //2 gwei
      "gasLimit"  : estimateGas,
      "to"        : contractAddr,     
      "value"     : "0x00",
      "data"      : data,
      // "chainId"    : 3

I want to set the gasLimit dynamically. I tried to calculate the gasLimit by using web3.eth.estimateGas like:


          to  : toAddress,
          data: data

But the value is lower than need. I always got error like:

Error: Transaction ran out of gas. Please provide more gas:

Do you have better way to calculate the gasLimit? Thank you.

4 Answers 4


Include nonce field in estimate gas calculation.

     "from"      : walletbase,       
     "nonce"     : value, 
     "to"        : contractAddr,     
     "data"      : data

So you need to transfer tokens and estimate gas for that. If you are using web 1.0.x, you have more cleaner solution.

Generally, the gas estimate is correct but may be you didn't get the data param correctly. TRy the code below from documentation. If this doesn't help. Post your code as well so that problem can be replicated.

// using the promise
myContract.methods.myMethod(123).estimateGas({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})

@Pinank's a good solution, but in my case I discovered it worked once and then was 'reverted' subsequently. After spending many hours troubleshooting, I decided to change the destination address and that ended the revert error.


this works perfectly for me all the time..

   var gasprice = await web3.eth.getGasPrice();
   gasprice = Math.round(gasprice * 1.2);// to speed up 1.2 times..

  var buyItem = contractInstances.methods.method1(objId)
  var gas_estimate = await buyItem.estimateGas({ from: account })
  gas_estimate = Math.round(gas_estimate * 1.2); 

        from: account,
        gas: web3.utils.toHex(gas_estimate), 
        gasPrice:  web3.utils.toHex(gasprice),

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.