add coin arg to getBalance

This commit is contained in:
Ivan Socolsky 2017-09-01 11:45:19 -03:00
parent 5dffb5f958
commit 87ba10de84
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
3 changed files with 50 additions and 14 deletions

View File

@ -422,6 +422,7 @@ ExpressApp.prototype.start = function(opts, cb) {
router.get('/v1/balance/', function(req, res) {
getServerWithAuth(req, res, function(server) {
var opts = {};
if (req.query.coin) opts.coin = req.query.coin;
if (req.query.twoStep == '1') opts.twoStep = true;
server.getBalance(opts, function(err, balance) {
if (err) return returnError(err, res, req);

View File

@ -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 = 'btc'] - Override wallet coin.
* @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance
* @returns {Object} balance - Total amount & locked amount.
*/
@ -1327,7 +1345,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 +1409,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 +1789,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;

View File

@ -1880,6 +1880,20 @@ describe('Wallet service', function() {
});
});
});
it('should get balance for a different coin', function(done) {
helpers.stubUtxos(server, wallet, 1, function() {
var spy = sinon.spy(server, '_getBlockchainExplorer');
server.getBalance({
coin: 'bch'
}, function(err, balance) {
should.not.exist(err);
should.exist(balance);
var args = spy.getCalls()[0].args;
args[0].should.equal('bch');
done();
});
});
});
});
describe('#getBalance 2 steps', function() {