Calculation of Bitcoin Transaction Fees Explained

Published by Martin Schuster on

In this article, we will explain what bitcoin transaction fees are and how they are calculated.

Bitcoin transaction fees are paid whenever the owner of bitcoins wants to make a transaction. They are an incentive for the miner to include the transaction in the next mined block on the blockchain. If you have an urgent transaction that needs confirmation in the next block, you will have to attach a high fee to get into that block because there is a limited number of transactions per block. These fees are not directly included in a transaction, but can easily be determined by calculating the difference between the amount of token that went into the transaction and the amount that were sent out of it. Fees are measured in Satoshi per byte (1 Satoshi equals the smallest bitcoin unit). It is useful to calculate the size of a transaction before sending it, so we can estimate how much a transaction will cost us to get confirmed in the desired timeframe.

In the next part, we will explain how you can calculate the size of your transaction.

Calculating Bitcoin Transaction Size

With the further development of bitcoin, different types of addresses were introduced, like multi-Signature, segwit and taproot addresses, to name a few. All these types require different operations to execute a transaction, therefore the size of a transaction differs between these types of addresses. For our explanation, we limit ourselves to legacy addresses and the currently most used segwit addresses. Legacy Addresses were the first form of bitcoin addresses and start with a 1. Segwit or segregated witness addresses enabled the possibility to save the signature in the witness separated from the transaction, which reduces the transaction size. The size of the transaction consists of three main components, the inputs, outputs, and overhead.

Inputs

For every Input address, the following information depending on the type of address (legacy or segwit) is saved in the transaction:

  • Transaction ID and index number of the Unspent Transaction Output that is being spent
  • Signature script length
  • Signature script for legacy addresses, prove that the transaction is authorized by someone controlling the corresponding private keys
  • Sequence number indicates between the different inputs
  • Number of witness items
  • Witness item for segwit addresses, prove transaction is authorized by someone controlling the corresponding private keys

All this information accumulates to 148 bytes per input for legacy addresses and 68,5 bytes for segwit addresses.

Outputs

Like the input address, for every output address, the following information is saved.

  • Amount Bitcoin being paid
  • Public Key Script length
  • Public Key Script, a specification which conditions need to be fulfilled for the output to be spent. Different Address types have different conditions

 For legacy addresses, it is 34 bytes for each output address. Segwit addresses require 31 bytes each.

Overhead

The overhead consists of meta information for the transaction.

  • Transaction version number
  • Number of inputs
  • Number of outputs
  • Locktime, the earliest time when the transaction can be included in a block
  • Segwit Flag, is used to differentiate between segwit and legacy transactions

This information makes up 10 bytes of the transaction.

With these three components, we can derive a handy formula to calculate the transaction size for legacy and segwit addresses quickly.

Transaction size = number of inputs * 148 + number of outputs * 34 + 10
Formula Legacy Addresses
Transaction size = number of inputs * 68,5 + number of outputs * 31 + 10
Formula Segwit Addresses

Now that we know how to calculate the size of the transaction, we multiply this number by the current fee rate for the urgency we want. How this fee rate is determined is explained in the next part.

Determine the current fee rate

As we mentioned earlier, every bitcoin user has its reason and motivation to get their transaction confirmed within a given timeframe. For some, this needs to happen in the next one or two blocks, others may be able to wait a couple of hours or days.

Another important factor is the miners. Originally, these transaction fees were introduced to combat transactions spam that would slow down the network. Nowadays, these fees are an essential part in keeping the miners in the market. Because with each Bitcoin halving, the hash rate falls, and a falling hash rate simultaneously increases the cost of mining new blocks while the block reward itself decreases. Validating new blocks takes significant computing power and energy, so rising transaction fees incentivize miners to continue validating new blocks.

The block size is limited. Hence, miners need to maximize the transaction size to fee ratio and users need to offer adequate fees for their transactions. Therefore, there is no fixed fee rate that you can pay and then know when your transaction is confirmed on the blockchain. Most wallets give the user an estimate of how fast their transaction will be confirmed when paying a certain fee. This information is gathered from current and historical data of similar transactions on the blockchain.

Similar to the big stock exchanges, there are also times when the blockchain network is busier than at other times, and the fee rates will rise. Currently, with writing this Article, the Fee rate to get into the next block is between 6-12 Satoshi/byte, and if you can wait a couple of hours, you will need to pay at least 1 Satoshi/byte. With this information, you can create an estimate of how much your transaction is going to cost you.

If you’d like, you can use our Tool to check the size and estimated fee for your upcoming transaction.

Bitcoin Transaction Size and Fee Estimator

Transaction Size

Segwit stands for Segregated Witness and was introduced in BIP-0141 (Bitcoin Improvement Proposal 141). The size of a Segwit transaction is lower than the size of a legacy Bitcoin transaction, as Segwit reduces the overall size of the transaction.
Formula (For Segwit Bitcoin Addresses):
bytes = Number of Input * 68,5 + Number of Outputs * 31 + 10

Transaction Fee

Hello
bytes

Sources:

https://99bitcoins.com/bitcoin/fees/

https://bitcoinops.org/en/tools/calc-size/

https://river.com/learn/how-bitcoin-fees-work/