Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to estimate tx fee? #126

Closed
Th0rgal opened this issue Aug 6, 2020 · 13 comments
Closed

How to estimate tx fee? #126

Th0rgal opened this issue Aug 6, 2020 · 13 comments

Comments

@Th0rgal
Copy link

Th0rgal commented Aug 6, 2020

Hello,

I know the content of a balance, a quantity and I have 3 outputs: A, B and C and I'm using a segwit address. My quantity is at least equals to my balance.

  • I want to send 1% of the quantity to A.
    if my balance is superior enough to my quantity:
  • I want to send 99% (the rest of the quantity) to B.
  • Use C as a leftover address (the tx fee will be deducted from what he will receive)
    else:
  • I will use B as a leftover address, so he will receive 99% minus the tx fee. C will not receive anything

My question is: how to detect my balance is sufficient to send the rest of the quantity, pay the fees and add a leftover?
I looked into the code and found this:

def estimate_tx_fee(in_size, n_in, out_size, n_out, satoshis, segwit=False):

But I don't understand how to use it (I think it is not in the documentation).

Thanks for your help,
Thomas

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

Hi, I am a bit confused about what you are trying to do. Bit will automatically create a change output in the transaction so you don't lose anything. You may try to use key.send(...) and if you don't have enough balance it will return an error message.

You can get your balance via key.get_balance(). You can then get a very rough idea of the fee of your transaction via 200*bit.network.get_fee(fast=True) (this assumes your transaction will be 200 bytes).

@Th0rgal
Copy link
Author

Th0rgal commented Aug 6, 2020

Let's say I received 0.87035 BTC and I need to send 0.87 BTC to an address + the rest to another address. I need to know if I'll be able to send 0.87 BTC, pay the fees and send the rest to another address or if I need to send less than 0.87 BTC to pay the fees (and thus not have rest). I need to know the exact fee, right?

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

Is the other address your own bit address? In your example, you very likely will be able to send, because the minimum fee in bitcoin is 1 sat/byte and 35000 satoshis will cover the fee of a 35 kB transaction. Whether or not that transaction will confirm fast is a whole different issue. You can get an idea of what will left like this:

from bit.transaction import select_coins
selected_unspents, rest = select_coins(87000000, 1, [34], min_change=566, unspents=key.get_unspents())

The value rest will tell you how much is leftover. If you want to try for a different feerate use:

feerate  = 5 # your choice
selected_unspents, rest = select_coins(87000000, feerate, [34], min_change=566, unspents=key.get_unspents())

@Th0rgal
Copy link
Author

Th0rgal commented Aug 6, 2020

Is the other address your own bit address? In your example, you very likely will be able to send, because the minimum fee in bitcoin is 1 sat/byte and 35000 satoshis will cover the fee of a 35 kB transaction. Whether or not that transaction will confirm fast is a whole different issue. You can get an idea of what will left like this:

from bit.transaction import select_coins
selected_unspents, rest = select_coins(87000000, 1, [34], min_change=566, unspents=key.get_unspents())

The value rest will tell you how much is leftover. If you want to try for a different feerate use:

feerate  = 5 # your choice
selected_unspents, rest = select_coins(87000000, feerate, [34], min_change=566, unspents=key.get_unspents())

Nope the other address is not mine: my app is basically a middleman for a transaction.
What does [34], min_change and unspents stand for?

Edit: this might does the job: https://bitcoin.stackexchange.com/a/3011

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

34 is the size (in bytes) of the outputs and min_change is the minimum amount of change you want to have in your change (the minimum is 566 satoshis, this is called the dust limit).

If you want to send what is left to another person you can use the leftover parameter.

key.send([('A', 87000000)], fee=1,leftover='B')

@Th0rgal
Copy link
Author

Th0rgal commented Aug 6, 2020

34 is the size (in bytes) of the outputs and min_change is the minimum amount of change you want to have in your change (the minimum is 566 satoshis, this is called the dust limit).

If you want to send what is left to another person you can use the leftover parameter.

key.send([('A', 87000000)], fee=1,leftover='B')

Okay, sorry to bother you with all these questions and thank you very much for your help! What's the change exactly?

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

From the past question, unspents are UTXOs.

If you have 1 bitcoin and you want to send 0.1 BTC to someone, you send 0.1 BTC to that person and 0.9 BTC to yourself. The 0.9 BTC output is known as change. What is your app by the way?

@Th0rgal
Copy link
Author

Th0rgal commented Aug 6, 2020

From the past question, unspents are UTXOs.

If you have 1 bitcoin and you want to send 0.1 BTC to someone, you send 0.1 BTC to that person and 0.9 BTC to yourself. The 0.9 BTC output is known as change. What is your app by the way?

Oh okay so the change is the leftover, right? If I don't send the leftover to my own address, I don't have a change, is that it?
Here is the github link: https://github.com/cashplace
It's a project I started recently, I don't know if it will come to an end but I find it super interesting!

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

Yes, the leftover argument is the change. if you don't set the leftover argument it will send the change to your own address so you don't lose any funds.

@Th0rgal
Copy link
Author

Th0rgal commented Aug 6, 2020

Yes, the leftover argument is the change. if you don't set the leftover argument it will send the change to your own address so you don't lose any funds.

Yeah but I meant, if I set the leftover to another address, this is basically like any other output, isn't it?

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 6, 2020

not exactly because the fee is deducted from that output, so you don't know exactly how much it will be... contrary to the outputs in the output list where you know the exact amount they will receive

@pedr0-fr
Copy link
Contributor

pedr0-fr commented Aug 8, 2020

@Th0rgal is this solved? maybe close this issue?

@Th0rgal Th0rgal closed this as completed Aug 8, 2020
@Th0rgal
Copy link
Author

Th0rgal commented Aug 8, 2020

You are right, sorry 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants