I have a transaction with 3 inputs and 2 outputs. 1output is the recipient and the other output is change address. How should I calculate the fee and apply it to output?
-
4Does this answer your question? Specifying fee amount using bitcoinjs– MCCCSMay 5, 2020 at 15:01
-
Your question shows a bit of a misunderstanding. If you read some of the hits after searching for "transaction fee," I think you'll ask a better question. You may have meant, "How do I SET the fee in a BitcoinJS raw transaction?" but even this would show that you don't realize the fee is simply the difference between the sum of the inputs and the sum of the outputs. Or perhaps you meant to ask for advice in choosing a fee (bitcoin.stackexchange.com/questions/64443/…).– Dave ScoteseMay 29, 2020 at 1:01
1 Answer
The TX fee is the difference between the totals of all the input values and all the output values. If you are looking at a single TX you can see all the output values, but how do you get the input values? They are not included in the TX itself. What you must do is look up the UTXO being spent by each TX input and total their values. (The data structure used to track and process all the input coins is called a "view").
Note that there is no "applying a fee to the output". A TX has no extra fee field or property, and the fee itself does not occupy an entire output.
If you are constructing a TX where the input values equal exactly the output values, you "include" a fee by reducing the value of one of the outputs.