How Transaction Fees Are Calculated
How Transaction Fees Are Calculated
If you look at the raw bytes of a Bitcoin transaction, you will find fields for inputs, outputs, scripts, and timelocks. However, you will notice one glaring omission: there is no "Fee" field in a Bitcoin transaction.
Instead, Bitcoin transaction fees are calculated using an implicit mathematical remainder.
π The Formula of Implicit Fees
In Bitcoin's accounting system, a transaction fee is defined as the difference between the total value of all inputs consumed and the total value of all outputs created:
$$\text{Transaction Fee} = \sum \text{Input Values} - \sum \text{Output Values}$$
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONSUMED INPUT UTXOs β
β UTXO 1 (1.2 BTC) + UTXO 2 (0.8 BTC) = 2.0 BTC β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β CREATED OUTPUT UTXOs β
β Sent to Bob (1.5 BTC) β
β Sent to Alice (0.49 BTC) (Change Output) β
β Total Created Outputs = 1.99 BTC β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ
β
βΌ
Implicit Remainder:
2.00 BTC - 1.99 BTC = 0.01 BTC (Miner Fee)
For a transaction to be considered valid by the network, it must respect the Conservation of Value rule: $$\sum \text{Output Values} \le \sum \text{Input Values}$$
- If total outputs exceed total inputs, nodes instantly reject the transaction as a mathematical attempt to mint counterfeit coins.
- The leftover remainder is left unallocated. When a miner successfully processes the block, their node is permitted to add this leftover remainder to their Coinbase transaction reward, claiming it as a tip.
π An Audit Walkthrough
Letβs trace a real-world transaction scenario:
- The Goal: Alice wants to send 0.35 BTC to Bob.
- The Inputs: Alice's wallet searches the UTXO database and finds a single unspent output belonging to Alice worth 0.50 BTC. The wallet creates an input referencing this UTXO.
- The Outputs: The wallet constructs two outputs:
- Bob's Output:
35000000satoshis (0.35 BTC). - Alice's Change Output:
14950000satoshis (0.1495 BTC).
- Bob's Output:
- The Remainder Math:
- Total Input Value:
50000000satoshis (0.50 BTC). - Total Output Value:
35000000 + 14950000 = 49950000satoshis (0.4995 BTC). - The leftover amount:
50000000 - 49950000 = 50000satoshis (0.0005 BTC).
- Total Input Value:
- The Result: The miner who mines this block receives 50,000 satoshis as their transaction fee. Alice pays Bob 0.35 BTC, keeps 0.1495 BTC in change, and pays 0.0005 BTC to the network.
β οΈ The Danger of "Fat-Finger" Errors
Because fees are implicit, the safety of your funds depends entirely on your wallet software constructing a proper Change Output.
If Alice wants to spend her 0.50 BTC UTXO to send Bob 0.01 BTC, but her wallet is poorly programmed and fails to create a change output back to Alice:
* Input Value: 50000000 satoshis (0.50 BTC).
* Output Value: 1000000 satoshis (0.01 BTC).
* Leftover Remainder: 49000000 satoshis (0.49 BTC).
By consensus, the entire 0.49 BTC ($20,000+ USD) is immediately claimed by the miner as a fee!
These eventsβknown as "fat-finger errors"βhave occurred multiple times in Bitcoinβs history. Modern, verified hardware and software wallets are engineered with strict safety checks to prevent users from broadcasting transactions with exceptionally large remainder fees, ensuring that self-sovereign accounting remains safe and secure.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: