|
|
@ -1241,44 +1241,44 @@ WalletService.prototype._totalizeUtxos = function(utxos) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
WalletService.prototype._getBalanceFromAddresses = function(opts, cb) { |
|
|
|
WalletService.prototype._getBalanceFromAddresses = function(opts, cb, i) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var opts = opts || {}; |
|
|
|
|
|
|
|
self._getUtxosForCurrentWallet({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: opts.addresses |
|
|
|
}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var balance = self._totalizeUtxos(utxos); |
|
|
|
// This lock is to prevent server starvation on big wallets
|
|
|
|
self._runLocked(cb, function(cb) { |
|
|
|
self._getUtxosForCurrentWallet({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: opts.addresses |
|
|
|
}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
// Compute balance by address
|
|
|
|
var byAddress = {}; |
|
|
|
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) { |
|
|
|
byAddress[key] = { |
|
|
|
address: key, |
|
|
|
path: value.path, |
|
|
|
amount: 0, |
|
|
|
}; |
|
|
|
}); |
|
|
|
var balance = self._totalizeUtxos(utxos); |
|
|
|
|
|
|
|
_.each(utxos, function(utxo) { |
|
|
|
byAddress[utxo.address].amount += utxo.satoshis; |
|
|
|
}); |
|
|
|
// Compute balance by address
|
|
|
|
var byAddress = {}; |
|
|
|
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) { |
|
|
|
byAddress[key] = { |
|
|
|
address: key, |
|
|
|
path: value.path, |
|
|
|
amount: 0, |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
balance.byAddress = _.values(byAddress); |
|
|
|
_.each(utxos, function(utxo) { |
|
|
|
byAddress[utxo.address].amount += utxo.satoshis; |
|
|
|
}); |
|
|
|
|
|
|
|
return cb(null, balance); |
|
|
|
balance.byAddress = _.values(byAddress); |
|
|
|
return cb(null, balance); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._getBalanceOneStep = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
// This lock is to prevent server starvation on big wallets
|
|
|
|
self._runLocked(cb, function(cb) { |
|
|
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) { |
|
|
|
if (err) return cb(err); |
|
|
|
self._getBalanceFromAddresses({ |
|
|
@ -1297,7 +1297,6 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -1356,7 +1355,8 @@ WalletService.prototype._checkAndUpdateAddressCount = function(twoStepCache, cb) |
|
|
|
* @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance |
|
|
|
* @returns {Object} balance - Total amount & locked amount. |
|
|
|
*/ |
|
|
|
WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
|
|
|
|
WalletService.prototype.getBalance = function(opts, cb, i) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
opts = opts || {}; |
|
|
@ -1388,6 +1388,7 @@ WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
return self._getBalanceOneStep(opts, cb); |
|
|
|
} else { |
|
|
|
log.debug('Requesting partial balance for ' + activeAddresses.length + ' addresses'); |
|
|
|
|
|
|
|
self._getBalanceFromAddresses({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: activeAddresses |
|
|
@ -1423,7 +1424,7 @@ WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
}); |
|
|
|
}, 1); |
|
|
|
return; |
|
|
|
}); |
|
|
|
}, i); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|