|
|
@ -1088,9 +1088,11 @@ WalletService.prototype._getUtxos = function(coin, addresses, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) { |
|
|
|
WalletService.prototype._getUtxosForCurrentWallet = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var opts = opts || {}; |
|
|
|
|
|
|
|
function utxoKey(utxo) { |
|
|
|
return utxo.txid + '|' + utxo.vout |
|
|
|
}; |
|
|
@ -1100,14 +1102,19 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) { |
|
|
|
async.series([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self.getWallet({}, function(err, wallet) { |
|
|
|
coin = wallet.coin; |
|
|
|
return next(); |
|
|
|
}); |
|
|
|
if (opts.coin) { |
|
|
|
coin = opts.coin; |
|
|
|
next(); |
|
|
|
} else { |
|
|
|
self.getWallet({}, function(err, wallet) { |
|
|
|
coin = wallet.coin; |
|
|
|
return next(); |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
if (_.isArray(addresses)) { |
|
|
|
allAddresses = addresses; |
|
|
|
if (_.isArray(opts.addresses)) { |
|
|
|
allAddresses = opts.addresses; |
|
|
|
return next(); |
|
|
|
} |
|
|
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) { |
|
|
@ -1195,7 +1202,9 @@ WalletService.prototype.getUtxos = function(opts, cb) { |
|
|
|
return cb(new ClientError('Invalid coin')); |
|
|
|
|
|
|
|
if (_.isUndefined(opts.addresses)) { |
|
|
|
self._getUtxosForCurrentWallet(null, cb); |
|
|
|
self._getUtxosForCurrentWallet({ |
|
|
|
coin: opts.coin |
|
|
|
}, cb); |
|
|
|
} else { |
|
|
|
self._getUtxos(opts.coin, opts.addresses, cb); |
|
|
|
} |
|
|
@ -1215,10 +1224,15 @@ WalletService.prototype._totalizeUtxos = function(utxos) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
WalletService.prototype._getBalanceFromAddresses = function(addresses, cb) { |
|
|
|
WalletService.prototype._getBalanceFromAddresses = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self._getUtxosForCurrentWallet(addresses, function(err, utxos) { |
|
|
|
var opts = opts || {}; |
|
|
|
|
|
|
|
self._getUtxosForCurrentWallet({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: opts.addresses |
|
|
|
}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var balance = self._totalizeUtxos(utxos); |
|
|
@ -1248,7 +1262,10 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) { |
|
|
|
|
|
|
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) { |
|
|
|
if (err) return cb(err); |
|
|
|
self._getBalanceFromAddresses(addresses, function(err, balance) { |
|
|
|
self._getBalanceFromAddresses({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: addresses |
|
|
|
}, function(err, balance) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
// Update cache
|
|
|
@ -1305,6 +1322,7 @@ WalletService.prototype._getActiveAddresses = function(cb) { |
|
|
|
/** |
|
|
|
* Get wallet balance. |
|
|
|
* @param {Object} opts |
|
|
|
* @param {string} [opts.coin] - Override wallet coin (default wallet's coin). |
|
|
|
* @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance |
|
|
|
* @returns {Object} balance - Total amount & locked amount. |
|
|
|
*/ |
|
|
@ -1313,6 +1331,11 @@ WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
|
|
|
|
opts = opts || {}; |
|
|
|
|
|
|
|
if (opts.coin) { |
|
|
|
if (!Utils.checkValueInCollection(opts.coin, Constants.COINS)) |
|
|
|
return cb(new ClientError('Invalid coin')); |
|
|
|
} |
|
|
|
|
|
|
|
if (!opts.twoStep) |
|
|
|
return self._getBalanceOneStep(opts, cb); |
|
|
|
|
|
|
@ -1327,7 +1350,10 @@ WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
return self._getBalanceOneStep(opts, cb); |
|
|
|
} else { |
|
|
|
log.debug('Requesting partial balance for ' + activeAddresses.length + ' out of ' + nbAddresses + ' addresses'); |
|
|
|
self._getBalanceFromAddresses(activeAddresses, function(err, partialBalance) { |
|
|
|
self._getBalanceFromAddresses({ |
|
|
|
coin: opts.coin, |
|
|
|
addresses: activeAddresses |
|
|
|
}, function(err, partialBalance) { |
|
|
|
if (err) return cb(err); |
|
|
|
cb(null, partialBalance); |
|
|
|
setTimeout(function() { |
|
|
@ -1388,7 +1414,7 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) { |
|
|
|
return cb(new ClientError('Invalid fee per KB')); |
|
|
|
} |
|
|
|
|
|
|
|
self._getUtxosForCurrentWallet(null, function(err, utxos) { |
|
|
|
self._getUtxosForCurrentWallet({}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var info = { |
|
|
@ -1768,7 +1794,7 @@ WalletService.prototype._selectTxInputs = function(txp, utxosToExclude, cb) { |
|
|
|
|
|
|
|
log.debug('Selecting inputs for a ' + Utils.formatAmountInBtc(txp.getTotalAmount()) + ' txp'); |
|
|
|
|
|
|
|
self._getUtxosForCurrentWallet(null, function(err, utxos) { |
|
|
|
self._getUtxosForCurrentWallet({}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var totalAmount; |
|
|
|