From 9a5daa5bf45d48010adbd9302c5423e473344e26 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 20 Jul 2015 12:45:12 -0300 Subject: [PATCH] add confirmed amounts to balance --- lib/server.js | 21 ++++++++++----------- test/integration/server.js | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/server.js b/lib/server.js index 0043088..5d216df 100644 --- a/lib/server.js +++ b/lib/server.js @@ -687,16 +687,15 @@ WalletService.prototype.getUtxos = function(cb) { }; WalletService.prototype._totalizeUtxos = function(utxos) { - var balance = {}; - balance.totalAmount = Utils.strip(_.reduce(utxos, function(sum, utxo) { - return sum + utxo.satoshis; - }, 0)); + var balance = { + totalAmount: _.sum(utxos, 'satoshis'), + lockedAmount: _.sum(_.filter(utxos, 'locked'), 'satoshis'), + totalConfirmedAmount: _.sum(_.filter(utxos, 'confirmations'), 'satoshis'), + lockedConfirmedAmount: _.sum(_.filter(_.filter(utxos, 'locked'), 'confirmed'), 'satoshis'), + }; + balance.availableAmount = balance.totalAmount - balance.lockedAmount; + balance.availableConfirmedAmount = balance.totalConfirmedAmount - balance.lockedConfirmedAmount; - balance.lockedAmount = Utils.strip(_.reduce(_.filter(utxos, { - locked: true - }), function(sum, utxo) { - return sum + utxo.satoshis; - }, 0)); return balance; }; @@ -755,7 +754,7 @@ WalletService.prototype.getBalance = function(opts, cb) { balance.byAddress = _.values(byAddress); - self._computeKbToSendMax(utxos, balance.totalAmount - balance.lockedAmount, function(err, sizeInKb) { + self._computeKbToSendMax(utxos, balance.availableAmount, function(err, sizeInKb) { if (err) { log.error('Could not compute fees needed to transfer max amount', err); } @@ -873,7 +872,7 @@ WalletService.prototype._selectTxInputs = function(txp, cb) { if (balance.totalAmount < txp.getTotalAmount()) return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds')); - if ((balance.totalAmount - balance.lockedAmount) < txp.amount) + if (balance.availableAmount < txp.amount) return cb(new ClientError('LOCKEDFUNDS', 'Funds are locked by pending transaction proposals')); utxos = _.reject(utxos, { diff --git a/test/integration/server.js b/test/integration/server.js index 687f76c..64ff730 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1363,13 +1363,19 @@ describe('Wallet service', function() { }); it('should get balance', function(done) { - helpers.stubUtxos(server, wallet, [1, 2, 3], function() { + helpers.stubUtxos(server, wallet, [1, 'u2', 3], function() { server.getBalance({}, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(6)); balance.lockedAmount.should.equal(0); + balance.availableAmount.should.equal(helpers.toSatoshi(6)); balance.totalKbToSendMax.should.equal(1); + + balance.totalConfirmedAmount.should.equal(helpers.toSatoshi(4)); + balance.lockedConfirmedAmount.should.equal(0); + balance.availableConfirmedAmount.should.equal(helpers.toSatoshi(4)); + should.exist(balance.byAddress); balance.byAddress.length.should.equal(2); balance.byAddress[0].amount.should.equal(helpers.toSatoshi(4)); @@ -1389,6 +1395,7 @@ describe('Wallet service', function() { should.exist(balance); balance.totalAmount.should.equal(0); balance.lockedAmount.should.equal(0); + balance.availableAmount.should.equal(0); balance.totalKbToSendMax.should.equal(0); should.exist(balance.byAddress); balance.byAddress.length.should.equal(0); @@ -1404,6 +1411,7 @@ describe('Wallet service', function() { should.exist(balance); balance.totalAmount.should.equal(0); balance.lockedAmount.should.equal(0); + balance.availableAmount.should.equal(0); balance.totalKbToSendMax.should.equal(0); should.exist(balance.byAddress); balance.byAddress.length.should.equal(0); @@ -1609,6 +1617,7 @@ describe('Wallet service', function() { balance.totalAmount.should.equal(helpers.toSatoshi(300)); balance.lockedAmount.should.equal(tx.inputs[0].satoshis); balance.lockedAmount.should.be.below(balance.totalAmount); + balance.availableAmount.should.equal(balance.totalAmount - balance.lockedAmount); server.storage.fetchAddresses(wallet.id, function(err, addresses) { should.not.exist(err); var change = _.filter(addresses, { @@ -1931,11 +1940,10 @@ describe('Wallet service', function() { server.getBalance({}, function(err, balance) { should.not.exist(err); balance.totalAmount.should.equal(helpers.toSatoshi(30.6)); - var amountInputs = _.reduce(_.pluck(txs[0].inputs, 'satoshis'), function(memo, satoshis) { - return memo + satoshis; - }, 0); + var amountInputs = _.sum(txs[0].inputs, 'satoshis'); balance.lockedAmount.should.equal(amountInputs); balance.lockedAmount.should.be.below(balance.totalAmount); + balance.availableAmount.should.equal(balance.totalAmount - balance.lockedAmount); done(); }); }); @@ -2030,6 +2038,7 @@ describe('Wallet service', function() { should.not.exist(err); balance.totalAmount.should.equal(helpers.toSatoshi(9)); balance.lockedAmount.should.equal(0); + balance.availableAmount.should.equal(helpers.toSatoshi(9)); balance.totalKbToSendMax.should.equal(3); var max = (balance.totalAmount - balance.lockedAmount) - (balance.totalKbToSendMax * 10000); var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', max / 1e8, null, TestData.copayers[0].privKey_1H_0); @@ -2056,6 +2065,7 @@ describe('Wallet service', function() { should.not.exist(err); balance.totalAmount.should.equal(helpers.toSatoshi(9)); balance.lockedAmount.should.equal(helpers.toSatoshi(4)); + balance.availableAmount.should.equal(helpers.toSatoshi(5)); balance.totalKbToSendMax.should.equal(2); var max = (balance.totalAmount - balance.lockedAmount) - (balance.totalKbToSendMax * 2000); var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', max / 1e8, null, TestData.copayers[0].privKey_1H_0, 2000);