3

How meta estimating gas cost before broadcasting.

How can I calculate gas cost for myfunc() in web3js?

function myfunc(){
   require(msg.sender == owner)
   ...
}

await contractInstance.myfunc.sendTransaction(); //it sends the transaction and wastes the gas if `myfunc` will fail.

Is there any best way to know gas cost before call solidity myfunc(), its saves gas for me ? (Similar like metamask)

0

2 Answers 2

0

web3 has an estimateGas method you can use to estimate the gas required for the transaction. You can use it for contract methods as well.

0

Yes there is way but not that much accurate. Just add 10 or 20% extra gas.

contractInstance.method.estimateGas(ARGS...);

Please have a look below link

how to estimate gas cost?

1
  • How do you add 10 - 20%?
    – Shamoon
    Commented Jun 28, 2018 at 15:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.