2

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:

web3.eth.estimateGas({

          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.

2

Include nonce field in estimate gas calculation.

 web3.eth.estimateGas({
     "from"      : walletbase,       
     "nonce"     : value, 
     "to"        : contractAddr,     
     "data"      : data
})
1

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'})
.then(function(gasAmount){
    ...
})
.catch(function(error){
    ...
});

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.