add coin arg to getBalance
This commit is contained in:
parent
5dffb5f958
commit
87ba10de84
|
@ -422,6 +422,7 @@ ExpressApp.prototype.start = function(opts, cb) {
|
||||||
router.get('/v1/balance/', function(req, res) {
|
router.get('/v1/balance/', function(req, res) {
|
||||||
getServerWithAuth(req, res, function(server) {
|
getServerWithAuth(req, res, function(server) {
|
||||||
var opts = {};
|
var opts = {};
|
||||||
|
if (req.query.coin) opts.coin = req.query.coin;
|
||||||
if (req.query.twoStep == '1') opts.twoStep = true;
|
if (req.query.twoStep == '1') opts.twoStep = true;
|
||||||
server.getBalance(opts, function(err, balance) {
|
server.getBalance(opts, function(err, balance) {
|
||||||
if (err) return returnError(err, res, req);
|
if (err) return returnError(err, res, req);
|
||||||
|
|
|
@ -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 self = this;
|
||||||
|
|
||||||
|
var opts = opts || {};
|
||||||
|
|
||||||
function utxoKey(utxo) {
|
function utxoKey(utxo) {
|
||||||
return utxo.txid + '|' + utxo.vout
|
return utxo.txid + '|' + utxo.vout
|
||||||
};
|
};
|
||||||
|
@ -1100,14 +1102,19 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) {
|
||||||
async.series([
|
async.series([
|
||||||
|
|
||||||
function(next) {
|
function(next) {
|
||||||
|
if (opts.coin) {
|
||||||
|
coin = opts.coin;
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
self.getWallet({}, function(err, wallet) {
|
self.getWallet({}, function(err, wallet) {
|
||||||
coin = wallet.coin;
|
coin = wallet.coin;
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
if (_.isArray(addresses)) {
|
if (_.isArray(opts.addresses)) {
|
||||||
allAddresses = addresses;
|
allAddresses = opts.addresses;
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
||||||
|
@ -1195,7 +1202,9 @@ WalletService.prototype.getUtxos = function(opts, cb) {
|
||||||
return cb(new ClientError('Invalid coin'));
|
return cb(new ClientError('Invalid coin'));
|
||||||
|
|
||||||
if (_.isUndefined(opts.addresses)) {
|
if (_.isUndefined(opts.addresses)) {
|
||||||
self._getUtxosForCurrentWallet(null, cb);
|
self._getUtxosForCurrentWallet({
|
||||||
|
coin: opts.coin
|
||||||
|
}, cb);
|
||||||
} else {
|
} else {
|
||||||
self._getUtxos(opts.coin, opts.addresses, cb);
|
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;
|
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);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var balance = self._totalizeUtxos(utxos);
|
var balance = self._totalizeUtxos(utxos);
|
||||||
|
@ -1248,7 +1262,10 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) {
|
||||||
|
|
||||||
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
||||||
if (err) return cb(err);
|
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);
|
if (err) return cb(err);
|
||||||
|
|
||||||
// Update cache
|
// Update cache
|
||||||
|
@ -1305,6 +1322,7 @@ WalletService.prototype._getActiveAddresses = function(cb) {
|
||||||
/**
|
/**
|
||||||
* Get wallet balance.
|
* Get wallet balance.
|
||||||
* @param {Object} opts
|
* @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
|
* @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance
|
||||||
* @returns {Object} balance - Total amount & locked amount.
|
* @returns {Object} balance - Total amount & locked amount.
|
||||||
*/
|
*/
|
||||||
|
@ -1327,7 +1345,10 @@ WalletService.prototype.getBalance = function(opts, cb) {
|
||||||
return self._getBalanceOneStep(opts, cb);
|
return self._getBalanceOneStep(opts, cb);
|
||||||
} else {
|
} else {
|
||||||
log.debug('Requesting partial balance for ' + activeAddresses.length + ' out of ' + nbAddresses + ' addresses');
|
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);
|
if (err) return cb(err);
|
||||||
cb(null, partialBalance);
|
cb(null, partialBalance);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
@ -1388,7 +1409,7 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) {
|
||||||
return cb(new ClientError('Invalid fee per KB'));
|
return cb(new ClientError('Invalid fee per KB'));
|
||||||
}
|
}
|
||||||
|
|
||||||
self._getUtxosForCurrentWallet(null, function(err, utxos) {
|
self._getUtxosForCurrentWallet({}, function(err, utxos) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var info = {
|
var info = {
|
||||||
|
@ -1768,7 +1789,7 @@ WalletService.prototype._selectTxInputs = function(txp, utxosToExclude, cb) {
|
||||||
|
|
||||||
log.debug('Selecting inputs for a ' + Utils.formatAmountInBtc(txp.getTotalAmount()) + ' txp');
|
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);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var totalAmount;
|
var totalAmount;
|
||||||
|
|
|
@ -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() {
|
describe('#getBalance 2 steps', function() {
|
||||||
|
|
Loading…
Reference in New Issue