Is there any way to understand if a specific transaction will revert while in pending status?

If so, how can I code it exactly?


It is possible to see if it's likely that a pending transaction will revert, but to know with 100% certainty you need to be aware of all other transactions that will be mined before your target transaction (even those ordered before it in the same block). This is because other transactions can change the revert conditions of the transaction you're looking at.

For instance, imagine a pending transaction that will revert because a flag has not been raised. However, if the miner includes a transaction that raises this flag before including your transaction after, it would execute without reverting.

For the other instances where you just want a naive estimation if a transaction will revert or not, simply estimating the gas usage of the transaction will get you a long way. You will have to use the transaction details to estimate the gas usage of this transaction.

In ethers, you can see if provider.estimateGas() throws an error:

try {
  await provider.estimateGas({
    from: "0x8ba1f109551bD432803012645Ac136ddd64DBA72",
    to: "0x733aF852514e910E2f8af40d61E00530377889E9",
    data: "0x000000000000000000000000084b1c3c81545d370f3634392de611caabff8148",
    value: BigNumber.from(0),
} catch (e) {
  console.log('tx will likely revert')

If provider.estimateGas() does not throw an error with the given transaction details, you can also check if it returns a gas estimation higher than the gas limit of your target transaction.

  • How can i be aware of all other transactions that will be mined? Also, some of those transactions comes from custom smart contracts. Is there any way to know those as well? Sep 17 '21 at 9:59
  • You will have to watch the mempool of a full node (such as geth). However with the current way Ethereum works, some transactions may be sent directly to miners instead of going through the p2p network, and you have no way of seeing these until they are mined. Transactions to smart contracts are treated the same as any other transaction.
    – Jonas Hals
    Sep 17 '21 at 10:55

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.