<p><strong>Do not</strong> use bitcoind's accounts feature. Handle user/address associations in your DB instead.</p>

<p>Even Gavin Andresen himself has said:</p>

<blockquote>
  <p>... the bitcoind "accounts" code hasn't been optimized for that.  For
  example, computing an account's balance loops through all transactions
  in the wallet, so will take an increasing amount of time the more
  transactions you have in the wallet.</p>
</blockquote>

<p><a href="https://bitcointalk.org/index.php?topic=40430.0" rel="nofollow">https://bitcointalk.org/index.php?topic=40430.0</a></p>

<p>As for how to handle transactions/purchases in your game, I'd recommend setting up a field in your users table to store a user's balance. When a user deposits to their personal deposit address you'd update the field with their new balance. Set up a cron task that runs every minute and calls a script that does something like this: </p>

<ol>
<li>Pull latest 10000 transactions from bitcoind and store them in an array.</li>
<li>Filter out transactions with less than 2 confirmations, or whatever number makes you feel comfortable.</li>
<li>Insert all confirmed transactions into a DB table called "transactions." Don't allow duplicates. Add a boolean field called "processed"</li>
<li>When you're ready to process transactions to the users' accounts, pull all rows where processed = 0 and category = 'receive'. Process each deposit by adding the value of the transaction to the user's account, and set processed = 1 on the processed transactions.</li>
</ol>

<p>When a user buys something in-game, just subtract the purchase amount from their balance.</p>

<p>This will be a lot more efficient, scalable, and easy-to-use than relying on bitcoind for user balance-keeping. It will also be a lot easier to migrate data around in the future, if necessary.</p>

CC BY-SA 3.0
2014-03-30T17:36:17.467Z
2014-03-30T17:36:17.467Z
jq9939
<p>You are very correct, but:</p>

<ol>
<li>I would use getaccountaddress instead of getnewaddress. If the user doesn't exist, which is the case for a new user, the wallet will create a new one anyway.</li>
<li>I always would use the user's name to recover from the wallet his address, there's no sense to store it again on a DB. ¿?</li>
</ol>

<p>The time for a balance to be confirmed depends on the coin and its network.</p>

<p>$bitcoin->move($useraddress, $myaddress, $amount) Yes, it is instantaneous. There's no transaction broadcasted to the net. </p>

<p>How can I test out my bitcoind/php application with testnet? Anyone has any experience of this? ---> Go to the wiki, U have to set a specific parameter on bicoin.conf</p>

CC BY-SA 3.0
2014-03-29T17:30:07.827Z
2014-03-29T17:35:52.927Z
2014-03-29T17:35:52.927Z
foroplus
<p>You are correct on most of it rest can be experienced in development process. So I suggest you to start development and ask if you find it difficult to fix issues.</p>

CC BY-SA 3.0
2014-02-24T01:16:05.173Z
https://en.bitcoin.it/wiki/Running_Bitcoin gives you either the command line option or the configuration file option to use testnet.
ChrisW
CC BY-SA 3.0
2014-01-24T23:20:21.167Z
2014-01-24T23:20:21.167Z
Khan Shahrukh
<p>I asked this question on bitcoin forum too (just for the record):
<a href="https://bitcointalk.org/index.php?topic=430972.new#new" rel="nofollow">https://bitcointalk.org/index.php?topic=430972.new#new</a></p>

<p>I am trying to develop a php web application (a game, and not a gambling site) where users can make in-game purchases. Now if I am understanding it right, The architecture (using bitcoind and Json-rpc api) should be: </p>

<ul>
<li><p>When a new user registers - generate a new address for the new user: </p>

<blockquote>
  <p>$bitcoin->getnewaddress("username");</p>
</blockquote>

<p>Store that wallet address for later use in my DB.</p>

<p>Display that address and let a user send bitcoins to that address. </p>

<p>Check the balance of that address using the </p>

<blockquote>
  <p>$bitcoin->getbalance($username,0)</p>
</blockquote>

<p>for unonfirmed balance  and </p>

<blockquote>
  <p>getbalance($username,6)</p>
</blockquote>

<p>confirmed balance.    </p>

<p>Now I can let the user buy my in-game items
and use </p>

<blockquote>
  <p>$bitcoin->move($useraddress, $myaddress, $amount)</p>
</blockquote>

<p>to debit his account and give him the stuff he needs.</p></li>
</ul>

<p>Questions: </p>

<pre><code>1. Does this sound about right?  Or am I completely off here! 
2. How long does it take for the unconfirmed balance to show up?  [$bitcoin-&gt;getbalance($username,0)] 
3. $bitcoin-&gt;move($useraddress, $myaddress, $amount)  is a change of bitcoins between addresses within my wallet, so this should be
offchain and instantaneous, right? 
4.  How can some gambling dice sites like coinroll.it ( https://bitcointalk.org/index.php?topic=191176.0) accept unconfirmed
transactions and lets  people play instantly? are they just taking
the risk or there is something that I am not aware of. 
5. How can I test out my bitcoind/php application with testnet? Anyone has any experience of this?
6. Suggestions on Security are welcome...
</code></pre>

<p>I am going to make parts of my code open source when I figure this shit out</p>

<bitcoind><php>
Bitcoind JSON-RPC - PHP user handling
EnderWiggins
# Bitcoind JSON-RPC - PHP user handling

(Asked by: username_0 on 2014-01-24)

I asked this question on bitcoin forum too (just for the record):
https://bitcointalk.org/index.php?topic=430972.new#new
I am trying to develop a php web application (a game, and not a gambling site) where users can make in-game purchases. Now if I am understanding it right, The architecture (using bitcoind and Json-rpc api) should be: 

When a new user registers - generate a new address for the new user: 

$bitcoin->getnewaddress("username");

Store that wallet address for later use in my DB.
Display that address and let a user send bitcoins to that address. 
Check the balance of that address using the 

$bitcoin->getbalance($username,0)

for unonfirmed balance  and 

getbalance($username,6)

confirmed balance.    
Now I can let the user buy my in-game items
and use 

$bitcoin->move($useraddress, $myaddress, $amount)

to debit his account and give him the stuff he needs.

Questions: 
1. Does this sound about right?  Or am I completely off here! 
2. How long does it take for the unconfirmed balance to show up?  [$bitcoin->getbalance($username,0)] 
3. $bitcoin->move($useraddress, $myaddress, $amount)  is a change of bitcoins between addresses within my wallet, so this should be
offchain and instantaneous, right? 
4.  How can some gambling dice sites like coinroll.it ( https://bitcointalk.org/index.php?topic=191176.0) accept unconfirmed
transactions and lets  people play instantly? are they just taking
the risk or there is something that I am not aware of. 
5. How can I test out my bitcoind/php application with testnet? Anyone has any experience of this?
6. Suggestions on Security are welcome...

I am going to make parts of my code open source when I figure this shit out

**Tags:** bitcoind, php


---

## 3 answers

### Answer by username_1 on 2014-03-30. Score: 1

**Do not** use bitcoind's accounts feature. Handle user/address associations in your DB instead.

Even Gavin Andresen himself has said:

> ... the bitcoind "accounts" code hasn't been optimized for that. For
> example, computing an account's balance loops through all transactions
> in the wallet, so will take an increasing amount of time the more
> transactions you have in the wallet.

<https://bitcointalk.org/index.php?topic=40430.0>

As for how to handle transactions/purchases in your game, I'd recommend setting up a field in your users table to store a user's balance. When a user deposits to their personal deposit address you'd update the field with their new balance. Set up a cron task that runs every minute and calls a script that does something like this:

1. Pull latest 10000 transactions from bitcoind and store them in an array.
2. Filter out transactions with less than 2 confirmations, or whatever number makes you feel comfortable.
3. Insert all confirmed transactions into a DB table called "transactions." Don't allow duplicates. Add a boolean field called "processed"
4. When you're ready to process transactions to the users' accounts, pull all rows where processed = 0 and category = 'receive'. Process each deposit by adding the value of the transaction to the user's account, and set processed = 1 on the processed transactions.

When a user buys something in-game, just subtract the purchase amount from their balance.

This will be a lot more efficient, scalable, and easy-to-use than relying on bitcoind for user balance-keeping. It will also be a lot easier to migrate data around in the future, if necessary.


---

### Answer by username_2 on 2014-03-29. Score: 1

You are very correct, but:

1. I would use getaccountaddress instead of getnewaddress. If the user doesn't exist, which is the case for a new user, the wallet will create a new one anyway.
2. I always would use the user's name to recover from the wallet his address, there's no sense to store it again on a DB. ¿?

The time for a balance to be confirmed depends on the coin and its network.

$bitcoin->move($useraddress, $myaddress, $amount) Yes, it is instantaneous. There's no transaction broadcasted to the net.

How can I test out my bitcoind/php application with testnet? Anyone has any experience of this? ---> Go to the wiki, U have to set a specific parameter on bicoin.conf


---

### Answer by username_3 on 2014-01-24. Score: 0

You are correct on most of it rest can be experienced in development process. So I suggest you to start development and ask if you find it difficult to fix issues.

> **Comments:**
> **username_4**: https://en.bitcoin.it/wiki/Running_Bitcoin gives you either the command line option or the configuration file option to use testnet.  

---

