fix wallet updates

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

View File

@ -209,14 +209,13 @@
<div class="home" data-ng-controller="HomeController">
<div ng-show='$root.wallet.publicKeyRing.isComplete()'>
<h3>Address</h3>
<div class="row">
<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 class="large-3 columns line-dashed-v text-center">
<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 class="large-1 columns"> </div>
<div class="large-2 columns">

View File

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

View File

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

View File

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

View File

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

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;
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
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<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) {
if (balance === parseInt(balance)) {
balance = balance;
}
else {
balance = balance.toFixed(8);
}
}
return cb(balance);
Object.keys(balanceByAddr).forEach(function(a) {
balanceByAddr[a] = balanceByAddr[a]/COIN;
});
return cb(balance / COIN, balanceByAddr);
});
};
Wallet.prototype.listUnspent = function(addrs, cb) {
this.blockchain.listUnspent(addrs, function(utxos) {
return cb(utxos);
Wallet.prototype.getUnspent = function(cb) {
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
return cb(unspentList);
});
};
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
var self = this;
if (typeof opts === 'function') {
@ -374,9 +374,9 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()};
}
self.listUnspent(self.getAddressesStr(), function(utxos) {
self.getUnspent(function(unspentList) {
// TODO check enough funds, etc.
self.createTxSync(toAddress, amountSatStr, utxos, opts);
self.createTxSync(toAddress, amountSatStr, unspentList, opts);
self.sendTxProposals();
self.store();
return cb();
@ -426,11 +426,4 @@ Wallet.prototype.disconnect = function() {
this.network.disconnect();
};
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
;
module.exports = require('soop')(Wallet);

View File

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