Consideration of adding "send-max" feature
gitzhou opened this issue · comments
Hi @AustEcon, per our discussion in Slack, raising this issue.
Currently, it's not easy to send all the amount of several unspents since the fee is calculated inside the functions. Below code is my way, not clean and elegant, but works...
# Sum the amount of these UTXOs
amount = 0
for unspent in unspent_group:
amount += unspent.amount
# Calculate transaction fee
fee = 0
try:
# Send the whole balance directly should be fail since insufficient funds
priv_key.send(outputs=[(send_to, amount, 'satoshi')], message=message, unspents=unspent_group)
except InsufficientFunds as e:
match = re.match(r'Balance (.+) is less than (.+)\(including fee\).', str(e))
if len(match.groups()) == 2:
fee = int(match.group(2)) - int(match.group(1))
# Resend the transaction including the accurate fee
tx_hash = priv_key.send(outputs=[(send_to, amount - fee, 'satoshi')], message=message, unspents=unspent_group)
Already read the code of estimate_tx_fee
and sanitize_tx_data
calculated_fee = estimate_tx_fee(len(unspents), num_outputs, fee, compressed, total_op_return_size)
It seems that I need to recalculate the total OP_RETURN size again by myself at least, also need many related parameters, which means repeated code(not sure).
So I'm wondering whether bitsv could expose some functions that calculate the fee ahead of time or just give out a function like create_transaction
and send
, which will send all the amount of inputs.
Thanks 😄
Unless I'm missing something, we already have this.
https://ofek.github.io/bit/guide/transactions.html#transfer-funds
I hope that's a little bit easier ;).
Unless I'm missing something, we already have this.
https://ofek.github.io/bit/guide/transactions.html#transfer-funds
I hope that's a little bit easier ;).
WOW!
Seems this is what I need. I will try this.
Thanks for teaching 😄
No problem! :)
Sorry I've been busy. If this suits your needs can just add a simple send_all() function anyway because it'll be more intuitive to find etc (as we have demonstrated here!).