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 as 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:
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 the upper bound by the 1MB (ish) block size and then there are considerations from the miners and finally, your wallet may not let you 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?
Well, we'd select the 1 BTC, the 0.5 BTC, the 0.25 BTC and then 77 coins of 0.1 BTC (thats 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.
This process of deciding which coins (or UTXOs) to select is unsurprisingly called "Coin Selection" and has been studied long and hard by academics.
The TrustVault coin selection algorithm is currently a simple one that is roughly suited to most users. It simply 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. (API users will get a response showing how much you can spend)
So, if you can't spend all your UTXOs in one transaction, as in our example, what can you do?
What you can do is "consolidate your UTXOs".
In our coin example above, let say you want send your full balance of 11.75 BTC but since you can only carry 80 coins, therefore you can only send 9.45 BTC using the largest first coin selection algorithm.
What if we went to a bank and gave them 80 coins (the limit we could carry) and asked them to give us a single coin of the full value in return (banks cannot do this as physical coins have fixed sizes, but with Bitcoin can create a UTXO of any size).
We could give the bank a total of 11.75 BTC made up of 80 coins and they would return us a single coin of 11.75 BTC.
Our wallet would now contain the following:
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 (you can select an existing address but it MUST be in the same sub-wallet). DO NOT SEND IT TO SOMEONE ELSE
Send a large balance. You can use the SetMax from the iOS app or set an unfeasibly large balance in the API and check the
maxAllowedToSendresponse from the API
Set the transaction amount for the maximum can you send
Send the transaction
Wait for confirmation
This process is often done during times of lower fees to minimise the cost.
We have introduced how Bitcoin operates like real world coins and there are limits to how many can be used in a single transaction. We also saw how you can consolidate the number of coins you have into fewer coins of higher value allowing you "consolidate your UTXOs" thus allowing you to spend more in a single transaction.
This has been a small introduction to his process and there are many, many good articles on this topic. We have also not gone into all the details such as fees, the size of the transactions, how the coin section algorithm can affect the number of UTXOs over time, but please reach out if you'd like to understand more.
If you wish to find out more about your current state of UTXOs, please get in touch.