How can I calculate the correct fees for a blockchain transaction? I use https://bitcoinfees.earn.com/api/v1/fees/recommended. I got hourfee - 88. How can i calculate bytes of transaction?
Corollary, the Earn.com fee estimates flatlined almost a year ago: twitter.com/murchandamus/status/1453367595076407298. You should use something else.– Murch ♦Oct 27, 2021 at 15:42
@Murch ty. so. what should i do?– Clark DevlinOct 27, 2021 at 18:19
You should pick a better source for feerate estimates, e.g. mempool.space/api/v1/fees/recommended perhaps?– Murch ♦Oct 27, 2021 at 18:45
For the most part, your wallet software should take care of all this. You should only determine how urgently you need the transaction to be confirmed and pick the feerate, the wallet would take care of the rest. The 88 you mention would likely refer to a feerate of 88 sats/vbyte here, although that sounds like a gross overestimate at the current market conditions.
If you want to manually build a transaction, you need to know a bit more.
Before you build a bitcoin transaction, you know which transaction outputs you are aiming to create. The recipients addresses tell you how much data it'll take to specify each output in the transaction. The transaction header is also mostly constant size, it's 10 bytes for non-segwit transactions and 42 weight units for segwit transactions.
The input side is a bit more complicated, as you usually do not know in advance how many inputs it'll take to fund your transaction. Your wallet may also be able to spend Unspent Transaction Outputs (UTXOs) of various output types which take different amounts of data to include in a transaction. So, you'll either need to pick the inputs first, then determine their size and calculate the fees, or you can do both in concert, by calculating the cost for each input and deducting in from the UTXO's values before considering them for inclusion.
The actual sizes for some popular output types can be seen in this table:
I try to do it with python. So, i know only minimal fee and amount of money. Oct 27, 2021 at 14:18
The minimal feerate for a transaction to get relayed is 1 sat/vbyte. The estimate on earn.com seems to be off by a factor of 20, so I would suggest that you don't use that.– Murch ♦Oct 27, 2021 at 14:30
For Python, may I suggest using the Mempool.space API. You can use their public API or run an instance of their software locally.
Here's is a sample code I've used in the past that should point you in the right direction.