Determination and Calculation of Transaction Fees

Published by Mario Oettler on

In Bitcoin, users can choose the fees they want to pay. It is even possible to create a transaction with zero fees. But the higher the fee the more lucrative is the transaction for the miners. Which results in a shorter confirmation time (time until a transaction is included in a block).

The transaction fee is determined fairly simply. It is the difference between the input and the output of a transaction.

Sum(inputs) – Sum(outputs) = transaction fee

A numerical example will help.

A user wants to send 5 Coins from address A to address B. Therefore, he references an unspent transaction output (UTXO) as input for the transaction. Let’s say the UTXO contains 7 coins. If the output of the transaction is 5 coins the transaction fee is 7 – 5 = 2.

If the user wants to reference this UTXO but does only want to pay only 0.5 coins as a fee instead of 2, he can include a change address A’ in his transaction.

The input is still 7 coins, but 5 coins are sent to address B and 1.5 coins are sent to the change address A’. The difference 7 – 5 – 1.5 = 0.5 goes to the miner.

Miners have to solve the knapsack problem when deciding which transactions to include in their block. This problem is hard to solve. In fact, it is NP-hard. That’s why miners most likely apply easier solutions. A simple approach is to calculate the fees per byte and order all transactions according to this value.

TXsize in byteFee in mBTCfee per byte

The miner would order the transactions B-D-A-C.