Refactory get balance 's methods

This commit is contained in:
Gustavo Cortez 2014-04-17 16:42:27 -03:00
commit f55d243bed
5 changed files with 24 additions and 26 deletions

View file

@ -9,8 +9,7 @@ angular.module('copay.home').controller('HomeController',
var _getBalance = function() {
$scope.addrs.forEach(function(addr) {
$rootScope.wallet.blockchain.listUnspent([addr], function(unspent) {
var balance = $rootScope.wallet.blockchain.getBalance(unspent);
$rootScope.wallet.getBalance([addr], function(balance) {
$scope.addrBalance[addr] = balance;
$scope.$digest();
});

View file

@ -29,17 +29,6 @@ function _asyncForEach(array, fn, callback) {
}
};
Insight.prototype.getBalance = function(unspent) {
var balance = 0;
for(var i=0;i<unspent.length; i++) {
balance = balance + unspent[i].amount;
}
if (balance) {
balance = balance.toFixed(4);
}
return balance;
};
Insight.prototype.listUnspent = function(addresses, cb) {
var self = this;

View file

@ -277,21 +277,37 @@ Wallet.prototype.getAddressesStr = function() {
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;
this.blockchain.listUnspent(this.getAddressesStr(), function(unspent) {
this.listUnspent(addrs, function(unspent) {
for(var i=0;i<unspent.length; i++) {
balance = balance + unspent[i].amount;
}
if (balance) {
balance = balance.toFixed(4);
if (balance === parseInt(balance)) {
balance = balance;
}
else {
balance = balance.toFixed(8);
}
}
return cb(balance);
});
};
Wallet.prototype.listUnspent = function(cb) {
this.blockchain.listUnspent(this.getAddressesStr(), cb);
Wallet.prototype.listUnspent = function(addrs, cb) {
if (!addrs) {
addrs = this.getAddressesStr();
}
this.blockchain.listUnspent(addrs, function(unspent) {
return cb(unspent);
});
};

View file

@ -8,7 +8,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
$rootScope.wallet = w;
// Initial getBalance
$rootScope.wallet.getBalance(function(balance) {
$rootScope.wallet.getTotalBalance(function(balance) {
$rootScope.totalBalance = balance;
$rootScope.$digest();
});
@ -34,7 +34,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
addrs.forEach(function(addr) {
socket.on(addr, function(txid) {
console.log('Received!', txid);
$rootScope.wallet.getBalance(function(balance) {
$rootScope.wallet.getTotalBalance(function(balance) {
scope.$apply(function() {
$rootScope.totalBalance = balance;
});

View file

@ -61,12 +61,6 @@ describe('Insight model', function() {
done();
});
});
it('should return balance', function () {
var w = new Insight();
var b = w.getBalance(unspent);
should.exist(b);
b.should.equal(91);
});
it.skip('should return txid', function (done) {
var w = new Insight();
w.sendRawTransaction(rawtx, function(a) {