mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #81 from cmgustavo/feature/refactory-get-balance
Feature/refactory get balance
This commit is contained in:
commit
8f964bf007
|
@ -9,8 +9,7 @@ angular.module('copay.home').controller('HomeController',
|
||||||
|
|
||||||
var _getBalance = function() {
|
var _getBalance = function() {
|
||||||
$scope.addrs.forEach(function(addr) {
|
$scope.addrs.forEach(function(addr) {
|
||||||
$rootScope.wallet.blockchain.listUnspent([addr], function(unspent) {
|
$rootScope.wallet.getBalance([addr], function(balance) {
|
||||||
var balance = $rootScope.wallet.blockchain.getBalance(unspent);
|
|
||||||
$scope.addrBalance[addr] = balance;
|
$scope.addrBalance[addr] = balance;
|
||||||
$scope.$digest();
|
$scope.$digest();
|
||||||
});
|
});
|
||||||
|
|
|
@ -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) {
|
Insight.prototype.listUnspent = function(addresses, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
|
|
@ -277,21 +277,34 @@ Wallet.prototype.getAddressesStr = function() {
|
||||||
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.blockchain.listUnspent(this.getAddressesStr(), function(unspent) {
|
this.listUnspent(addrs, function(utxos) {
|
||||||
for(var i=0;i<unspent.length; i++) {
|
for(var i=0;i<utxos.length; i++) {
|
||||||
balance = balance + unspent[i].amount;
|
balance = balance + utxos[i].amount;
|
||||||
}
|
}
|
||||||
if (balance) {
|
if (balance) {
|
||||||
balance = balance.toFixed(4);
|
if (balance === parseInt(balance)) {
|
||||||
|
balance = balance;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
balance = balance.toFixed(8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return cb(balance);
|
return cb(balance);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.listUnspent = function(cb) {
|
Wallet.prototype.listUnspent = function(addrs, cb) {
|
||||||
this.blockchain.listUnspent(this.getAddressesStr(), cb);
|
this.blockchain.listUnspent(addrs, function(utxos) {
|
||||||
|
return cb(utxos);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -311,7 +324,7 @@ 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(function(utxos) {
|
self.listUnspent(self.getAddressesStr(), function(utxos) {
|
||||||
// TODO check enough funds, etc.
|
// TODO check enough funds, etc.
|
||||||
self.createTxSync(toAddress, amountSatStr, utxos, opts);
|
self.createTxSync(toAddress, amountSatStr, utxos, opts);
|
||||||
self.store();
|
self.store();
|
||||||
|
|
|
@ -8,7 +8,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
||||||
$rootScope.wallet = w;
|
$rootScope.wallet = w;
|
||||||
|
|
||||||
// Initial getBalance
|
// Initial getBalance
|
||||||
$rootScope.wallet.getBalance(function(balance) {
|
$rootScope.wallet.getTotalBalance(function(balance) {
|
||||||
$rootScope.totalBalance = balance;
|
$rootScope.totalBalance = balance;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
});
|
});
|
||||||
|
@ -34,7 +34,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
||||||
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.getBalance(function(balance) {
|
$rootScope.wallet.getTotalBalance(function(balance) {
|
||||||
scope.$apply(function() {
|
scope.$apply(function() {
|
||||||
$rootScope.totalBalance = balance;
|
$rootScope.totalBalance = balance;
|
||||||
});
|
});
|
||||||
|
|
|
@ -61,12 +61,6 @@ describe('Insight model', function() {
|
||||||
done();
|
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) {
|
it.skip('should return txid', function (done) {
|
||||||
var w = new Insight();
|
var w = new Insight();
|
||||||
w.sendRawTransaction(rawtx, function(a) {
|
w.sendRawTransaction(rawtx, function(a) {
|
||||||
|
|
Loading…
Reference in New Issue