fix wallet updates

This commit is contained in:
Matias Alejo Garcia 2014-04-18 13:20:35 -03:00
commit b2508b7195
8 changed files with 60 additions and 79 deletions

View file

@ -209,14 +209,13 @@
<div class="home" data-ng-controller="HomeController"> <div class="home" data-ng-controller="HomeController">
<div ng-show='$root.wallet.publicKeyRing.isComplete()'> <div ng-show='$root.wallet.publicKeyRing.isComplete()'>
<h3>Address</h3> <h3>Address</h3>
<div class="row"> <div class="row">
<div class="large-6 columns"> <div class="large-6 columns">
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}} <span class="right">({{addrBalance[addr]}} BTC) &gt;</span></a> <a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}} <span class="right">{{balanceByAddr[addr] || 0}} BTC &gt;</span></a>
</div> </div>
<div class="large-3 columns line-dashed-v text-center"> <div class="large-3 columns line-dashed-v text-center">
<qrcode size="160" data="{{selectedAddr}}"></qrcode> <qrcode size="160" data="{{selectedAddr}}"></qrcode>
<p class="m10t" ng-repeat="addr in addrs" ng-if="selectedAddr==addr"> <strong> {{addrBalance[addr]}} BTC </strong> </p> <p class="m10t" ng-repeat="addr in addrs" ng-if="selectedAddr==addr"> <strong> {{balanceByAddr[addr]}} BTC </strong> </p>
</div> </div>
<div class="large-1 columns"> </div> <div class="large-1 columns"> </div>
<div class="large-2 columns"> <div class="large-2 columns">

View file

@ -6,8 +6,7 @@ angular.module('copay.backup').controller('BackupController',
$location.path('signin'); $location.path('signin');
} }
else { else {
var socket = Socket($scope); controllerUtils.handleTransactionByAddress($scope);
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
} }
$scope.title = 'Backup'; $scope.title = 'Backup';

View file

@ -3,40 +3,34 @@
angular.module('copay.home').controller('HomeController', angular.module('copay.home').controller('HomeController',
function($scope, $rootScope, $location, Socket, controllerUtils) { function($scope, $rootScope, $location, Socket, controllerUtils) {
$scope.title = 'Home'; $scope.title = 'Home';
$scope.oneAtATime = true; $scope.oneAtATime = true;
$scope.addrBalance = {}; $scope.addrBalance = {};
var _getBalance = function() { var w = $rootScope.wallet;
$scope.addrs.forEach(function(addr) {
$rootScope.wallet.getBalance([addr], function(balance) { var _updateBalance = function () {
$scope.addrBalance[addr] = balance; w.getBalance(function (balance, balanceByAddr) {
$scope.$digest(); $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() { $scope.newAddr = function() {
var a = $rootScope.wallet.generateAddress().toString(); var a = w.generateAddress().toString();
$scope.addrs.push(a); _updateBalance();
_getBalance();
var socket = Socket($scope);
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
}; };
$scope.selectAddr = function(addr) { $scope.selectAddr = function(addr) {
$scope.selectedAddr = addr; $scope.selectedAddr = addr;
}; };
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
_updateBalance();
}); });

View file

@ -11,8 +11,7 @@ angular.module('copay.peer').controller('PeerController',
$location.path('signin'); $location.path('signin');
} }
else { else {
var socket = Socket($scope); controllerUtils.handleTransactionByAddress($scope);
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
} }
}); });

View file

@ -8,9 +8,7 @@ angular.module('copay.send').controller('SendController',
$location.path('signin'); $location.path('signin');
} }
else { else {
var socket = Socket($scope); controllerUtils.handleTransactionByAddress($scope);
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
} }
$scope.sendTest = function() { $scope.sendTest = function() {

View file

@ -29,7 +29,7 @@ function _asyncForEach(array, fn, callback) {
} }
}; };
Insight.prototype.listUnspent = function(addresses, cb) { Insight.prototype.getUnspent = function(addresses, cb) {
var self = this; var self = this;
if (!addresses || !addresses.length) return cb([]); if (!addresses || !addresses.length) return cb([]);

View file

@ -152,13 +152,17 @@ Wallet.prototype.netStart = function() {
}); });
}; };
Wallet.prototype.store = function() { Wallet.prototype.store = function(isSync) {
this.log('[Wallet.js.135:store:]'); //TODO this.log('[Wallet.js.135:store:]'); //TODO
var wallet = this.toObj(); var wallet = this.toObj();
this.storage.setFromObj(this.id, wallet); this.storage.setFromObj(this.id, wallet);
this.log('[Wallet.js.146] EMIT REFRESH'); //TODO if (isSync) {
this.emit('refresh'); 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() { Wallet.prototype.generateAddress = function() {
var addr = this.publicKeyRing.generateAddress(); var addr = this.publicKeyRing.generateAddress();
this.sendPublicKeyRing(); this.sendPublicKeyRing();
this.store(); this.store(true);
return addr; return addr;
}; };
@ -272,13 +276,13 @@ Wallet.prototype.sign = function(ntxid) {
this.blockchain.sendRawTransaction(txHex, function(txid) { this.blockchain.sendRawTransaction(txHex, function(txid) {
this.log('[Wallet.js.235:txid:]',txid); //TODO this.log('[Wallet.js.235:txid:]',txid); //TODO
if (txid) { if (txid) {
this.store(); this.store(true);
} }
}); });
} }
else { else {
this.sendTxProposals(); this.sendTxProposals();
this.store(); this.store(true);
} }
} }
return ret; return ret;
@ -311,7 +315,6 @@ Wallet.prototype.getAddressesStr = function(onlyMain) {
return ret; return ret;
}; };
Wallet.prototype.addressIsOwn = function(addrStr) { Wallet.prototype.addressIsOwn = function(addrStr) {
var addrList = this.getAddressesStr(); var addrList = this.getAddressesStr();
var l = addrList.length; var l = addrList.length;
@ -326,38 +329,35 @@ Wallet.prototype.addressIsOwn = function(addrStr) {
return ret; return ret;
}; };
Wallet.prototype.getBalance = function(cb) {
Wallet.prototype.getTotalBalance = function(cb) {
this.getBalance(this.getAddressesStr(), function(balance) {
return cb(balance);
});
};
Wallet.prototype.getBalance = function(addrs, cb) {
var balance = 0; 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<utxos.length; i++) { for(var i=0;i<utxos.length; i++) {
balance = balance + utxos[i].amount; var u= utxos[i];
var amt = u.amount * COIN;
balance = balance + amt;
balanceByAddr[u.address] = (balanceByAddr[u.address]||0) + amt;
} }
if (balance) { Object.keys(balanceByAddr).forEach(function(a) {
if (balance === parseInt(balance)) { balanceByAddr[a] = balanceByAddr[a]/COIN;
balance = balance; });
} return cb(balance / COIN, balanceByAddr);
else {
balance = balance.toFixed(8);
}
}
return cb(balance);
}); });
}; };
Wallet.prototype.listUnspent = function(addrs, cb) { Wallet.prototype.getUnspent = function(cb) {
this.blockchain.listUnspent(addrs, function(utxos) { this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
return cb(utxos); return cb(unspentList);
}); });
}; };
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) { Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
var self = this; var self = this;
if (typeof opts === 'function') { if (typeof opts === 'function') {
@ -374,9 +374,9 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()}; opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()};
} }
self.listUnspent(self.getAddressesStr(), function(utxos) { self.getUnspent(function(unspentList) {
// TODO check enough funds, etc. // TODO check enough funds, etc.
self.createTxSync(toAddress, amountSatStr, utxos, opts); self.createTxSync(toAddress, amountSatStr, unspentList, opts);
self.sendTxProposals(); self.sendTxProposals();
self.store(); self.store();
return cb(); return cb();
@ -426,11 +426,4 @@ Wallet.prototype.disconnect = function() {
this.network.disconnect(); this.network.disconnect();
}; };
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
;
module.exports = require('soop')(Wallet); module.exports = require('soop')(Wallet);

View file

@ -32,35 +32,34 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
$location.path('peer'); $location.path('peer');
$rootScope.wallet = w; $rootScope.wallet = w;
// Initial getBalance $rootScope.wallet.getBalance(function(balance) {
$rootScope.wallet.getTotalBalance(function(balance) {
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.$digest(); $rootScope.$digest();
}); });
}); });
w.on('refresh', function() { w.on('refresh', function() {
console.log('[controllerUtils.js] Refreshing'); //TODO console.log('[controllerUtils.js] Refreshing'); //TODO
// Do not use $digest() here. $rootScope.$digest();
}); });
w.on('openError', root.onErrorDigest); w.on('openError', root.onErrorDigest);
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);
}; };
root.handleTransactionByAddress = function(scope) { root.handleTransactionByAddress = function(scope, cb) {
var socket = Socket(scope); var socket = Socket(scope);
var addrs = $rootScope.wallet.getAddressesStr(); var addrs = $rootScope.wallet.getAddressesStr();
socket.emit('subscribe', 'inv');
for(var i=0;i<addrs.length;i++) { for(var i=0;i<addrs.length;i++) {
socket.emit('subscribe', addrs[i]); socket.emit('subscribe', addrs[i]);
} }
addrs.forEach(function(addr) { addrs.forEach(function(addr) {
socket.on(addr, function(txid) { socket.on(addr, function(txid) {
console.log('Received!', txid); console.log('Received!', txid);
$rootScope.wallet.getTotalBalance(function(balance) { $rootScope.wallet.getBalance(function(balance) {
scope.$apply(function() { scope.$apply(function() {
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
}); });
console.log('New balance:', balance); console.log('New balance:', balance);
if (typeof cb === 'function') return cb();
}); });
}); });
}); });