Sure! The trick is to depend every enter with its efficient worth fairly than its nominal worth.
Transactions are composed of three components: the overhead, the inputs and the outputs.
-
Outputs:
The dimensions of the output scripts is decided by the recipient addresses, so their dimension is given by the transaction directions. We have no idea whether or not we’ll want a change output but, however we all know what dimension it will be if we created one. -
Overhead:
The transaction overhead is 10 bytes for non-segwit transactions, or 42 WU for segwit transactions¹. You may in all probability guess which one you may want, however within the worst case you overestimate by 0.5 vB. We are able to subsequently think about the overhead dimension additionally to be fastened. -
Inputs:
The difficult half is the transaction inputs. You have no idea what number of you have to, and won’t know what enter script sizes they will have when you’ve got inputs of varied deal with codecs. Moreover, signatures will not be at all times the identical dimension. Nevertheless, we do know the utmost dimension of the signature, and the dimensions of the enter script for every UTXO. Along with the goal payment charge, we are able to calculate the efficient worth:effectiveValue = utxo.worth − feePerByte × bytesPerInput
²
The choice goal begins out because the sum of the recipient quantities. We simplify the coin choice drawback by including the fastened prices to the choice goal. We then carry out the choice by way of counting efficient values fairly than nominal values. Since every enter has paid for itself already when it’s chosen, we not want to fret about an enter’s payment influence after choice; as an alternative every efficient worth goes absolutely in direction of the choice goal.
In regard to the change output, there are two methods. One, in case you are aiming to construct a transaction that avoids creating change, e.g. through the use of Department and Certain choice, you retain the goal as is: goal = recipient quantities + charges for fastened transaction components
. Two, in case you are utilizing a technique that can create change, you add a buffer to the choice goal that’s massive sufficient to cowl the payment of the change output and go away sufficient for a good-sized change output: goal = recipient quantities + charges for fastened tx components + change output value + minChange
.
For the signatures, you estimate utilizing the utmost signature size. For the reason that signatures are a part of the witness for segwit inputs, this outcomes solely in a small dimension overestimation which is able to result in minutely overshooting the goal payment charge.³ Alternatively, you should utilize signature grinding to save lots of an anticipated 0.5 bytes per signature and estimate the payment charge extra exactly.
¹ Assuming that the transaction has not more than 253 inputs and not more than 253 outputs. The enter/output counter will take 3 bytes as an alternative of 1 byte for better enter/output counts.
² Efficient worth was proposed in part 5.3 of An Analysis of Coin Choice Methods, Erhardt 2016, disclosure: authored by yours really.
³ Overshooting the goal payment charge is preferable; it prices just a few satoshis and barely will increase the precedence of the transaction. Undershooting the goal payment charge is problematic, since some fee processors require a minimal payment charge to just accept a fee and falling under the default minRelayTxFeeRate
can forestall a transaction from relaying altogether.