Bitcoin

Wallets – What is the most efficient way to create raw transactions with a specific commission rate?

yes! The trick is to calculate each input. valid value Rather than nominal value.

A transaction consists of three parts: overhead, input, and output.

  • Print:
    The size of the output script is determined by the recipient’s address, so its size is determined by the transaction instructions. We don’t know yet whether we need change output or not, but we do know how big it will be once we generate it.

  • overhead:
    Transaction overhead is 10 bytes for non-Segwit transactions and 42 WU for Segwit transactions. You can guess which one you’ll need, but in the worst case you’ll overestimate by 0.5vB. Therefore, the overhead size can also be considered fixed.

  • input:
    The tricky part is entering transactions. You don’t know how many addresses you will need, and you don’t know what the input script size will be if you have inputs of various address formats. Additionally, signatures are not always the same size. However, we know the maximum size of the signature and the size of the input script for each UTXO. It can be calculated along with the target commission rate. valid value:

    effectiveValue = utxo.value − feePerByte × bytesPerInput²

that much Selection target It starts with the sum of the recipient amount. We simplify the coin selection problem by adding a fixed cost to the selection. The selection is then made through calculations. valid value Rather than having a nominal value, each input has already been paid for at the time of selection, so you no longer need to worry about the cost implications of the input after selection. instead of each valid value Moves fully towards the selected target.

There are two strategies regarding change output: First, if you want to build a transaction that prevents creating changes (e.g. using branching and boundary selection), you leave the target untouched. target = recipient amounts + fees for fixed transaction parts. Second, if you use a strategy that generates changes, add a buffer to the selection large enough to cover the fee of the change output, leaving enough for an appropriately sized change output. target = recipient amounts + fees for fixed tx parts + change output cost + minChange.

For signatures, it is estimated using the maximum signature length. Since the signature is part of the witness for the segwit input, only a small overestimation will occur, causing the target fee rate to be exceeded by a small amount.3 Alternatively, signature grinding can be used to save the expected 0.5 bytes per signature and estimate the expected fee rate. there is. Commission rates more accurately


¹ Assume a transaction has no more than 253 inputs and no more than 253 outputs. The input/output counter uses 3 bytes instead of 1 byte to allow for more input/output counts.
² The effective value was suggested in section 5.3 of Evaluating Coin Selection Strategies (Erhardt 2016). Disclosure: Written by you.
3 It is desirable to exceed the target commission rate. It costs only a few satoshis and gives you a slightly higher transaction priority. Missing your target commission rate is problematic because some payment processors require a minimum commission rate to accept payments and falling below their base commission rate is problematic. minRelayTxFeeRate This can prevent transactions from being completely relayed.

Related Articles

Back to top button