<p>I'm using the <a href="https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list" rel="nofollow">json API</a>. I need to keep all the account balances positive: I call <code>getbalance</code> first, check if it has enough bitcoins to proceed, and then <code>move</code> or <code>sendfrom</code>.</p>

<p>This approach causes two problems.</p>

<p>Problem 1 is that this is not an atomic operation, so after the first <code>getbalance</code> the balance could actually be changed.</p>

<p>Problem 2 only happens with <code>sendfrom</code>: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.</p>

<p>Solution to Problem 2 would be to <strong>know in advance the transaction fee</strong>: I noticed (though I didn't try) I can <em>set</em> it with <code>settxfee</code>, but I found no call to <em>get</em> it, is there?</p>

<p>While I could implement some external lock mechanism to solve Problem 1, I'd rather use a bitcoind native method, though I get it does not exist, does it? <strong>Hacking the <code>sendfrom</code> function to have it check the balance before sending</strong> would be enough, I guess?</p>

CC BY-SA 3.0
2014-07-23T23:21:54.797Z
Are you making a bitcoin web application? How can the balance be lowered without you knowing? All money out goes through your API (code), doesn't it?
Jori
CC BY-SA 3.0
2014-07-24T08:36:15.140Z
@Jori as I said, it's not an atomic operation.
o0'.
CC BY-SA 3.0
2014-07-24T08:40:49.047Z
How does that matter then? `bitcoind` will not send any coins out of an account if there are no coins in it. What is the purpose of checking the balance first?
Jori
CC BY-SA 3.0
2014-07-24T08:43:44.440Z
The solution to problem 2 is just set the transaction fee yourself or go along with the default (which is best IMHO). See https://en.bitcoin.it/wiki/Transaction_fees.
Jori
<json-rpc><transaction-fees><api><bitcoin-core-accounts>
API: keeping account balance positive (+knowing in advance the transaction fee)
o0'.
# API: keeping account balance positive (+knowing in advance the transaction fee)

(Asked by: username_0 on 2012-12-05)

I'm using the [json API](https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list). I need to keep all the account balances positive: I call `getbalance` first, check if it has enough bitcoins to proceed, and then `move` or `sendfrom`.

This approach causes two problems.

Problem 1 is that this is not an atomic operation, so after the first `getbalance` the balance could actually be changed.

Problem 2 only happens with `sendfrom`: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.

Solution to Problem 2 would be to **know in advance the transaction fee**: I noticed (though I didn't try) I can *set* it with `settxfee`, but I found no call to *get* it, is there?

While I could implement some external lock mechanism to solve Problem 1, I'd rather use a bitcoind native method, though I get it does not exist, does it? **Hacking the `sendfrom` function to have it check the balance before sending** would be enough, I guess?

**Tags:** json-rpc, transaction-fees, api, bitcoin-core-accounts

> **Comments:**
> **username_1**: Are you making a bitcoin web application? How can the balance be lowered without you knowing? All money out goes through your API (code), doesn't it?  
> **username_0**: @username_1 as I said, it's not an atomic operation.  
> **username_1**: How does that matter then? `bitcoind` will not send any coins out of an account if there are no coins in it. What is the purpose of checking the balance first?  
> **username_1**: The solution to problem 2 is just set the transaction fee yourself or go along with the default (which is best IMHO). See https://en.bitcoin.it/wiki/Transaction_fees.  

---

## 0 answers

