I'm sorry if this has been answered before, but I haven't found anyone really bring this up.
I am trying to construct a PSBT, which assumes a fixed feerate and a fixed amount to send to some address. Any extra sats is change back to the sender. Something like:
const psbt = new Psbt({ network: bitcoin.networks.bitcoin });
psbt.addInput({hash, index, witnessUtxo: {...}});
psbt.addOutput({address: recipient, value: amountToSend});
psbt.addOutput({address: sender, value: changeAmount}); // total inputs - amountToSend - fees
However, creating that last change output seems like a circular exercise:
- To determine the last output to add to the PSBT, we need to know the exact fee in satoshis we're willing to spend.
- To know the exact fee in satoshis, we need to know the virtual bytes that the tx will consume, which requires us having a fully constructed and signed PSBT i.e.
signedPsbt.extractTransaction().virtualSize();
.
Am I missing something obvious here? Is there something to help estimate vsize before signing a PSBT (by estimating the size of signatures)? Or something to help estimate fees given number of inputs and outputs?
Any help is appreciated. Thank you in advance!