I looked at https://github.com/ethereum/web3.js/blob/develop/dist/web3.js but can not find the method which it uses to estimate the amount of gas a transaction is going to consume.

Does anybody know how this works or can point to a reference?


The web3.js library makes a call eth_estimategas to the ethereum client.

Geth will execute the transaction and will return the gas consumed, but it will discard any modification.

A note from the documentation

Note that the estimate may be significantly more than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.

Metamask's VM subprovider also implements estimate gas by executing the transaction and measuring the gas consumed.

| improve this answer | |

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.