Bitcoin Transaction Fees.
In Bitcoin, all transactions pay a fee for them to be processed by miners. In this article, we learn about Bitcoin transaction fees, and how they are calculated and encoded in Bitcoin transactions.
A secure immutable blockchain such as Bitcoin or Ethereum relies on decentralized nodes managed by miners for the processing of transactions, this includes validation, adding the transaction to a block that is added to the chain. This process of mining is not free since miners use a lot of computing power(electricity) to manage the nodes and keep the online 24-7. For their work, they are rewarded using incentives, a miner's fee. During mining, nodes compete to solve a complex cryptographic puzzle, the winner is awarded bitcoins for their work and the new block is added to the chain. Miners also earn incentives from the processing of transactions. This transaction fee is constant regardless of the cryptocurrency markets. This structure of decentralization makes a blockchain secure.
In this article, we learn how transaction fees are encoded in a bitcoin transaction. Bitcoin wallets are programmed with the logic to calculate and include transaction fees. This is automatic and users don't have to manually calculate and include the fees. In the case of building an app on top of bitcoin, programmers must manually program this logic to deduct transaction fees.
Transaction fees not only incentives miners of the blockchain but acts as a security measure, in that, they prevent unintentional transactions which might slow the network down. Note that Bitcoin or Etherium transaction fees are different from the transaction fees charged by cryptocurrency exchanges such as coinbase.
The following graph shows the transaction fees chart for the total BTC value of all transaction fees paid to miners in the last 1 year;
Transaction Fees.
It is important to note that at one point in the past, Bitcoin transaction fees were not required, miners processed transactions with insufficient fees depending on the circumstances. However, this was not assured, the transaction could be processed immediately, delayed for a few days or weeks, or even not processed at all. Overtime fees became 'required', this is because most participants now paid for their transactions to be validated fast, meaning if you don't pay the transaction might not be processed and propagated to the network.
For bitcoin developers, it is important to implement dynamic fees using third-party services such as bitcoinfees that calculate them. Developers can also implement custom fee estimation algorithms for this task to do away with third parties. These algorithms calculate transaction fees based on capacity and other competing transactions on the blockchain. They give transactions a high probability of being chosen and included in the next block. Wallet owners can choose the amount of transaction fees to ensure that a transaction is prioritized, a transaction can be a high, low, or medium priority. The last two caters to users who don't mind the wait and are OK with the transaction taking a few hours, days, or weeks depending on the network traffic.
Calculating the Transaction Fees.
Transaction fees also vary, in that, a user can pay more for his/her transaction to be processed faster on the blockchain. A miner is attracted to process such a transaction because of the incentive he/she will get. However, this depends on the network traffic, if the traffic is high, then the fees will be higher and vice versa. Faster transaction processing usually happens in minutes if not immediately while others may take days or weeks, this of course depends on the network traffic.
A block can hold about 4 megabytes of data, meaning larger transactions will require more blocks. As mentioned a wallet is responsible for handling all this, usually, Bitcoin wallets have the option to choose a transaction fee rate that is calculated in satoshis - the smallest unit of Bitcoin. To get the total transaction fee to be paid by a transaction we find the product of the rate and the transaction size in kilobytes.
Bitcoin's Fee Estimation Algorithm.
The algorithm works as follows, First, it groups transactions into fee rate buckets and then estimated how long transactions in each bucket take to be mined. Here the algorithm assumes that transactions that pay higher fees will be included in the block before those paying lower fees.
When a transaction enters the memory pool(mempool) it notes the current block heigh. After a new block is added, the transaction counts the number of transactions in each bucket and the total fees paid in each bucket then calculates the number of blocks-X it took each transaction to be mined. The transaction also tracks an array of counters in each bucket and estimates how long it took for transactions to be confirmed. It increments all counters from X up to the highest bucket. This means that for any number Y that is greater than or equal to X, the transaction was successfully mined within Y blocks.
This algorithm prevents issues such as;
- Users paying exorbitant fees, the algorithm ignores such transactions.
- Miners validating transactions with extremely low fees, these are also ignored.
- Miner only adding high fee transactions to their blocks, these are ignored if the miner does not broadcast them as unconfirmed.
Encoding transaction Fees in Bitcoin Transactions.
In Bitcoin transactions, fees are calculated as the difference between the sum of inputs and the sum of outputs; fees = sum(inputs) - sum(outputs) This is a very important point, especially for developers who implement custom transaction fee estimation algorithms, developers must make sure not to underspend the inputs to get the right incentive to a miner and also include change for the transactions. E.g, in a transaction whereby A sends B 3 bitcoins out of 7 bitcoins, a developer must make sure that the appropriate change is sent to A after the transaction otherwise the whole 7 bitcoins will be sent to a miner as the fee.
Summary
Transaction fees encoded in a Bitcoin transaction increase the transaction data size. Miners are incentivized for all processed transactions. Mining makes the network decentralized and secure since all nodes have to validate a transaction. Halving in blockchain technologies is the process of the miner's fee getting halved after a certain period of time. This increases the computational power needed to mine new coins into circulation while lowering the incentive. Bitcoin developers who implement a custom transaction fee algorithm must make sure not to overpay a miner by making sure that change is returned to the sender and an appropriate transaction fee is deducted from the inputs. Bitcoin fees depend on the following two factors, the size of the transaction and the market fee density.