I am attempting to build a working withdrawal system where users can withdraw funds in BTC. I am required to estimate the fee before allowing them to withdraw to include it in their payment. I was able to determine the maximum estimated fee based on inputs and outputs of the transaction. I had assumed that the transactions would have 1 input and 2 outputs as when I was testing that was the outcome. I am now seeing transactions with multiple inputs and outputs making the transaction size larger, therefore, increasing the fee. This is problematic for me as I am having to pay for these fees. I need to be able to determine the inputs and outputs of the transaction before I send it to the blockchain.
-
Possible duplicate of How to calculate transaction size before sending– pebwindkraftCommented Jun 4, 2017 at 12:41
-
I am trying to determine inputs/outputs, not the formula to a transaction size– MonstrumCommented Jun 5, 2017 at 6:52
-
1I don't really see your question, but I think the term you're looking for is "Coin selection" or "Coin control". This question might help explain the issue at hand: What is the coin selection algorithm?. Do you know what a UTXO is?– Murch ♦Commented Jun 6, 2017 at 5:54
1 Answer
If you are constructing your own transactions (rather than letting a wallet application do it for you), then you get to choose which inputs and outputs you use in the transaction. For that matter, you also get to choose the fee for the transaction, since any leftover from the sum of the inputs minus the sum of the outputs is the mining fee. Just remember that leaving too low of a fee will result in long delays of unconfirmed transactions.
Based on your needs, you can choose to use different coin selection algorithms. A coin selection algorithm's purpose is to select inputs from the pool of UTXOs that you control. Selecting too many for a single transaction will result in a higher fee. However, refusing to use smaller UTXOs will leave your entire pool comprised of small ones, forcing you to use them (and pay higher fees) until your pool recovers.
There are pros and cons to different algorithms, especially when you are concerned with fees, so be sure to understand the ramifications of your decision. A good place to start might be @Murch's presentation at Scaling Bitcoin.