0

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?

3

2 Answers 2

0

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: enter image description here

2
  • 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.