2 step getBalance

This commit is contained in:
Ivan Socolsky 2015-12-04 17:17:40 -03:00
parent 8c0882bf82
commit 3874d14f71
2 changed files with 112 additions and 11 deletions

View File

@ -832,7 +832,7 @@ WalletService.prototype._getBlockchainExplorer = function(network) {
return this.blockchainExplorer;
};
WalletService.prototype._getUtxosForAddresses = function(addresses, cb) {
WalletService.prototype._getUtxos = function(addresses, cb) {
var self = this;
if (addresses.length == 0) return cb(null, []);
@ -875,7 +875,7 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) {
if (addresses.length == 0) return next(null, []);
var addressStrs = _.pluck(addresses, 'address');
self._getUtxosForAddresses(addressStrs, function(err, utxos) {
self._getUtxos(addressStrs, function(err, utxos) {
if (err) return next(err);
if (utxos.length == 0) return next(null, []);
@ -918,7 +918,7 @@ WalletService.prototype.getUtxos = function(opts, cb) {
if (_.isUndefined(opts.addresses)) {
self._getUtxosForCurrentWallet(null, cb);
} else {
self._getUtxosForAddresses(opts.addresses, cb);
self._getUtxos(opts.addresses, cb);
}
};
@ -958,16 +958,12 @@ WalletService.prototype._computeBytesToSendMax = function(utxos, cb) {
});
};
/**
* Creates a new transaction proposal.
* @param {Object} opts
* @returns {Object} balance - Total amount & locked amount.
*/
WalletService.prototype.getBalance = function(opts, cb) {
WalletService.prototype._getBalanceFromAddresses = function(addresses, cb) {
var self = this;
self.getUtxos({}, function(err, utxos) {
self._getUtxosForCurrentWallet(addresses, function(err, utxos) {
if (err) return cb(err);
var balance = self._totalizeUtxos(utxos);
// Compute balance by address
@ -996,6 +992,59 @@ WalletService.prototype.getBalance = function(opts, cb) {
});
};
var prioritaryAddresses;
/**
* Get wallet balance.
* @param {Object} opts
* @returns {Object} balance - Total amount & locked amount.
*/
WalletService.prototype.getBalance = function(opts, cb) {
var self = this;
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err);
self._getBalanceFromAddresses(addresses, function(err, balance) {
if (err) return cb(err);
// Update cache
var addressIndex = _.indexBy(addresses, 'address');
prioritaryAddresses = _.map(_.pluck(balance.byAddress, 'address'), function(addrStr) {
return addressIndex[addrStr];
});
return cb(null, balance);
});
});
};
/**
* Get wallet balance.
* @param {Object} opts
* @returns {Object} balance - Total amount & locked amount.
*/
WalletService.prototype.getBalance2Steps = function(opts, cb) {
var self = this;
if (!prioritaryAddresses) return self.getBalance(opts, cb);
self._getBalanceFromAddresses(prioritaryAddresses, function(err, partialBalance) {
if (err) return cb(err);
cb(null, partialBalance);
setTimeout(function() {
self.getBalance(opts, function(err, fullBalance) {
if (err) return;
if (!_.isEqual(partialBalance, fullBalance)) {
console.log('*** [server.js ln1015] ACTUALIZAR BALANCE!!!!, partialBalance, fullBalance:', partialBalance, fullBalance); // TODO
}
});
}, 1);
return;
});
};
WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
var self = this;
@ -1110,7 +1159,7 @@ WalletService.prototype._selectTxInputs = function(txp, utxosToExclude, cb) {
return _.pluck(_.sortBy(list, 'order'), 'utxo');
};
self.getUtxos({}, function(err, utxos) {
self._getUtxosForCurrentWallet(null, function(err, utxos) {
if (err) return cb(err);
var excludeIndex = _.reduce(utxosToExclude, function(res, val) {

View File

@ -1504,6 +1504,58 @@ describe('Wallet service', function() {
});
});
describe('#getBalance 2 steps', function() {
var server, wallet;
beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, function(s, w) {
server = s;
wallet = w;
done();
});
});
it('should get balance', function(done) {
helpers.stubUtxos(server, wallet, [1, 'u2', 3], function() {
server.getBalance2Steps({}, function(err, balance) {
should.not.exist(err);
should.exist(balance);
balance.totalAmount.should.equal(helpers.toSatoshi(6));
balance.lockedAmount.should.equal(0);
balance.availableAmount.should.equal(helpers.toSatoshi(6));
balance.totalBytesToSendMax.should.equal(578);
balance.totalConfirmedAmount.should.equal(helpers.toSatoshi(4));
balance.lockedConfirmedAmount.should.equal(0);
balance.availableConfirmedAmount.should.equal(helpers.toSatoshi(4));
should.exist(balance.byAddress);
balance.byAddress.length.should.equal(2);
balance.byAddress[0].amount.should.equal(helpers.toSatoshi(4));
balance.byAddress[1].amount.should.equal(helpers.toSatoshi(2));
done();
});
});
});
it.only('should trigger notification when balance of non-prioritary addresses is updated', function(done) {
helpers.stubUtxos(server, wallet, [1, 2], function() {
server.getBalance2Steps({}, function(err, balance) {
should.not.exist(err);
should.exist(balance);
balance.totalAmount.should.equal(helpers.toSatoshi(3));
helpers.stubUtxos(server, wallet, [0.5, 0.6], function() {
server.getBalance2Steps({}, function(err, balance) {
should.not.exist(err);
should.exist(balance);
//balance.totalAmount.should.equal(helpers.toSatoshi(1.1));
//done();
});
});
});
});
});
});
describe('#getFeeLevels', function() {
var server, wallet;
beforeEach(function(done) {