I have a script that creates a transaction based on multiple inputs and outputs, I can get the tx vsize with get_vsize and multiply it by a "sat per vb" amount to estimate fees, and subtract that from my total inputs - total outputs to know if I have enough change to send to another address.
The problem is that to add a change address I need to add another output AND sign my inputs again to avoid signature mismatch both of which increase the tx size and invalidates my change amount so I'm stuck in a catch 22.
The way I'm handling it now is to hardcode a fee and manually tweak it until it roughly hits the "sat per vb" number I want to go for but it's not very efficient. Is there a better way to accomplish this?
You create the tx (inlc. the change address with any value) and sign it. Then calculate the tx fee, update the amount of the fees/change address and re-sign the transaction before broadcasting.
I have a script that creates a transaction based on multiple inputs and outputs, I can get the tx vsize with
get_vsize
and multiply it by a "sat per vb" amount to estimate fees, and subtract that from my total inputs - total outputs to know if I have enough change to send to another address.The problem is that to add a change address I need to add another output AND sign my inputs again to avoid signature mismatch both of which increase the tx size and invalidates my change amount so I'm stuck in a catch 22.
The way I'm handling it now is to hardcode a fee and manually tweak it until it roughly hits the "sat per vb" number I want to go for but it's not very efficient. Is there a better way to accomplish this?
Great job on the library btw!