AustEcon / bitsv

BitSV: Bitcoin made easy. Documentation:
https://AustEcon.github.io/bitsv
MIT License
97 stars 28 forks source link

Consideration of adding "send-max" feature #18

Closed gitzhou closed 5 years ago

gitzhou commented 5 years ago

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 😄

ghost commented 5 years ago

Unless I'm missing something, we already have this.

https://ofek.github.io/bit/guide/transactions.html#transfer-funds

https://github.com/teran-mckinney/walkingliberty-python/blob/4fa8e932ddddde6d086e99beafeb44e5bb6212ac/walkingliberty/__init__.py#L98

I hope that's a little bit easier ;).

gitzhou commented 5 years ago

Unless I'm missing something, we already have this.

https://ofek.github.io/bit/guide/transactions.html#transfer-funds

https://github.com/teran-mckinney/walkingliberty-python/blob/4fa8e932ddddde6d086e99beafeb44e5bb6212ac/walkingliberty/__init__.py#L98

I hope that's a little bit easier ;).

WOW! Seems this is what I need. I will try this. Thanks for teaching 😄

ghost commented 5 years ago

No problem! :)

AustEcon commented 5 years ago

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!).

gitzhou commented 5 years ago

Hi @teran-mckinney and @AustEcon, I've tried and everything's fine. This is exactly what I need. Thank you very much Close this issue.