From 4692d4a034f187ce66397dd1c7e9c43e34b1f00e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 13 Nov 2014 11:56:15 -0800 Subject: [PATCH] fix getbalance. --- src/bitcoindjs.cc | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 8a96790b..b7057812 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -427,6 +427,9 @@ ListTransactions_V8(const CWalletTx& wtx, const string& strAccount, int nMinDepth, bool fLong, Local ret, const isminefilter& filter); +static int64_t +SatoshiFromAmount(const CAmount& amount); + extern "C" void init(Handle); @@ -3949,40 +3952,42 @@ NAN_METHOD(WalletGetBalance) { return NanThrowError("No account name provided."); } + isminefilter filter = ISMINE_SPENDABLE; + if (strAccount == "*") { // Calculate total balance a different way from GetBalance() // (GetBalance() sums up all unspent TxOuts) // getbalance and getbalance '*' 0 should return the same number - int64_t nBalance = 0; + CAmount nBalance = 0; for (map::iterator it = pwalletMain->mapWallet.begin(); - it != pwalletMain->mapWallet.end(); ++it) { + it != pwalletMain->mapWallet.end(); + ++it) { const CWalletTx& wtx = (*it).second; - if (!wtx.IsTrusted() || wtx.GetBlocksToMaturity() > 0) { continue; } - int64_t allFee; + CAmount allFee; string strSentAccount; - list > listReceived; - list > listSent; - // With v0.9.0 - // wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount); + list listReceived; + list listSent; + wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount, filter); if (wtx.GetDepthInMainChain() >= nMinDepth) { - BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listReceived) { - nBalance += r.second; + BOOST_FOREACH(const COutputEntry& r, listReceived) { + nBalance += r.amount; } } - BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listSent) { - nBalance -= r.second; + BOOST_FOREACH(const COutputEntry& s, listSent) { + nBalance -= s.amount; } nBalance -= allFee; } - NanReturnValue(NanNew(nBalance)); + + NanReturnValue(NanNew(SatoshiFromAmount(nBalance))); } - double nBalance = (double)GetAccountBalance(strAccount, nMinDepth, ISMINE_SPENDABLE); - NanReturnValue(NanNew((int64_t)(nBalance * 100000000))); + CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, filter); + NanReturnValue(NanNew(SatoshiFromAmount(nBalance))); } /** @@ -6065,6 +6070,14 @@ get_genesis_block(CBlock *genesis) { "0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")); } +static int64_t +SatoshiFromAmount(const CAmount& amount) { + // ./core.h : static const int64_t COIN = 100000000; + // ValueFromAmount: + // return (double)amount / (double)COIN; + return (int64_t)amount; +} + /** * Init() * Initialize the singleton object known as bitcoindjs.