TeachMeBitcoin

Custom Python Fee Calculator

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python Fee Calculator

In this final guide, we will build a Python script that calculates the Total Fee and the Feerate of a transaction. Since the fee is implicit, we must simulate the process of looking up input values from the UTXO set.

The Python Fee Auditor

def calculate_transaction_fees(input_values, output_values, vsize):
    # 1. Sum all input satoshis
    total_in = sum(input_values)

    # 2. Sum all output satoshis
    total_out = sum(output_values)

    # 3. Calculate implicit fee
    fee = total_in - total_out

    print(f"--- Transaction Fee Audit ---")
    print(f"[*] Total Input Value:  {total_in:,} sats")
    print(f"[*] Total Output Value: {total_out:,} sats")
    print(f"[*] Calculated Fee:     {fee:,} sats")

    # Check for invalid transaction (outputs \u003e inputs)
    if fee \u003c 0:
        print("[ERROR] INVALID TRANSACTION: Outputs exceed inputs!")
        return

    # 4. Calculate Feerate
    feerate = fee / vsize
    print(f"[*] Virtual Size:       {vsize} vBytes")
    print(f"[*] Feerate:            {feerate:.2f} sats/vB")

    # 5. Interpretation
    if feerate \u003e 100:
        print("[STATUS] High Priority / Urgent")
    elif feerate \u003e 10:
        print("[STATUS] Standard Priority")
    else:
        print("[STATUS] Low Priority / Economy")

# --- Simulation ---

# Scenario: Spending two UTXOs to one recipient and one change address
inputs = [5000000, 2500000] # 0.075 BTC total
outputs = [7000000, 480000] # 0.07 BTC to recipient, remainder to change
vsize = 141 # Standard P2WPKH transaction size

calculate_transaction_fees(inputs, outputs, vsize)

How to Run the Calculator

  1. Ensure you have Python 3 installed.

  2. Copy the code into a file named fee_calculator.py.

  3. Run it using python3 fee_calculator.py.

Technical Takeaways

  1. Implicit Audit: Nodes must verify every input's value by looking at the previous transaction's outputs. This is why running a full node is essential for verifying you haven't been cheated.

  2. Satoshi Precision: Notice how we perform all calculations in satoshis, not BTC. This prevents floating-point rounding errors that could invalidate a transaction.

  3. Miner Selection: Your feerate is the only "resume" a miner looks at when deciding whether to hire your transaction for their next block.

Congratulations! You have completed the Transaction Fees module. You now understand the economic engine of the Bitcoin network.

☕ 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!