Handling Stuck Ethereum Transactions

31 Mar 2021

The code snippets in this post use web3.js, decimal.js and assume that you are connected to a geth node.

Ethereum transactions stuck on pending
Stuck Transactions on Eth

If you have seen and experienced something similar to the screenshot, then you know what I’m talking about. :)

Why are they stuck?

What is gas & gas price?

The gas value of a transaction tells miners how much computational resources will be needed to mine the transaction.
The gas price tells miners how much Ether you’re willing to pay for the unit of work (gas).
Therefore, this is how the transaction fees are calculated in a nutshell: gasNeeded * gasPrice .

The solutions

Try to calculate optimal gas prices to avoid transactions getting stuck

This is the first step in fixing the problem for future transactions. For this purpose you could use:

Note: The methods below do not guarantee that the last submitted transaction will be processed. It just increases the probability! Therefore, for reliable transaction handling, every transaction has to be tracked!

Replace pending transaction with same transaction but with higher gas price

As you might have seen Ethereum transactions that belong to one account execute in strict order. This order is controlled by the nonce value of the transaction.
In order to increase the probability of a transaction getting confirmed you can:

FYI, Some refer to this as a ‘speedup’ transaction.

What if I don’t want to send it anymore?

Similarly you can:

FYI, Some refer to this as a ‘cancel’ transaction.