From b2508b7195d844ff3535a454c5411a4ea2f9bf1e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 18 Apr 2014 13:20:35 -0300 Subject: [PATCH] fix wallet updates --- index.html | 5 +-- js/controllers/backup.js | 3 +- js/controllers/home.js | 44 +++++++++----------- js/controllers/peer.js | 3 +- js/controllers/send.js | 4 +- js/models/blockchain/Insight.js | 2 +- js/models/core/Wallet.js | 71 +++++++++++++++------------------ js/services/controllerUtils.js | 11 +++-- 8 files changed, 62 insertions(+), 81 deletions(-) diff --git a/index.html b/index.html index 39f50b272..90191c5c3 100644 --- a/index.html +++ b/index.html @@ -209,14 +209,13 @@

Address

-
-

{{addrBalance[addr]}} BTC

+

{{balanceByAddr[addr]}} BTC

diff --git a/js/controllers/backup.js b/js/controllers/backup.js index 1a46faa9f..bad72f097 100644 --- a/js/controllers/backup.js +++ b/js/controllers/backup.js @@ -6,8 +6,7 @@ angular.module('copay.backup').controller('BackupController', $location.path('signin'); } else { - var socket = Socket($scope); - socket.on('connect', controllerUtils.handleTransactionByAddress($scope)); + controllerUtils.handleTransactionByAddress($scope); } $scope.title = 'Backup'; diff --git a/js/controllers/home.js b/js/controllers/home.js index f16c71566..f17f92ac5 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -3,40 +3,34 @@ angular.module('copay.home').controller('HomeController', function($scope, $rootScope, $location, Socket, controllerUtils) { $scope.title = 'Home'; - $scope.oneAtATime = true; $scope.addrBalance = {}; - - var _getBalance = function() { - $scope.addrs.forEach(function(addr) { - $rootScope.wallet.getBalance([addr], function(balance) { - $scope.addrBalance[addr] = balance; - $scope.$digest(); - }); + + var w = $rootScope.wallet; + + var _updateBalance = function () { + w.getBalance(function (balance, balanceByAddr) { + $scope.balanceByAddr = balanceByAddr; + $scope.addrs = Object.keys(balanceByAddr); + $scope.selectedAddr = $scope.addrs[0]; + $scope.$digest(); }); + var socket = Socket($scope); + controllerUtils.handleTransactionByAddress($scope, _updateBalance); }; - if (!$rootScope.wallet || !$rootScope.wallet.id) { - $location.path('signin'); - } else { - $scope.addrs = $rootScope.wallet.getAddressesStr(true); - $scope.selectedAddr = $scope.addrs[0]; - - _getBalance(); - - var socket = Socket($scope); - socket.on('connect', controllerUtils.handleTransactionByAddress($scope)); - } - $scope.newAddr = function() { - var a = $rootScope.wallet.generateAddress().toString(); - $scope.addrs.push(a); - _getBalance(); - var socket = Socket($scope); - socket.on('connect', controllerUtils.handleTransactionByAddress($scope)); + var a = w.generateAddress().toString(); + _updateBalance(); }; $scope.selectAddr = function(addr) { $scope.selectedAddr = addr; }; + + + if (!$rootScope.wallet || !$rootScope.wallet.id) { + $location.path('signin'); + } + _updateBalance(); }); diff --git a/js/controllers/peer.js b/js/controllers/peer.js index a3098aca4..ad0537b4b 100644 --- a/js/controllers/peer.js +++ b/js/controllers/peer.js @@ -11,8 +11,7 @@ angular.module('copay.peer').controller('PeerController', $location.path('signin'); } else { - var socket = Socket($scope); - socket.on('connect', controllerUtils.handleTransactionByAddress($scope)); + controllerUtils.handleTransactionByAddress($scope); } }); diff --git a/js/controllers/send.js b/js/controllers/send.js index 8e28185bf..a808e3941 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -8,9 +8,7 @@ angular.module('copay.send').controller('SendController', $location.path('signin'); } else { - var socket = Socket($scope); - socket.on('connect', controllerUtils.handleTransactionByAddress($scope)); - + controllerUtils.handleTransactionByAddress($scope); } $scope.sendTest = function() { diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js index ab86fd6e9..f935e8777 100644 --- a/js/models/blockchain/Insight.js +++ b/js/models/blockchain/Insight.js @@ -29,7 +29,7 @@ function _asyncForEach(array, fn, callback) { } }; -Insight.prototype.listUnspent = function(addresses, cb) { +Insight.prototype.getUnspent = function(addresses, cb) { var self = this; if (!addresses || !addresses.length) return cb([]); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 277d31501..97c4f74b4 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -152,13 +152,17 @@ Wallet.prototype.netStart = function() { }); }; -Wallet.prototype.store = function() { +Wallet.prototype.store = function(isSync) { this.log('[Wallet.js.135:store:]'); //TODO var wallet = this.toObj(); this.storage.setFromObj(this.id, wallet); - this.log('[Wallet.js.146] EMIT REFRESH'); //TODO - this.emit('refresh'); + if (isSync) { + this.log('Wallet stored.'); //TODO + } else { + this.log('Wallet stored. REFRESH Emitted'); //TODO + this.emit('refresh'); + } }; @@ -222,7 +226,7 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) { Wallet.prototype.generateAddress = function() { var addr = this.publicKeyRing.generateAddress(); this.sendPublicKeyRing(); - this.store(); + this.store(true); return addr; }; @@ -272,13 +276,13 @@ Wallet.prototype.sign = function(ntxid) { this.blockchain.sendRawTransaction(txHex, function(txid) { this.log('[Wallet.js.235:txid:]',txid); //TODO if (txid) { - this.store(); + this.store(true); } }); } else { this.sendTxProposals(); - this.store(); + this.store(true); } } return ret; @@ -311,7 +315,6 @@ Wallet.prototype.getAddressesStr = function(onlyMain) { return ret; }; - Wallet.prototype.addressIsOwn = function(addrStr) { var addrList = this.getAddressesStr(); var l = addrList.length; @@ -326,38 +329,35 @@ Wallet.prototype.addressIsOwn = function(addrStr) { return ret; }; - -Wallet.prototype.getTotalBalance = function(cb) { - this.getBalance(this.getAddressesStr(), function(balance) { - return cb(balance); - }); -}; - -Wallet.prototype.getBalance = function(addrs, cb) { +Wallet.prototype.getBalance = function(cb) { var balance = 0; - this.listUnspent(addrs, function(utxos) { + var balanceByAddr = {}; + var COIN = bitcore.util.COIN; + + // Prefill balanceByAddr with main address + this.getAddressesStr(true).forEach(function(a){ + balanceByAddr[a]=0; + }); + this.getUnspent(function(utxos) { for(var i=0;i