In web3.js, if a revert occurs, it doesn't give a receipt along with it. I want to determine how much gas was actually used after a revert.

In 1.0, I can't even get the tx hash from the event transmitter.

  • I have a custom geth which gives you the real gas estimation on any transaction, erroneous or not, with gas refunds or without, contact me and I will send you the code – Nulik Sep 19 '18 at 0:46

EDIT: The below only works for ganache.

In my testing, the below works. The function storeAndRevert, as you might imagine, stores the argument in a state variable and then reverts:

instance.methods.storeAndRevert(42).send({ from: accounts[0], gas: 3000000 })
.on('error', async (err) => {
  const receipt = await web3.eth.getTransactionReceipt(err.hashes[0]);
  console.log(`Gas used: ${receipt.gasUsed}`);

As far as I know, the error event is the only one you'll be able to listen for, though I think try/catch will give you access to the same error object.

  • This doesn't work for me. I'm using web3 1.0.0-beta.36. I tested your exact code on a function which reverts. The err parameter is a string for me, not an object. – JBrouwer Oct 15 '18 at 12:48
  • Yes, this send to only work with ganache. I'll edit the answer. – user19510 Oct 15 '18 at 13:52
  • I am using this on ganache actually... I am assuming that instance is a web3.eth.Contract instance? – JBrouwer Oct 15 '18 at 14:15
  • Yes. This may vary depending on the ganache version? I was using ganache-core at version 2.2.1. – user19510 Oct 15 '18 at 14:21
  • I am testing this on the latest ganache-cli and ganache-core (2.2.1) versions. – JBrouwer Oct 15 '18 at 15:17

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.