TeachMeBitcoin

How Transaction Fees Are Calculated

From TeachMeBitcoin, the free encyclopedia ⏱️ 3 min read

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}$$


πŸ”Ž An Audit Walkthrough

Let’s trace a real-world transaction scenario:

  1. The Goal: Alice wants to send 0.35 BTC to Bob.
  2. 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.
  3. The Outputs: The wallet constructs two outputs:
    • Bob's Output: 35000000 satoshis (0.35 BTC).
    • Alice's Change Output: 14950000 satoshis (0.1495 BTC).
  4. The Remainder Math:
    • Total Input Value: 50000000 satoshis (0.50 BTC).
    • Total Output Value: 35000000 + 14950000 = 49950000 satoshis (0.4995 BTC).
    • The leftover amount: 50000000 - 49950000 = 50000 satoshis (0.0005 BTC).
  5. 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.

β˜• Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!