Whoever has never made a mistake when sending a transaction on a blockchain, let him cast the first stone… Probably no stone will be thrown, which indicates how important it is to know how to cancel or replace a pending Ethereum transaction.
In this article, I will go over the methods to carry out this action, but it is also an opportunity, which I will not miss, to explain what are and how transactions in Ethereum work.
So, let’s abandon the preambles and get into the action.
The question contains, in itself, the key to this entire article. Yes, it is possible to replace or cancel a transaction in Ethereum, as long as the transaction is pending. Once a transaction is confirmed, this means that it was included in a block on the network and its modification will be impossible.
Each transaction we make on the network contains a unique and unrepeatable number, related to our address on the blockchain or, in other words, to our wallet. This number, called “Nonce“, can be modified manually to cancel or replace a transaction on the network.
But, perhaps this beginning gives too much knowledge for known. Therefore, it is necessary to take a step back and prior to knowing the method that we must put into practice, it is necessary for me to elaborate on the transactions, per se, on the Ethereum network.
When we talk about transactions in Ethereum, we are talking about nothing more than interacting with this blockchain. Within the network, there are two types of transactions:
In this article we will focus on the first type. Just to mention them, the second ones are those transactions that those who wish to create an application on the network carry out to “immortalize” their “smart contracts” in the blockchain.
Regular transactions have both a sender and a recipient, and that is their main difference with the former. These can represent a sending from the wallet of one person to another or a deposit in an application, among other examples.
Now, let’s learn a little about the life of a transaction.
When we send a transaction from our wallet, it travels to a place called “mempool”. This sort of crypto purgatory is where transactions wait to be selected by a miner to be included in a block.
Once the transaction is added to a block and the miner, after managing to solve the corresponding mathematical problem, manages to add the block to the network, it only remains to wait for confirmation. In simple words, this confirmation is a certain number of blocks added to the blockchain, which guarantee that the transactions contained in the previous blocks are valid and immutable.
So, now let’s learn about the different states that transactions go through during their “cryptohistory”.
If we check the status of a transaction, after sending it, we will see that it is “unknown“. This means that it has not yet been “seen” or “checked” by a node, which will take it to the famous and, at the same time, scary “mempool”.
Once this happens and our transaction is in the mempool waiting for a miner to take it to include it in a block, its status will be “pending” and it is only here that we will be able to act on it.
If we perform the method I will explain in the next section, the original transaction, replaced or cancelled, will have the status “replaced”, once we consult it in a search engine.
Finally, two statuses. “In block” will be the status of our transaction once it has been selected by a miner and included in a block already added to the network. Here, our transaction, together with all the transactions that make up this block, are waiting for the famous confirmation that gives them the “Success” status.
Let us now learn how to act on this pending transaction.
The structure of a network like Ethereum, its organization and consensus, are what today allow us to cancel or replace a transaction.
Now, if you wonder why we might want to perform these actions, there are two great examples:
Before knowing how to cancel a transaction, one more step.
The following is a list of the information contained in a transaction:
This data, the “Nonce”, is the one we will use to cancel or replace a transaction in Ethereum.
Once we have sent our transaction to the blockchain, and it is still pending, we will still be able to cancel or replace it. If this is what we wish to do we must follow the following simple steps:
When creating the new transaction we will be able to perform two actions:
In the first case, what we should do is to recreate the same transaction, either sending the same amount to the same address or interacting with the same contract, while correcting the erroneous parameter and selecting a higher gas, so that this transaction is prioritized for a miner. It is important to clarify, that the gas of both, will be consumed and will not be returned to us.
In the second case, when we do not want the transaction to be executed, the option usually used is to create a transaction in which we send 0 ETH to ourselves.
In this way, if this transaction has the same nonce, but more gas than the previous one, once confirmed, it will replace the unwanted transaction.
The world behind an Ethereum transaction can be fascinating. This article is but a small sketch of what happens when sending a transaction and the options we have to actively intervene in its future.
Knowing the fundamentals and concepts behind our daily tools is not an obligation or a necessity. But it undoubtedly provides us with the necessary expertise to act with full knowledge of the facts and minimize errors.
An informed community is a community that is able to protect itself and share its knowledge with the world. There are already too many proofs that the road to adoption is built on education.