Initial getBalance of all your addresses. Socket-io that listening if your address receives btc.

Known bug: if you add new the address you can to recall socket-io. Socket-io should be listening on headerController.
This commit is contained in:
Gustavo Cortez 2014-04-17 06:43:30 -03:00
parent 864156ed4f
commit eaa1bc37e0
7 changed files with 54 additions and 10 deletions

View File

@ -18,7 +18,8 @@
</figure>
<div class="text-right" ng-show="$root.wallet">
<h5>Company Funds</h5>
<p>4.324 BTC</p>
<p ng-show="totalBalance">{{totalBalance}} BTC</p>
<p ng-show="!totalBalance">0 BTC</p>
</div>
</div>

View File

@ -18,10 +18,12 @@ var config = {
verbose: 1,
},
blockchain: {
host: 'test.insight.is',
port: 80
// host: 'localhost',
// port: 3001
host: 'localhost',
port: 3001
},
socket: {
host: 'localhost',
port: 3001
},
verbose: 1,
};

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copay.header').controller('HeaderController',
function($scope, $rootScope, $location, walletFactory) {
function($scope, $rootScope, $location, walletFactory, Socket) {
$scope.menu = [{
'title': 'Home',
'icon': 'fi-home',
@ -24,7 +24,7 @@ angular.module('copay.header').controller('HeaderController',
'link': '#/backup'
}];
if (!$rootScope.peerId) {
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
@ -40,6 +40,8 @@ angular.module('copay.header').controller('HeaderController',
if (w) {
w.disconnect();
delete $rootScope['wallet'];
var socket = Socket($scope);
socket.removeAllListeners();
$location.path('signin');
}
};
@ -47,4 +49,5 @@ angular.module('copay.header').controller('HeaderController',
$scope.clearFlashMessage = function() {
$rootScope.flashMessage = {};
};
});

View File

@ -1,10 +1,31 @@
'use strict';
angular.module('copay.peer').controller('PeerController',
function($scope, $rootScope, $location, $routeParams) {
function($scope, $rootScope, $location, $routeParams, Socket) {
$scope.init = function() {
//Network.connect($rootScope.masterId);
};
var addrs = $rootScope.wallet.getAddressesStr();
var socket = Socket($scope);
socket.on('connect', function() {
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.getBalance(function(balance) {
$scope.$apply(function() {
$rootScope.totalBalance = balance;
});
console.log('New balance:', balance);
});
});
});
});
});

View File

@ -17,6 +17,14 @@ angular.module('copay.signin').controller('SigninController',
w.on('created', function() {
$location.path('peer');
$rootScope.wallet = w;
// Initial getBalance
$rootScope.wallet.getBalance(function(balance) {
$scope.$apply(function() {
$rootScope.totalBalance = balance;
});
});
$rootScope.$digest();
});
w.on('refresh', function() {

View File

@ -276,7 +276,15 @@ Wallet.prototype.getAddressesStr = function() {
return ret;
};
Wallet.prototype.getBalance = function(cb) {
var balance = 0;
this.blockchain.listUnspent(this.getAddressesStr(), function(unspent) {
for(var i=0;i<unspent.length; i++) {
balance = balance + unspent[i].amount;
}
return cb(balance);
});
};
Wallet.prototype.listUnspent = function(cb) {
this.blockchain.listUnspent(this.getAddressesStr(), cb);

View File

@ -52,7 +52,8 @@ ScopedSocket.prototype.emit = function(event, data, callback) {
angular.module('copay.socket').factory('Socket',
function($rootScope) {
var socket = io.connect('http://localhost:3001', {
var server = 'http://' + config.socket.host + ':' + config.socket.port;
var socket = io.connect(server, {
'reconnect': true,
'reconnection delay': 500,
});