Sub topic 2
UTXO and account models: how balances and transfers appear
This chapter assumes you know what a blockchain, an address, and a transaction are. It also assumes you can read short Python functions. The models below decide what "a transfer from A to B" means in raw chain data, so they decide how you count exchange flows later.
Two ways to record who owns what
A blockchain has to answer one question: how much does each owner hold right now? Two designs dominate.
- The UTXO model (unspent transaction output) is used by Bitcoin, Litecoin, and Dogecoin. The chain stores a set of individual coin-like outputs. Your balance is the sum of the outputs you can spend.
- The account model is used by Ethereum and most chains compatible with it. The chain stores a table of accounts, and each account has a balance. A transfer subtracts from one balance and adds to another.
Both are fully public. The difference is in what you can read directly and what you must infer.
The UTXO model
An output is a record of "this amount is locked to this address." A transaction consumes existing outputs, called its inputs, and creates new outputs. Once an output is spent it can never be spent again.
Three consequences matter for tracking:
- There is no "from" field. A transaction lists the outputs it spends. You find the sender addresses by looking up each spent output in an earlier transaction.
- There is no single "amount." A transaction has a list of inputs and a list of outputs. Whole outputs must be spent, so the transaction usually returns the excess to the sender as a change output.
- The fee is implicit. The fee is total input value minus total output value. It appears in no output.
A balance is a derived number. To get it, you replay the chain, keep the set of unspent outputs, and sum the ones locked to an address.
The following code replays a tiny chain and computes balances. Amounts are integers in the smallest unit (satoshis for Bitcoin), which avoids rounding errors.
def apply_transactions(transactions):
"""Return the unspent outputs after replaying transactions in order."""
utxos = {}
for tx in transactions:
for ref in tx["inputs"]:
del utxos[ref] # a spent output is removed for good
for index, out in enumerate(tx["outputs"]):
utxos[(tx["txid"], index)] = out
return utxos
def balance(utxos, address):
return sum(o["value"] for o in utxos.values() if o["address"] == address)
transactions = [
{"txid": "tx1", "inputs": [], "outputs": [{"address": "alice", "value": 100000}]},
{
"txid": "tx2",
"inputs": [("tx1", 0)],
"outputs": [
{"address": "exch_deposit", "value": 30000},
{"address": "alice_change", "value": 69000},
],
},
]
utxos = apply_transactions(transactions)
print(balance(utxos, "alice")) # 0
print(balance(utxos, "alice_change")) # 69000
print(balance(utxos, "exch_deposit")) # 30000The address alice now holds nothing, yet the same person still owns 69,000 units. Wallets usually send change to a fresh address, so one person's funds drift across many addresses. This is why UTXO analysis depends on clustering, covered in a later chapter.
Counting an exchange inflow on a UTXO chain
Summing every output sent to an exchange address overcounts. The following cases would all be counted as inflow:
- The exchange consolidates many of its own deposit outputs into one hot wallet output.
- The exchange pays a batch withdrawal, and the change output returns to an exchange address.
A reasonable rule is to ignore outputs that go to an entity that also appears among the senders.
def inflow_to_exchanges(tx, labels):
"""tx: dict with 'inputs' and 'outputs', each a list of
{'address': ..., 'value': ...}. labels maps address -> exchange name.
Returns {exchange: amount received from outside that exchange}."""
senders = {labels.get(i["address"]) for i in tx["inputs"]}
inflow = {}
for out in tx["outputs"]:
name = labels.get(out["address"])
if name and name not in senders:
inflow[name] = inflow.get(name, 0) + out["value"]
return inflow
labels = {"ex1": "ExchangeA", "ex2": "ExchangeA", "ex3": "ExchangeA"}
deposit = {
"inputs": [{"address": "alice", "value": 100000}],
"outputs": [
{"address": "ex1", "value": 30000},
{"address": "alice_change", "value": 69000},
],
}
consolidation = {
"inputs": [{"address": "ex1", "value": 50000}, {"address": "ex2", "value": 50000}],
"outputs": [{"address": "ex3", "value": 99000}],
}
withdrawal_batch = {
"inputs": [{"address": "ex1", "value": 100000}],
"outputs": [
{"address": "user1", "value": 40000},
{"address": "user2", "value": 30000},
{"address": "ex1", "value": 29000},
],
}
print(inflow_to_exchanges(deposit, labels)) # {'ExchangeA': 30000}
print(inflow_to_exchanges(consolidation, labels)) # {}
print(inflow_to_exchanges(withdrawal_batch, labels)) # {}Only the first transaction is a real inflow. The 69,000 change output is not a payment to anyone, and the rule handles it because alice_change carries no exchange label.
The account model
In the account model, the chain state holds a record per account: a balance, a nonce (a counter of transactions sent, which orders them and prevents replays), and for contracts, code and storage. A plain transfer names one sender, one recipient, and one value. That matches the spine's idea of a "labeled transfer": from, to, amount.
Several properties make this model easier to track:
- Balances are directly queryable. You can ask a node for an address's balance at any past block, with no replay.
- Transfers have a clear direction. There is no change output, so the amount in a simple transfer is the amount that moved.
- Addresses are long-lived. People tend to reuse an account, so one address often stays tied to one holder for years.
The model has its own traps:
- Contract-internal transfers. When a user calls a contract, the contract may forward native currency (ETH) to other accounts. These movements are not top-level transactions. They show up only in execution traces, so a tool that reads only the transaction list misses them.
- Token balances live in contract storage. An ERC-20 token's holdings are a table inside the token contract, not in the account's own balance. The "balance" you see on the account is only the native currency. Token movement is recorded as event logs, which the next chapters cover.
- Contract wallets. A multisig or smart-contract wallet is an account controlled by code. Its address may be shared among many signers, or deployed at a predictable address on several chains, so one address does not always mean one owner.
- Failed transactions. A reverted transaction is still on the chain and still pays a fee, but moves no value. Count only successful ones.
Side-by-side comparison
| Question | UTXO model | Account model |
|---|---|---|
| What does the chain store? | A set of unspent outputs | A table of account states |
| How do you get a balance? | Sum unspent outputs for the address | Read the account balance at a block |
| Who sent a transaction? | Look up each spent output | Read the `from` field |
| How many parties per transaction? | Many inputs and many outputs | One sender, one recipient |
| Change | Explicit change output, often to a new address | None |
| Address reuse | Discouraged, so funds spread across addresses | Common, so identity is stable |
| Hidden movements | Fee is implicit | Internal transfers appear only in traces |
| Main tracking difficulty | Grouping addresses and spotting change | Capturing token logs and internal calls |
What this means for flow tracking
The model shapes the pipeline you build.
- On UTXO chains, resolve inputs first. Every analysis needs the previous output for each input, so you need an index of outputs or a full node with a transaction index.
- On account chains, collect three sources. These are top-level transactions, internal transfers from traces, and token Transfer logs. Leave out one and your inflow numbers will be too low.
- Normalize to one record shape. The chapter's later code expects records like
{day, from, to, amount}. On a UTXO chain, you must produce those by deciding which outputs are payments. On an account chain, you produce them by merging the three sources. - Keep the unit consistent. UTXO amounts are integers in satoshis. Ethereum values are integers in wei, and tokens have their own decimals. Convert only at the end.
- Remember balances are not flows. Summing flows from genesis should reproduce the balance. If it does not, you have a data gap, and that check is a cheap way to find it.
Exercise
A Bitcoin transaction spends one output of 5.0 BTC from address X. It creates two outputs: 1.2 BTC to an address labeled as ExchangeA's deposit address, and 3.7999 BTC to a new address Y. What is the fee, how much exchange inflow should you record, and what would you check before trusting the 1.2 figure?
Show answer
The fee is 5.0 - (1.2 + 3.7999) = 0.0001 BTC, because it is the input value minus the output values. Record 1.2 BTC as inflow to ExchangeA, and treat the 3.7999 BTC as change returned to the sender, not as a payment. Before trusting this, check that X is not itself an ExchangeA address (otherwise the move is internal and should be skipped), and check the label quality of the deposit address, since a wrong label would turn an ordinary payment into a fake inflow.
Exercise
On Ethereum, a user calls a contract that immediately forwards 2 ETH to a labeled exchange address. The top-level transaction shows only a call to the contract with value 0. Which data source reveals the 2 ETH movement, and what happens to your inflow total if you skip it?
Show answer
An execution trace (internal transactions) reveals the forwarded 2 ETH. A pipeline that reads only the transaction list sees a zero-value call and records no inflow, so the exchange's inflow total is understated by 2 ETH for this event.