I'm using ethers.js to interact with my smart contract and send a transaction. However, when the transaction fails (reverts), I can't access the receipt or the gas used, because ethers.js throws an error before I can capture those details.
Here's the code I'm using to send a transaction:
try { // Attempt to send the transaction to the contract const tx = await signer.sendTransaction(txRequest); // An error is thrown here in case of revert // Wait for the transaction to be mined and get the receipt const receipt = await tx.wait(); totalGasUsed += Number(receipt.gasUsed); } catch (error) { console.log(error); } error:
Error: VM Exception while processing transaction: reverted with reason string 'revert message'
Is there a way to get the gas used by a failed (reverted) transaction with ether.js?