Transaction fees and transactions with bitcoin-cli. Wallet address balance negative I'm trying to make a bitcoin website and I tried to implement a sort of 'withdraw' button which would allow you to send your money in the wallet address to some other address. The problem I am facing is that post-transaction it is taking making the address balance negative.
This is the setup:

There is bitcoind running in the background so I have one wallet on the server. It has a default account name of &quot;&quot; (empty string) with some address.
I generate a new address for each user in the wallet. Let's say each user gets his/her own account named &quot;user_1&quot;, &quot;user_2&quot;, etc. I generate addresses by basically doing bitcoin-cli getaccountaddress user_1.
Users can deposit/withdraw money to/from this address.
If I withdraw too much money from this address it'll make the address balance negative and I presume it is also taking out coins from the master account, i.e. the &quot;&quot; account.

For the transfers I am using the following command bitcoin-cli sendfrom user_1 &lt;some_other_bitcoin_address&gt; &lt;amount&gt; &lt;min_confirmations&gt; &lt;comment&gt; &lt;comment_to&gt;. If I truly pick more money than is available in the wallet then it'll error and tell me that I don't have enough balance. But in a certain edge case it goes through and make the address balance negative.
I figure that there are two approaches to this solution:

Option 1: Calculate the precise transaction fee and based on that fee make a check.
Option 2: Use bitcoin-cli move &quot;&quot; &quot;user_1&quot; &lt;amount_that_is_negative&gt; so it seems that the balance it not actually negative and that the site owner has decided to pay for the excess fee.

So basically, I need to solve the negative address balance problem and could also some clarification on the following:

How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?
Using bitcoin-cli settxfee &lt;amount&gt; seems to return true but the actual transaction fee that is applied to me was much higher. Why was this the case?
I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees?
Is option 2 a good option to hide/solve my problem?
What are other options / correct implementation for implementing a withdraw button?
Can sendfrom take out money from other accounts other than the &quot;&quot; account?

Thanks!
How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?


You can use the recently-introduced fundrawtransaction RPC:


First create a transaction with just the outputs you want but no inputs, using createrawtransaction.
Then call fundrawtransaction on it (which will compute fee, and add inputs and change).
Observe the fee, and do balance checks for your user.
If acceptable, sign the transaction (using signrawtransaction) and broadcast it (using sendrawtransaction).



  Using bitcoin-cli settxfee  seems to return true but the actual transaction fee that is applied to me was much higher. Why was this the case?


settxfee sets the fee per 1000 bytes, not the total. If your transaction ends up being larger than 1000 bytes, the fee will be higher.


  I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees?


Read up on how the account system works. There is no transaction involved, as account balances are simply local bean counters. All coins in your wallet are shared across all accounts. Account balances just keep track how much of them belong to whom (but not which specific coins belong to whom). Move simply updates the balances without any transaction on the network.                          


  Is option 2 a good option to hide/solve my problem?
  What are other options / correct implementation for implementing a withdraw button?                


No, because the account system is deprecated and will be removed in a future version. Reasons for this is that it is very hard to understand (as this question shows!), and makes backups nearly impossible (as the account information for sendfrom cannot be recovered from the blockchain, restoring a wallet backup will mess up your accounting).

Instead, use the fundrawtransaction mechanism described above, and implement user balances in a higher-level layer (which does for example stores the account information in a database with proper backups).


  Can sendfrom take out money from other accounts other than the "" account?


By definition, no. It creates a transaction using all coins in the entire wallet to fund it, and then subtracts the amount sent plus fee from the specified account.