2

I was wondering if the pattern below is correct for sending a transaction with the correct gas limit.

this.contracts.myContract.methods.myMethod.getEstimatedGas().then(gas => {
  this.contracts.myContract.methods.myMethod.cacheSend(
    ["prop1", "prop2"],
    {
      gas
    }
  );
});

I've also read that adding 10% to the estimate could be sensible.

Or should I just go with whatever prices Metamask populates when making the method call?

1

I use to follow this pattern that should be useful:

await contractInstance.methods.transfer(self.state.owner, self.state.value)
         .send({
            from: accounts[0],
            gas: '5000000'
          })

With gas you are setting up the gasLimit.

1

You can estimate the gas cost of your transaction using web3.eth.estimateGas.

Example:

var result = web3.eth.estimateGas({
to: "0xc4abd0339eb8d57087278718986382264244252f", 
data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"
});
console.log(result); // "0x0000000000000000000000000000000000000000000000000000000000000015

I would take this estimate and multiply by a factor of 1.1 to be safe.

See https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethestimategas for further reference.

  • I am indeed using the web3.eth.estimateGas method, I should have been clearer in my original question and code. I've updated the question now :) – Inigo Villalba Jun 28 '18 at 11:38

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.