<blockquote>
  <p>How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?</p>
</blockquote>

<p>You can use the recently-introduced <code>fundrawtransaction</code> RPC:</p>

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

<blockquote>
  <p>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?</p>
</blockquote>

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

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

<p>Read up on <a href="https://en.bitcoin.it/wiki/Help:Accounts_explained" rel="nofollow noreferrer">how the account system works</a>. 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.                          </p>

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

<p>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).</p>

<p>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).</p>

<blockquote>
  <p>Can sendfrom take out money from other accounts other than the "" account?</p>
</blockquote>

<p>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.</p>

CC BY-SA 4.0
2016-04-03T14:48:33.357Z
2019-04-02T17:31:20.463Z
2019-04-02T17:31:20.463Z
Pieter Wuille
<p>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.</p>
<p>This is the setup:</p>
<ol>
<li>There is bitcoind running in the background so I have one wallet on the server. It has a default account name of <code>&quot;&quot;</code> (empty string) with some address.</li>
<li>I generate a new address for each user in the wallet. Let's say each user gets his/her own account named <code>&quot;user_1&quot;</code>, <code>&quot;user_2&quot;</code>, etc. I generate addresses by basically doing <code>bitcoin-cli getaccountaddress user_1</code>.</li>
<li>Users can deposit/withdraw money to/from this address.</li>
<li>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 <code>&quot;&quot;</code> account.</li>
</ol>
<p>For the transfers I am using the following command <code>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;</code>. 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.</p>
<p>I figure that there are two approaches to this solution:</p>
<blockquote>
<p><strong>Option 1</strong>: Calculate the precise transaction fee and based on that fee make a check.</p>
<p><strong>Option 2</strong>: Use <code>bitcoin-cli move &quot;&quot; &quot;user_1&quot; &lt;amount_that_is_negative&gt;</code> so it seems that the balance it not actually negative and that the site owner has decided to pay for the excess fee.</p>
</blockquote>
<p>So basically, I need to solve the negative address balance problem and could also some clarification on the following:</p>
<ul>
<li>How to do #1? i.e. How to calculate the size of the transaction (in bytes) + transaction fee in advance?</li>
<li>Using <code>bitcoin-cli settxfee &lt;amount&gt;</code> seems to return true but the actual transaction fee that is applied to me was much higher. Why was this the case?</li>
<li>I need to know more details about how the move command works. Is move an actual transaction that will incur transaction fees?</li>
<li>Is option 2 a good option to hide/solve my problem?</li>
<li>What are other options / correct implementation for implementing a withdraw button?</li>
<li>Can <code>sendfrom</code> take out money from other accounts other than the <code>&quot;&quot;</code> account?</li>
</ul>
<p>Thanks!</p>

CC BY-SA 3.0
2016-04-01T05:13:18.140Z
Question is a duplicate of http://bitcoin.stackexchange.com/questions/14260/negative-balance-after-sendfrom-bitcoind and as suggested "move" is a good way to do this.
dark knight
<bitcoind><transaction-fees>
Transaction fees and transactions with bitcoin-cli. Wallet address balance negative
Sanchit
# Transaction fees and transactions with bitcoin-cli. Wallet address balance negative

(Asked by: username_0 on 2016-03-29)

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:

1. There is bitcoind running in the background so I have one wallet on the server. It has a default account name of `""` (empty string) with some address.
2. I generate a new address for each user in the wallet. Let's say each user gets his/her own account named `"user_1"`, `"user_2"`, etc. I generate addresses by basically doing `bitcoin-cli getaccountaddress user_1`.
3. Users can deposit/withdraw money to/from this address.
4. 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 `""` account.

For the transfers I am using the following command `bitcoin-cli sendfrom user_1 <some_other_bitcoin_address> <amount> <min_confirmations> <comment> <comment_to>`. 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 "" "user_1" <amount_that_is_negative>` 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 <amount>` 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 `""` account?

Thanks!

**Tags:** bitcoind, transaction-fees

> **Comments:**
> **username_1**: Question is a duplicate of http://bitcoin.stackexchange.com/questions/14260/negative-balance-after-sendfrom-bitcoind and as suggested "move" is a good way to do this.  

---

## 1 answers

### Answer by username_2 on 2016-04-03. Score: 4 | Accepted answer

> 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](https://en.bitcoin.it/wiki/Help:Accounts_explained). 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.


---

