What do we mean by consolidating Bitcoin UTXOs? Well, first you need to understand what a UTXO is.
UTXO stands for Unspent Transaction Output. The basic unit of value in Bitcoin.
In our under the hood section on Bitcoin from our developer site, there is an introduction to the UTXO model and how it affects balances. There are plenty of references on the internet covering more details and with many different analogies, but for the purposes of this article, we shall be equating a UTXO with a physical coin (e.g. a 10 pence piece or a Euro) and a Bitcoin wallet with a physical wallet.
A simple example
Let's assume that someone gives you some Bitcoin:
- 1 BTC
- 0.5 BTC
- 0.25 BTC
- 100 * 0.1 BTC
Your wallet balance is: 1 + 0.5 + 0.25 + (100 * 0.1) = 11.75 BTC
Let's assume that you want to spend 10 BTC.
We need to introduce a limitation to our model that exists in Bitcoin. In Bitcoin, you can, in theory, spend an unlimited amount of UTXOs in one transaction; however, there is an upper bound due to the 1MB block size. Additionally, there are considerations from the miners, and your wallet may not allow you to add lots of UTXOs.
Most wallets have sensible limits. Ledger has 100, TrustVault is currently 80. There are reasons for this that will be explained elsewhere.
In our coin analogy, let's assume this limit of 80 UTXOs in a single transaction is because you cannot carry more than 80 coins (they are too heavy for our pockets).
So, if we can only carry 80 coins, how much can we spend in one transaction? Well, this depends on which coins we select to spend.
If we order all our coins by value and then select the smallest coins first, we'd select 80 0.1 BTC coins, which means we'd only be able to spend 8 BTC (80 * 0.1 BTC).
What if we kept the order the same but selected the largest first?
We'd select the 1 BTC, the 0.5 BTC, the 0.25 BTC, and then 77 coins of 0.1 BTC (that's our limit of 80 coins), which would give us 1 + 0.5 + 0.25 + (77 * 0.1) = 9.45 BTC.
So, we can immediately spend more in one transaction using the second approach.
Coin Selection
This process of deciding which coins (or UTXOs) to select is called Coin Selection and has been studied long and hard by academics.
The TrustVault coin selection algorithm currently selects the largest coins until the required balance can be met. If after adding 80 coins, the balance does not equal the desired spending amount, you will get an insufficient balance error.
Consolidate
What you can do is "consolidate your UTXOs."
In our coin example above, let's say you want to send your full balance of 11.75 BTC, but since you can only carry 80 coins, you can only send 9.45 BTC using the largest-first coin selection algorithm.
What if we went to a bank, gave them 80 coins, and asked them to give us a single coin of the full value in return? In Bitcoin, you can create a UTXO of any size.
Our wallet would now contain the following:
- 11.75 BTC
- 1 BTC
- 0.5 BTC
- 0.25 BTC
Just 4 coins. Spending the full amount is now easy.
How do you do that with Bitcoin?
The process is relatively simple:
- Create a transaction to an address that you own in the same sub-wallet. DO NOT SEND IT TO SOMEONE ELSE.
- Send a large balance using the iOS app or API.
- Set the transaction amount to the maximum you can send.
- Send the transaction.
- Wait for confirmation.
This process is often done during times of lower fees to minimize the cost.
Conclusion
We've introduced how Bitcoin operates like real-world coins and explained that there are limits to how many can be used in a single transaction. We also discussed how you can consolidate UTXOs into fewer coins of higher value, allowing you to spend more in a single transaction.
Further reading
This has been a small introduction to this process. There are many articles on this topic. If you want to learn more or explore UTXOs, please reach out.