Blockchain Dev Series - Confirmations

Blockchain Dev Series - Confirmations

What is confirmation?

Confirmation is one of the fundamental parts of blockchain technology. It is a numeric value and tells us how trustworthy a given transaction is. In this post, I want to summarize all the information I collected about confirmations. They work similarly on Bitcoin, Bitcoin Cash, and Litecoin. So this information applies at least to the listed coins.

How does confirmation work?

For example, you need to send a few BTCs to Alex. You created a transaction with your favorite wallet or platform. At that point, your transaction has 0 confirmations. It is placed in an unconfirmed transaction pool until the next block is mined. This pool is also called a mempool. The mempool contains all unconfirmed transactions of the network. Unconfirmed transactions are prioritized by a fee, not a creation date. And if you set a high enough fee, the miner will pick it up from the pool and include it in the next block mined. When a new block is mined, with your transaction in it, you receive the first confirmation. Every next block mined increases the number of confirmations by 1.

How many confirmations are considered secure?

Okay, now we know how confirmations work. But how many do we need to be sure our transaction is secure and never will get reverted?

Well, it depends on the coin type. 0 confirmation may be reversed at any time, but more than 1 already means the transaction got accepted by the network. For example, Bitcoin transactions are called confirmed after 6 confirmations. So please adjust this number depending on your coin or the amount transferred.

How can I check confirmations?

You can check transaction confirmations via Coin's Explorer. Let's take the Bitcoin transaction as an example.

80da7267ab38c82919ad81e5a979d2c719f52a7f6a5d3486cca337fd77cd7218

If you check the link you can see the confirmation value.

Note: Confirmation of transaction and block are identical. So to check the transaction confirmation you can check the block where it is included and its confirmation as well.

Why my transaction is not confirmed?

The main reason why transactions get stuck in mempool is a low fee. The miner fees are like the shipping cost you pay when ordering something online. If it is low no Miner will choose your transaction to be placed in the next block. So if your transaction is not accepted for more than 48 hours try again with a higher fee. Also, unconfirmed transactions are reverted after some time by the network.

How to track transaction confirmations

During working on one of my projects, I had to track confirmations in real time. After a while, I found an almost perfect solution.

Bitcoin, Bitcoin Cash, and Litecoin support ZeroMQ. Wallets publish events on specific topics you can listen to. We are most curious about two such topics:

  1. zmqpubhashtx=address - publishes new, incoming transaction hash.
  2. zmqpubhashblock=address - publishes new, incoming block hash.

The first topic sends transaction ids with 0 and 1 confirmations. So it's already a good starting point to save all transactions in our database with 0 confirmation. And update them to confirmation 1 accordingly when the update arrives.

And next comes the trick with the block topic. Since every transaction with 1 confirmation gets +1 on every next block, we can increase all transaction confirmations which have more than 1 after every block arrival. This way we don't need to query the wallet with transaction details on a new block. It took me some time to realize this small hack. Previously on every new block, I was querying my database with all transactions with less than 6 confirmations and checking individually with the wallet. Before depending on my system load sometimes I had too many RPC calls. Maybe later I even provide an example code in NodeJS to demonstrate this.