implement balance by address
This commit is contained in:
parent
2c39d6a687
commit
6838f8d204
|
@ -441,6 +441,22 @@ WalletService.prototype.getBalance = function(opts, cb) {
|
||||||
return sum + utxo.satoshis;
|
return sum + utxo.satoshis;
|
||||||
}, 0));
|
}, 0));
|
||||||
|
|
||||||
|
// Compute balance by address
|
||||||
|
var byAddress = {};
|
||||||
|
_.each(_.indexBy(utxos, 'address'), function(value, key) {
|
||||||
|
byAddress[key] = {
|
||||||
|
address: key,
|
||||||
|
path: value.path,
|
||||||
|
amount: 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
_.each(utxos, function(utxo) {
|
||||||
|
byAddress[utxo.address].amount += utxo.satoshis;
|
||||||
|
});
|
||||||
|
|
||||||
|
balance.byAddress = _.values(byAddress);
|
||||||
|
|
||||||
return cb(null, balance);
|
return cb(null, balance);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -656,16 +656,22 @@ describe('Copay server', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get balance', function(done) {
|
it('should get balance', function(done) {
|
||||||
helpers.stubUtxos(server, wallet, 10, function() {
|
helpers.stubUtxos(server, wallet, [1, 2, 3], function() {
|
||||||
server.getBalance({}, function(err, balance) {
|
server.getBalance({}, function(err, balance) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(balance);
|
should.exist(balance);
|
||||||
balance.totalAmount.should.equal(helpers.toSatoshi(10));
|
balance.totalAmount.should.equal(helpers.toSatoshi(6));
|
||||||
balance.lockedAmount.should.equal(0);
|
balance.lockedAmount.should.equal(0);
|
||||||
should.exist(balance.byAddress);
|
should.exist(balance.byAddress);
|
||||||
balance.byAddress.length.should.equal(1);
|
balance.byAddress.length.should.equal(2);
|
||||||
balance.byAddress[0].amount.should.equal(helpers.toSatoshi(10));
|
balance.byAddress[0].amount.should.equal(helpers.toSatoshi(4));
|
||||||
done();
|
balance.byAddress[1].amount.should.equal(helpers.toSatoshi(2));
|
||||||
|
server.getMainAddresses({}, function(err, addresses) {
|
||||||
|
should.not.exist(err);
|
||||||
|
var addresses = _.uniq(_.pluck(addresses, 'address'));
|
||||||
|
_.intersection(addresses, _.pluck(balance.byAddress, 'address')).length.should.equal(2);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -681,6 +687,7 @@ describe('Copay server', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should get balance when there are no funds', function(done) {
|
it('should get balance when there are no funds', function(done) {
|
||||||
|
blockExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, null, []);
|
||||||
server.createAddress({}, function(err, address) {
|
server.createAddress({}, function(err, address) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
server.getBalance({}, function(err, balance) {
|
server.getBalance({}, function(err, balance) {
|
||||||
|
@ -689,7 +696,7 @@ describe('Copay server', function() {
|
||||||
balance.totalAmount.should.equal(0);
|
balance.totalAmount.should.equal(0);
|
||||||
balance.lockedAmount.should.equal(0);
|
balance.lockedAmount.should.equal(0);
|
||||||
should.exist(balance.byAddress);
|
should.exist(balance.byAddress);
|
||||||
balance.byAddress.length.should.equal(1);
|
balance.byAddress.length.should.equal(0);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue