enhacements on notifications and balance updates

This commit is contained in:
Matias Alejo Garcia 2014-10-15 12:09:10 -03:00
commit 95e8a22c54
7 changed files with 73 additions and 74 deletions

View file

@ -84,7 +84,8 @@ angular.module('copayApp.controllers').controller('MoreController',
$scope.deleteWallet = function() { $scope.deleteWallet = function() {
$rootScope.iden.deleteWallet(w.id, function() { $rootScope.iden.deleteWallet(w.id, function() {
controllerUtils.logout(); notification.info('Wallet deleted', $filter('translate')('wallet deleted'));
$location.path('/manage');
}); });
}; };

View file

@ -39,7 +39,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
if (w.isReady()) { if (w.isReady()) {
w.sendWalletReady(); w.sendWalletReady();
if ($rootScope.addrInfos.length > 0) { if ($rootScope.addrInfos.length > 0) {
controllerUtils.updateBalance(function() { controllerUtils.updateBalance(w, function() {
$rootScope.$digest(); $rootScope.$digest();
}); });
} }

View file

@ -294,13 +294,11 @@ Network.prototype.greet = function(copayerId, secretNumber) {
}; };
Network.prototype._addCopayerMap = function(peerId, copayerId) { Network.prototype._addCopayerMap = function(peerId, copayerId) {
console.log('[Async.js.296:_addCopayerMap:]',peerId, copayerId); //TODO
if (!this.copayerForPeer[peerId]) { if (!this.copayerForPeer[peerId]) {
if (Object.keys(this.copayerForPeer).length < this.maxPeers) { if (Object.keys(this.copayerForPeer).length < this.maxPeers) {
this.copayerForPeer[peerId] = copayerId; this.copayerForPeer[peerId] = copayerId;
} }
} }
console.log('[Async.js.296:_addCopayerMap:]',this.copayerForPeer); //TODO
}; };
Network.prototype._setInboundPeerAuth = function(peerId) { Network.prototype._setInboundPeerAuth = function(peerId) {
@ -376,7 +374,6 @@ Network.prototype.send = function(dest, payload, cb) {
dest = this.getCopayerIds(); dest = this.getCopayerIds();
payload.isBroadcast = 1; payload.isBroadcast = 1;
} }
console.log('[Async.js.374:dest:]',dest); //TODO
if (typeof dest === 'string') if (typeof dest === 'string')
dest = [dest]; dest = [dest];

View file

@ -200,9 +200,10 @@ Identity.open = function(email, password, opts, cb) {
var firstWallet; var firstWallet;
_.each(wids, function(wid) { _.each(wids, function(wid) {
iden.openWallet(wid, function(err, w) { iden.openWallet(wid, function(err, w) {
if (err) if (err) {
log.error('Cound not open wallet id:' + wid + '. Skipping') log.error('Cound not open wallet id:' + wid + '. Skipping')
else { iden.profile.deleteWallet(wid, function() {});
} else {
log.info('Open wallet id:' + wid + ' opened'); log.info('Open wallet id:' + wid + ' opened');
if (!firstWallet) if (!firstWallet)
firstWallet = w; firstWallet = w;

View file

@ -223,6 +223,8 @@ Insight.prototype.subscribe = function(addresses) {
s.emit('subscribe', address); s.emit('subscribe', address);
s.on(address, handler); s.on(address, handler);
} else {
log.debug('Already subcribed to: ', address);
} }
}); });
}; };

View file

@ -315,6 +315,7 @@ Wallet.prototype.seedCopayer = function(pubKey) {
Wallet.prototype._newAddresses = function(dontUpdateUx) { Wallet.prototype._newAddresses = function(dontUpdateUx) {
if (this.publicKeyRing.isComplete()) { if (this.publicKeyRing.isComplete()) {
this.subscribeToAddresses(); this.subscribeToAddresses();
}; };
this.emit('newAddresses', dontUpdateUx); this.emit('newAddresses', dontUpdateUx);
}; };
@ -380,7 +381,6 @@ Wallet.prototype._onPublicKeyRing = function(senderId, data) {
var inPKR = PublicKeyRing.fromObj(data.publicKeyRing); var inPKR = PublicKeyRing.fromObj(data.publicKeyRing);
var wasIncomplete = !this.publicKeyRing.isComplete(); var wasIncomplete = !this.publicKeyRing.isComplete();
console.log('[Wallet.js.382:wasIncomplete:]',wasIncomplete); //TODO
var hasChanged; var hasChanged;
try { try {
@ -390,7 +390,6 @@ console.log('[Wallet.js.382:wasIncomplete:]',wasIncomplete); //TODO
this.emit('connectionError', e.message); this.emit('connectionError', e.message);
return; return;
} }
console.log('[Wallet.js.393:hasChanged:]',hasChanged); //TODO
if (hasChanged) { if (hasChanged) {
if (wasIncomplete) { if (wasIncomplete) {
this.sendPublicKeyRing(); this.sendPublicKeyRing();
@ -881,12 +880,11 @@ Wallet.prototype._setBlockchainListeners = function() {
var self = this; var self = this;
self.blockchain.removeAllListeners(); self.blockchain.removeAllListeners();
log.debug('Setting Blockchain listeners for', this.getId());
self.blockchain.on('reconnect', function(attempts) { self.blockchain.on('reconnect', function(attempts) {
log.debug('Wallet:' + self.id + 'blockchain reconnect event'); log.debug('Wallet:' + self.id + 'blockchain reconnect event');
self.emit('insightReconnected'); self.emit('insightReconnected');
// Subscription should persist? TODO
//self.subscribeToAddresses();
}); });
self.blockchain.on('disconnect', function() { self.blockchain.on('disconnect', function() {
@ -894,7 +892,7 @@ Wallet.prototype._setBlockchainListeners = function() {
self.emit('insightError'); self.emit('insightError');
}); });
self.blockchain.on('tx', function(tx) { self.blockchain.on('tx', function(tx) {
log.debug('Wallet:' + self.id + 'blockchain tx event'); log.debug('Wallet:' + self.id + ' blockchain tx event');
var addresses = self.getAddressesInfo(); var addresses = self.getAddressesInfo();
var addr = _.findWhere(addresses, { var addr = _.findWhere(addresses, {
addressStr: tx.address addressStr: tx.address
@ -1305,7 +1303,7 @@ Wallet.prototype.sendIndexes = function(recipients) {
* @param {string[]} recipients - the pubkeys of the recipients * @param {string[]} recipients - the pubkeys of the recipients
*/ */
Wallet.prototype.sendAddressBook = function(recipients) { Wallet.prototype.sendAddressBook = function(recipients) {
if ( !Object.keys(this.addressBook).length ) return; if (!Object.keys(this.addressBook).length) return;
log.debug('Wallet:' + this.id + ' ### SENDING addressBook TO:', recipients || 'All', this.addressBook); log.debug('Wallet:' + this.id + ' ### SENDING addressBook TO:', recipients || 'All', this.addressBook);
this.send(recipients, { this.send(recipients, {
type: 'addressbook', type: 'addressbook',
@ -2221,6 +2219,7 @@ Wallet.prototype.getAddressesStr = function(opts) {
Wallet.prototype.subscribeToAddresses = function() { Wallet.prototype.subscribeToAddresses = function() {
var addrInfo = this.publicKeyRing.getAddressesInfo(); var addrInfo = this.publicKeyRing.getAddressesInfo();
this.blockchain.subscribe(_.pluck(addrInfo, 'addressStr')); this.blockchain.subscribe(_.pluck(addrInfo, 'addressStr'));
log.debug('Subscribed to ' + addrInfo.length + ' addresses'); //TODO
}; };
/** /**

View file

@ -11,7 +11,7 @@ angular.module('copayApp.services')
if (w) { if (w) {
if (!w.isReady()) { if (!w.isReady()) {
$location.path('/copayers'); $location.path('/copayers');
} }
} else { } else {
$location.path('/'); $location.path('/');
} }
@ -63,6 +63,16 @@ angular.module('copayApp.services')
return wid === $rootScope.wallet.getId(); return wid === $rootScope.wallet.getId();
}; };
root.updateTxsAndBalance = _.debounce(function(w) {
root.updateTxs({
wallet: w
});
root.updateBalance(w, function() {
$rootScope.$digest();
})
}, 3000);
root.installWalletHandlers = function($scope, w) { root.installWalletHandlers = function($scope, w) {
w.removeAllListeners(); w.removeAllListeners();
@ -94,41 +104,33 @@ angular.module('copayApp.services')
}); });
w.on('newAddresses', function(dontDigest) { w.on('newAddresses', function(dontDigest) {
if (root.isFocusedWallet(wid)) { root.updateTxsAndBalance(w);
root.updateAddressList(); if (!dontDigest) {
if (!dontDigest) { $rootScope.$digest();
$rootScope.$digest();
}
} }
}); });
w.on('tx', function(address, isChange) { w.on('tx', function(address, isChange) {
if (root.isFocusedWallet(wid)) { if (!isChange) {
if (!isChange) { notification.funds('Funds received on ' + w.getName(), address);
notification.funds('Funds received!', address);
}
root.updateBalance(function() {
$rootScope.$digest();
});
} }
root.updateBalance(w, function() {
$rootScope.$digest();
});
}); });
w.on('balanceUpdated', function() { w.on('balanceUpdated', function() {
if (root.isFocusedWallet(wid)) { root.updateBalance(w, function() {
root.updateBalance(function() { $rootScope.$digest();
$rootScope.$digest(); });
});
}
}); });
w.on('insightReconnected', function() { w.on('insightReconnected', function() {
if (root.isFocusedWallet(wid)) { $rootScope.reconnecting = false;
$rootScope.reconnecting = false; root.updateAddressList(w.getId());
root.updateAddressList(); root.updateBalance(w, function() {
root.updateBalance(function() { $rootScope.$digest();
$rootScope.$digest(); });
});
}
}); });
w.on('insightError', function() { w.on('insightError', function() {
@ -138,17 +140,8 @@ angular.module('copayApp.services')
} }
}); });
var updateTxsAndBalance = _.debounce(function() {
root.updateTxs();
root.updateBalance(function() {
$rootScope.$digest();
})
}, 3000);
w.on('txProposalsUpdated', function(dontDigest) { w.on('txProposalsUpdated', function(dontDigest) {
if (root.isFocusedWallet(wid)) { root.updateTxsAndBalance(w);
updateTxsAndBalance();
}
}); });
w.on('txProposalEvent', function(e) { w.on('txProposalEvent', function(e) {
@ -212,12 +205,12 @@ angular.module('copayApp.services')
}; };
root.setFocusedWallet = function(w) { root.setFocusedWallet = function(w) {
if (!_.isObject(w) ) if (!_.isObject(w))
w = $rootScope.iden.getOpenWallet(w); w = $rootScope.iden.getOpenWallet(w);
preconditions.checkState(w && _.isObject(w)); preconditions.checkState(w && _.isObject(w));
$rootScope.wallet = w; $rootScope.wallet = w;
root.updateAddressList(); root.updateTxsAndBalance();
root.redirIfLogged(); root.redirIfLogged();
}; };
@ -241,10 +234,11 @@ angular.module('copayApp.services')
} }
}; };
root.updateBalance = function(cb) { root.updateBalance = function(w, cb) {
var w = $rootScope.wallet; w = w || $rootScope.wallet;
if (!w) return root.onErrorDigest(); if (!w) return root.onErrorDigest();
if (!w.isReady()) return; if (!w.isReady()) return;
console.log('## Updating balance of:' + w.id)
$rootScope.balanceByAddr = {}; $rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true; $rootScope.updatingBalance = true;
@ -255,34 +249,39 @@ angular.module('copayApp.services')
var satToUnit = 1 / w.settings.unitToSatoshi; var satToUnit = 1 / w.settings.unitToSatoshi;
var COIN = bitcore.util.COIN; var COIN = bitcore.util.COIN;
$rootScope.totalBalance = balanceSat * satToUnit; if (root.isFocusedWallet(w.getId())) {
$rootScope.totalBalanceBTC = (balanceSat / COIN); $rootScope.totalBalance = balanceSat * satToUnit;
$rootScope.availableBalance = safeBalanceSat * satToUnit; $rootScope.totalBalanceBTC = (balanceSat / COIN);
$rootScope.availableBalanceBTC = (safeBalanceSat / COIN); $rootScope.availableBalance = safeBalanceSat * satToUnit;
$rootScope.availableBalanceBTC = (safeBalanceSat / COIN);
$rootScope.lockedBalance = (balanceSat - safeBalanceSat) * satToUnit; $rootScope.lockedBalance = (balanceSat - safeBalanceSat) * satToUnit;
$rootScope.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN; $rootScope.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN;
var balanceByAddr = {}; var balanceByAddr = {};
for (var ii in balanceByAddrSat) { for (var ii in balanceByAddrSat) {
balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit; balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit;
}
$rootScope.balanceByAddr = balanceByAddr;
root.updateAddressList();
$rootScope.updatingBalance = false;
rateService.whenAvailable(function() {
$rootScope.totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode);
$rootScope.alternativeIsoCode = w.settings.alternativeIsoCode;
$rootScope.lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode);
$rootScope.alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode);
return cb ? cb() : null;
});
} else {
// TODO
console.log('TODO: balance updated of a unfocused wallet');
} }
$rootScope.balanceByAddr = balanceByAddr;
root.updateAddressList();
$rootScope.updatingBalance = false;
rateService.whenAvailable(function() {
$rootScope.totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode);
$rootScope.alternativeIsoCode = w.settings.alternativeIsoCode;
$rootScope.lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode);
$rootScope.alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode);
return cb ? cb() : null;
});
}); });
}; };
root.updateTxs = function(opts) { root.updateTxs = function(opts) {
var w = $rootScope.wallet; var w = opts.wallet || $rootScope.wallet;
if (!w) return; if (!w) return;
opts = opts || $rootScope.txsOpts || {}; opts = opts || $rootScope.txsOpts || {};