In this article, we will describe how the UTxO model works through an analogy with banknotes. We will refine this simplified view further.
UTxOs are like Banknotes
People perceive a bank account as a balance. It is a number that increases if someone sends money to the account, or decreases if the owner of the account spends money. The account-based model that Ethereum uses works similarly.
If Alice has 3 ETH in her account and sends Bob 1 ETH, the Ethereum transaction will subtract 1 ETH from Alice's account and add it to Bob's account. An Ethereum transaction is an atomic operation. This means that it happens in one moment through the change of state that occurs through the blocks.
In the case of Cardano, forget the bank account analogy. The UTxO model behaves completely differently in the background, although users won't notice it thanks to Cardano wallets.
The UTxO model has no idea about concepts like account and balance. UTxO behaves more like banknotes that can have any nominal value. This means that UTxO can be 6.9 ADA, 47 ADA, or 459.7 ADA. Cardano wallets help users work with UTxOs like an account balance. The 3 mentioned UTxOs will be seen by Alice in her wallet as 513.6 ADA.
What happens if Alice wants to send Bob 50 ADA?
Alice creates a transaction in the Cardano wallet. In addition to Bob's recipient address, she also entered the amount of 50 ADA. The wallet needs to insert UTxOs into the transaction, which will have a total of 50 ADA plus 0.17 ADA for the fee.
A wallet can use either a single input UTxO with a value of 459.7 ADA or two input UTxOs with a value of 6.9 ADA and 47 ADA as input to a transaction. The wallet chooses the second option. A total of 53.9 ADA is used as input for the transaction.
A Cardano transaction specifies input UTxOs (or just inputs) and output UTxOs (or just outputs). The total value of the inputs must be equal to or greater than the total value of the outputs.
Each input UTxO can be spent only once, meaning it can be used as an input to only one transaction. Once a UTxO is used as an input, the total amount of funds of a given UTxO is consumed forever. More new UTxOs are usually created as an output of the transaction, which can be used as an input for a future transaction.
Note that the transaction input is a larger amount of ADA than Alice wants to send to Bob (including the fee). This is in line with how the UTxO model works. Through the transaction, Alice spends 50.17 ADA, so through the newly created UTxO, 3.73 ADA is returned to her account.
The transaction will have 2 output UTxOs. 50 ADA for Bob and 3.73 ADA for Alice.
In the image below you can see the described transaction. Both input UTxOs will be completely spent and output UTxOs will be created from them. The value of output UTxOs will be lower by the transaction fee. Bob receives exactly 50 ADA.
The fee is not a new UTxO, but rather a difference between the total value of the inputs and the total value of the outputs. The fee is paid to the network for processing and validating the transaction. The fee behaves like an account-based model, as it is deducted from the sender’s balance and added to the network’s balance, without creating a new output. However, unlike an account-based model, the fee is not explicitly specified in the transaction, but rather implicitly calculated by the network.
UTxO behaves in the same way as when paying with banknotes at the cash register in a store. If you have to pay say $36, you can pay with a $50 bill and the cashier will give you back $14. Or you can use two $20 bills and the cashier will give you $4 back. If you were paying for exactly $20 worth of merchandise, you can pay with a $20 bill and the cashier won't give you anything back. Of course, with Cardano, you always have to pay a transaction fee.
Someone may find it helpful to compare UTxOs with slips with numbers that are stuck on blackboards. Spending means crossing out the number on the existing slip and sticking new slips with new numbers so that the value of the crossed-out number and the numbers on the new slips are the same.
Let me briefly digress to another topic.
It is good to understand the UTxO model in the context of the reported 24-hour network transfer value.
In our example with Alice and Bob, only 50 ADAs were transferred between the 2 participants. This is the amount that should be reported. 3.73 ADA was being returned to Alice's account and should not be included in the report. It should be easy for analysts to find out which output UTxO went to a new (foreign) address and which returned to the sender's wallet. A staking key is often used to differentiate.
It is not possible to include input UTxOs in the report, because Alice's wallet could have used an input UTxO with a value of 1000 ADA (if it was not more suitable in the wallet), which would distort the report a lot. However, this can also happen if analysts do not correctly recognize a UTxO that went to a new (foreign) address and a UTxO that returned part of the funds back to the sender's wallet.
What is UTxO?
In the explanation above, we have made some simplifications for easier understanding. Let's fix it now.
UTxO stands for Unspent Transaction Output. UTxO is the output of a previous transaction, which can be spent in the future by a transaction.
Input UTxO can be simply described as a structure that consists of an identifier and a value. The identifier is a so-called outpoint. The outpoint acts as a pointer to the location of the UTxO in the blockchain. In other words, it is a reference to a previous transaction output. The address of the current owner of the funds is found in the previous transaction. Input UTxO refers to the transaction that created the UTxO that is owned by the current spender.
An outpoint of the input UTxO consists of the transaction ID and the output index of the transaction that created the UTxO. The output index refers to the position of the output in the transaction. The value (funds) is the amount of coins or tokens that the UTxO holds.
You can see the input UTxO structure in the image below.
Output UTxO in the previous transaction also contains the recipient address, which is the address of the current owner of funds. You will an example below.
When a UTxO becomes an input of a new transaction, the new transaction determines the output address (or addresses) that will receive the funds of the input. The output address can be either a public key address (which requires a signature to unlock) or a script address (which requires a redeemer to unlock). Output UTxO also contains the recipient address.
It is the transaction that links the outpoint with the recipient address. This link is not permanent, as the recipient address can become an outpoint for another transaction in the future. In other words, UTxOs are constantly consumed and created by transactions on the network. When a new transaction is created, a witness must be provided for each input address.
The outpoint is the link between the owner address and the UTxO, as it identifies which output of which transaction the UTxO belongs to.
A Cardano transaction is a structure that contains four main components: inputs, outputs, a fee, and witnesses. Inputs are UTxOs that are consumed by the transaction. Outputs are UTxOs that are created by the transaction. The fee is the amount of ada that is paid to the network for processing and validating the transaction. Witnesses are proof that the user has the right to spend the inputs.
Each output contains an important sub-component, namely the recipient address.
In the image below you can see the structure of the output UTxO in the transaction, which created the output UTxO (new UTxO). Notice that it contains the recipient's address.
In the following picture, you can see Alice's transaction again. Note that the addresses from which ADA is spent are not part of the transaction. The transaction only references addresses through outpoints (green arrows).
Alice must prove that the addresses are hers and that she has the right to spend the ADA coins. There is a Witness (red arrows) in the transaction for each address with funds.
There are 2 outputs in the transaction with indexes 0 and 1. Each index contains a UTxO, which also includes the recipient address. Output with index 0 contains UTxO with 50 ADA and recipient address belongs to Bob. Output with index 1 contains UTxO with 3.73 ADA and recipient address belongs to Alice. The blue arrows indicate the place in the blockchain where the UTxOs are located, where the addresses of the owners of funds (new UTxOs) will be found when a new transaction is created to spend them.
When people talk about UTxO, it's usually in a simplified form. UTxO is only perceived as a value that is held at the owner's address and that can be spent. Spending means moving value from the sender's address to the recipient's address. This basically describes what is actually happening.
It is important to realize that through transactions, input UTxOs are destroyed and new UTxOs are created. Newly created UTxOs can become inputs (input UTxOs) of new transactions. The transaction history is still stored in the blockchain. Each UTxO can only be spent once. Therefore, the entire value of UTxO is spent. Nodes maintain a set of all unspent (active) UTxOs that can be spent. With each new block added to the blockchain, the set of UTxOs changes.
In the following picture, you can see how UTxOs disappear and new UTxOs are created. The active set consists of UTxOs from transactions 5, 6, 7, 8, and 9. UTxOs from transactions 1, 2, 3, and 4 are forever spent (destroyed).
In order to spend UTxO in transaction 9, it was necessary for the blockchain to verify the signature of the owner of the funds. Input UTxO in transaction 9 refers to the address of the recipient (which is the address of the current owner) in transaction 4. The signature (Witness) in transaction 9 is verified against the address in output UTxO from transaction 4.
If the transaction passes validation and is included in the blockchain through a new block, the owners of output UTxO in transaction 9 can spend funds in the future.
During the destruction and creation of UTxOs, new funds must not be created out of thin air. In our example, the value in input UTxO from transaction 1 is equal to the sum of all values from UTxOs that belong to the active set (all UTxOs that can be spent).
If we neglect fees, it must be true that the value of the inputs in the transaction must correspond to the value of the outputs.
In the image below, the value of the input UTxO was 100 coins in transaction 1. This is equal to the value of all outputs in the active set of UTxOs. 100 = 8 + 2 + 15 + 5 + 18 + 12 + 3 + 7 + 23 + 7.
The UTxO model can appear complex compared to the account-based model. This also applies to application programmers, who may find it more difficult to work with this model, because they do not work with balances, but with UTxOs. Despite the higher complexity, the UTxO model has some advantages, mainly in the context of use in distributed networks. We'll talk more about that next time.