diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 43da6fec..8c5cebad 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -2126,21 +2126,9 @@ TXDB.prototype.getBalance = function getBalance(account, callback) { var parts = layout.cc(key); var hash = parts[0]; var index = parts[1]; - var height = data.readUInt32LE(4, true); - var value = utils.read64N(data, 8); - - assert(data.length >= 16); - - balance.total += value; - - if (height === 0x7fffffff) - balance.unconfirmed += value; - else - balance.confirmed += value; - - key = hash + index; - - self.coinCache.set(key, data); + var ckey = hash + index; + balance.addRaw(data); + self.coinCache.set(ckey, data); } }, function(err) { if (err) @@ -2161,20 +2149,6 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) { var balance = new Balance(this.wallet); var key, coin; - function parse(data) { - var height = data.readUInt32LE(4, true); - var value = utils.read64N(data, 8); - - assert(data.length >= 16); - - balance.total += value; - - if (height === 0x7fffffff) - balance.unconfirmed += value; - else - balance.confirmed += value; - } - this.getCoinHashes(account, function(err, hashes) { if (err) return callback(err); @@ -2185,7 +2159,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) { if (coin) { try { - parse(coin); + balance.addRaw(coin); } catch (e) { return next(e); } @@ -2200,7 +2174,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) { return next(); try { - parse(data); + balance.addRaw(data); } catch (e) { return callback(e); } @@ -2316,6 +2290,20 @@ Balance.prototype.unconfirm = function unconfirm(value) { this.confirmed -= value; }; +Balance.prototype.addRaw = function addRaw(data) { + var height = data.readUInt32LE(4, true); + var value = utils.read64N(data, 8); + + assert(data.length >= 16); + + this.total += value; + + if (height === 0x7fffffff) + this.unconfirmed += value; + else + this.confirmed += value; +}; + Balance.prototype.toJSON = function toJSON() { return { wid: this.wid,