Browse Source

Merge pull request #692 from isocolsky/feat/cross-balance

Allow checking balance of a wallet on a different coin
feat/estimateFee-limit
Matias Alejo Garcia 7 years ago
committed by GitHub
parent
commit
c4f87ccb17
  1. 1
      lib/expressapp.js
  2. 54
      lib/server.js
  3. 14
      test/integration/server.js

1
lib/expressapp.js

@ -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);

54
lib/server.js

@ -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;

14
test/integration/server.js

@ -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() {

Loading…
Cancel
Save