Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: bananas on March 06, 2014, 04:55:43 AM



Title: How to avoid negative account balance due to fee?
Post by: bananas on March 06, 2014, 04:55:43 AM
Supposing an app(running btc daemon) where the user can withdraw all the funds from his account(sendfrom comand). When the suer withdraw 100% of his balance it may go negative due to the transaction fee, and such fee will be taken from the main account. How could i avoid that? I can't predict the fee if any as far as i know? What is the solution?


Title: Re: How to avoid negative account balance due to fee?
Post by: jl2012 on March 06, 2014, 05:24:25 AM
Bitcoin won't allow negative balance anyway


Title: Re: How to avoid negative account balance due to fee?
Post by: Bitalo_Maciej on March 06, 2014, 07:17:51 AM
You CAN predict the fee, and YOU are in control of setting it. The algorithm is pretty simple (at this moment, it will change in next versions):

Code:
ceil((TX_SIZE_IN_BYTES) / 1000) * 0.0001

So for 250 bytes you pay 250 / 1000 * 0.0001 = 0.0001 BTC.
For 3200 bytes you pay 0.0004 BTC.
Etc.

If the amount to send + calculated fee is bigger then the balance, you have to subtract the fee from the amount itself, so you will send less to cover up for the fee.


Title: Re: How to avoid negative account balance due to fee?
Post by: donsanto on March 06, 2014, 07:24:54 AM
It is impossible to have a negative balance. The bitcoin protocol does not allow it.


Title: Re: How to avoid negative account balance due to fee?
Post by: bananas on March 06, 2014, 07:39:46 AM
It is impossible to have a negative balance. The bitcoin protocol does not allow it.

Are you sure? I mean on an individual account, not on the wallet toal. Here it gets negative.


Title: Re: How to avoid negative account balance due to fee?
Post by: bananas on March 06, 2014, 07:46:50 AM
You CAN predict the fee, and YOU are in control of setting it. The algorithm is pretty simple (at this moment, it will change in next versions):

Code:
ceil((TX_SIZE_IN_BYTES) / 1000) * 0.0001

So for 250 bytes you pay 250 / 1000 * 0.0001 = 0.0001 BTC.
For 3200 bytes you pay 0.0004 BTC.
Etc.

If the amount to send + calculated fee is bigger then the balance, you have to subtract the fee from the amount itself, so you will send less to cover up for the fee.

Thanks, but now i don't know how i will predict the tx size?

My withdraw code is just:

$is_valid = $api->call('sendfrom',$account,$address,$btc,6);


Is not there a way to just tell the wallet daemon to not allow negative balances? thanks


Title: Re: How to avoid negative account balance due to fee?
Post by: Bitalo_Maciej on March 06, 2014, 07:57:52 AM
Please don't use bitcoind accounts for that purpose. They weren't meant for this, and you will start to get bugs. I'm not sure what exactly happens when you try to send whole balance of one account to somewhere else. Either it will fail with an error message, or it will take the fee from the amount as I said (unlikely, I think), or - which is a disaster in your case, it will take the fee from another unspent output without you knowing it until it hits you.


Title: Re: How to avoid negative account balance due to fee?
Post by: bananas on March 06, 2014, 08:26:53 AM
Please don't use bitcoind accounts for that purpose. They weren't meant for this, and you will start to get bugs. I'm not sure what exactly happens when you try to send whole balance of one account to somewhere else. Either it will fail with an error message, or it will take the fee from the amount as I said (unlikely, I think), or - which is a disaster in your case, it will take the fee from another unspent output without you knowing it until it hits you.

What bugs?

It takes the fee from the account but when you send the whole  account balance it gets negative due to the fee and send the fee from the main(my own) account (which was my issue).

But suppose i make my own account system as you suggested, i will have the same issue. I still have to know the fee before sending any funds or it will be taken from me.

- user has funds to send X
- X + FEE is larger than user funds
- user sends X
- the system will send X + FEE anyway cause my wallet has enough funds

I need to take X + FEE from the user, and avoid sending it if the total is larger than his funds.


Title: Re: How to avoid negative account balance due to fee?
Post by: Bitalo_Maciej on March 06, 2014, 09:00:53 AM
Actually, there is a way to calculate the length of the transaction before sending it:

Code:
10 + (148 * I) + (34 * O) + I

Where I is the number of inputs, O is the number of outputs in the transaction. This assumes that all inputs are spending normal "pay to address" outputs, and all the outputs are normal "pay to address" outputs as well.


Title: Re: How to avoid negative account balance due to fee?
Post by: bananas on March 07, 2014, 04:15:52 AM
Actually, there is a way to calculate the length of the transaction before sending it:

Code:
10 + (148 * I) + (34 * O) + I

Where I is the number of inputs, O is the number of outputs in the transaction. This assumes that all inputs are spending normal "pay to address" outputs, and all the outputs are normal "pay to address" outputs as well.

How do i know the number of inputs ?