Closed RiskingTime closed 6 years ago
I've revised my initial estimate for this task somewhat to 3 hours. To recap:
need to be able to compare the money we actually have (bitcoins on addresses that we control), with the money we tell users they have (numbers in our database). And in order to confirm this, we need to get the info from different places. Our database knows all the bitcoin deposit addresses we've generated for users, so we then need to see which of these addresses has money on it, but in order to do this, we don't ask our database, we need to ask the blockchain, then we list all the addresses and their associated balances (according to the blockchain). We can use blockcypher to do this, or we can also use any blockchain explorer. It might even make sense to not use blockcypher in this case because we want the data to be generated independently from our database. We will use the data from the blockchain to compare to what is in our database, then we can see whether we have enough bitcoins in our control in order to satisfy our depositors. ...
the third table listed on the admin page should be the "balance sheet". the image I pasted in the history of this thread shows what I would like to list in this table. And as described earlier in this post, the "balance sheet" is a way for us to compare the amount we owe users (the amounts in our database) to the amounts the blockchain says we have.
more description on the "balance sheet", if the first user to the site opens an account and makes a deposit of 1 btc, then we will have a user account with an available balance of 1 and a bitcoin address in our control with 1 bitcoin on it. this bitcoin address should be listed in the "balance sheet" table and show a balance of 1. On our stats page, we should be displaying "in game balances" of 1 and total of 1. So we are in balance.
now let's say a second user comes to the site, deposits 1 btc and then invests this in the bankroll investment. the first user plays the game and loses 0.5 btc.
the stats page should show "in game balances" of 0.5 btc, the "bankroll" investment should have a "book value" of 1 btc and a "current value" of 1.5 btc. back on the admin page, our "balance sheet" should show 2 addresses with 1 bitcoin each on them since we haven't moved the bitcoins anywhere (and since two users each deposited 1 bitcoin each). The "total current value" on the stats page should read 2 btc (since 0.5 was lost by one user and is now part of the bankroll investment)
if time goes by and bankroll dividends are paid, rake is collected, affiliates are paid out, no matter what happens, we always need to have more bitcoins in our possession then we owe our users. i.e. the total current value listed on the stats page must always be equal to or less than the amount of bitcoins we have on all user addresses plus our cash register address and the amounts we have in cold storage.
Approach
This will require a number of updates to the client (browser) code, including:
also to note, maybe it would be better (easier) not to use blockcypher API to query the value of bitcoins on the associated addresses? maybe we could use another method, so as to avoid looking in the same place?
for the addresses that will end up being displayed, it will look similar to what is currently displayed, except instead, our "balance sheet" will show the current value of each bitcoin address according to the blockchain, and not by referencing our own database
I plan on clearing out the addresses on a regular basis (probably daily as required), so I'd like the balance sheet addresses not to show the addresses with zero balances.
Understood. I'll use an alternative to BlockCypher (perhaps https://www.blocktrail.com/tBTC or https://testnet.blockexplorer.com/), to retrieve the addresses and I'll make a note of where we can update this API reference should we wish to use a different one later.
Regarding 0 balances, I'll have to write an additional API function to make a note in the database to exclude them from future retrievals (for the admin page), otherwise we will not have a way to differentiate which addresses to retrieve and as the list grows, checking each in turn will take an increasingly long amount of time. (0 balances can exist for additional reasons besides a full withdrawal)
This will probably increase this task to the original 4 hour estimate as it'll probably affect other sections of the game server (when deposits and withdrawals are made).
The balance sheet update has been committed (https://github.com/CoinRoster/slotmachine/commit/f45358855e4065abae09d7c9095dcc22bd9400d9), has been pulled, and is currently active on the development server (http://myfruitgame.com/admin).
I ended up going with the blockchain.info API since Block Explorer's seems to be misreporting unconfirmed transactions (always 0). Nevertheless, reverting is simply a matter of uncommenting and commenting the desired API address in the admin page JavaScript client (browser) code:
... and if we ever want to support additional APIs we simply need to add a custom parser to the API return handler:
so the next thing to add to the balance sheet is other "hard coded" addresses to the asset list, such as the "cash register address" and other cold storage addresses that have bitcoins on them. at first glance it looks like we are in a deficit, the total amount showing on the stats page is more than the sum of the three addresses with bitcoins on them
Update has been made to the admin page as per our last discussion:
The update is available for review on the development server right now.
hey @Patrick-Bay the balance sheet looks good! wondering if the address in the asset table labelled "live wallet" is this the hot money wallet (connected from our system by way of blockcypher api) that I usually refer to as the "cash register address"?
I also noticed that the "total investment balance" in the liabilities table is the cost base of the investment, but it should actually show the market value of the investment, since the market value is what the users actually have and can withdraw, can you make this change?
also wondering if the buttons for "transfer all accounts funds" and "transfer all funds" for each addresses work? I haven't tested yet, since I don't want to push the buttons and mess up the data if the buttons are not ready to be used yet. just a reminder that if the "transfer target address" is the same address that is listed in another part of the balance sheet (say its the cold storage address) then I'd like the transfer to be reflected immediately as the transaction is made (even if it requires a browser refresh thats ok)
To answer your questions (in order):
I'll have another look at the total investment balance.
The transfer buttons should be fully functional. However, the balances in the list are not updated, only the transfer buttons are (I believe they show "transfer completed" or something similar).
I don't recall any special handling of the transfer target address but I will make this change. We won't be able to refresh the browser immediately as that can mess up any pending transfers but we can queue a refresh once all transfers are complete. Would this be acceptable? Alternatively, we can update the field when it's updated but this may not be the blockchain balance since we can't yet check it (since other transfers may still be pending). Basically our current options are: update immediately (probably correct but not verified with blockchain), update after all transfers (not immediate but correct).
regarding point 3, so if I push a transfer button, the transaction will be triggered, but how might the table change? will the address which I transferred from which now has a zero balance still show in the table after a browser refresh?
Currently the only thing you'll see is the transfer button change to "Transfer pending..." and then "Transfer Complete", but nothing else will update (so yes, the address will remain along with it previous balance). The admin page will remain this way until the page is refreshed.
That being said, we can zero out the balance immediately (just simply subtract in the browser), or we can attempt a balance re-check as part of the process (transfer -> re-check/update -> next account).
Please advise on what you would like to see here.
Thanks for chatting with me about this yesterday. To sum up, we will simply leave the page as-is when funds are transferred out and we will need to refresh the page to see updated balances (or to remove 0-balance addresses altogether).
The latest updates have been committed and pulled to the game server where they may be reviewed immediately.
One note on transfers: when testing extremely low transfer fees (e.g. 1 satoshi), I noticed that the transaction gets posted to BlockCypher's systems but does not appear to be reflected immediately elsewhere (e.g. blockchain.info or block explorer). I suspect that this is due to the fee which may be interpreted as an invalid transaction, or there may be some propagation delays. Nevertheless, this is something we should discuss as it can cause emptied addresses to re-appear in the list on the Admin page.
Currently we're applying only a 1 Satoshi miner's fee (BlockCypher rejects 0-fee transactions), but we can easily update this:
see explanation #41
@Patrick-Bay if necessary, can you provide details about how you will complete this along with an estimated time-frame?