From 626e2a1b0649f131f13b98974478042b6467369a Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 20 Jul 2015 21:11:44 -0300 Subject: [PATCH] fix sum of locked confirmed utxos + tests --- lib/server.js | 6 +++--- test/integration/server.js | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/server.js b/lib/server.js index b6c97f9..a5070ae 100644 --- a/lib/server.js +++ b/lib/server.js @@ -683,7 +683,7 @@ WalletService.prototype._totalizeUtxos = function(utxos) { totalAmount: _.sum(utxos, 'satoshis'), lockedAmount: _.sum(_.filter(utxos, 'locked'), 'satoshis'), totalConfirmedAmount: _.sum(_.filter(utxos, 'confirmations'), 'satoshis'), - lockedConfirmedAmount: _.sum(_.filter(_.filter(utxos, 'locked'), 'confirmed'), 'satoshis'), + lockedConfirmedAmount: _.sum(_.filter(_.filter(utxos, 'locked'), 'confirmations'), 'satoshis'), }; balance.availableAmount = balance.totalAmount - balance.lockedAmount; balance.availableConfirmedAmount = balance.totalConfirmedAmount - balance.lockedConfirmedAmount; @@ -864,7 +864,7 @@ WalletService.prototype._selectTxInputs = function(txp, cb) { var balance = self._totalizeUtxos(utxos); if (txp.excludeUnconfirmedUtxos) { totalAmount = balance.totalConfirmedAmount; - availableAmount = balance.totalConfirmedAvailable; + availableAmount = balance.availableConfirmedAmount; } else { totalAmount = balance.totalAmount; availableAmount = balance.availableAmount; @@ -909,7 +909,7 @@ WalletService.prototype._selectTxInputs = function(txp, cb) { return cb(ex); } } - }; + } if (bitcoreError instanceof Bitcore.errors.Transaction.FeeError) { return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds for fee')); diff --git a/test/integration/server.js b/test/integration/server.js index 8c3248a..c0f1b9e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1671,7 +1671,7 @@ describe('Wallet service', function() { }); }); - it('should use confirmed utxos only if specified', function(done) { + it('should exclude unconfirmed utxos if specified', function(done) { helpers.stubUtxos(server, wallet, [1.3, 'u2', 'u0.1', 1.2], function(utxos) { var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 3, 'some message', TestData.copayers[0].privKey_1H_0); txOpts.excludeUnconfirmedUtxos = true; @@ -1691,6 +1691,30 @@ describe('Wallet service', function() { }); }); + it('should use non-locked confirmed utxos when specified', function(done) { + helpers.stubUtxos(server, wallet, [1.3, 'u2', 'u0.1', 1.2], function(utxos) { + var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 1.4, 'some message', TestData.copayers[0].privKey_1H_0); + txOpts.excludeUnconfirmedUtxos = true; + server.createTx(txOpts, function(err, tx) { + should.not.exist(err); + should.exist(tx); + tx.inputs.length.should.equal(2); + server.getBalance({}, function(err, balance) { + should.not.exist(err); + balance.lockedConfirmedAmount.should.equal(helpers.toSatoshi(2.5)); + balance.availableConfirmedAmount.should.equal(0); + var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.01, 'some message', TestData.copayers[0].privKey_1H_0); + txOpts.excludeUnconfirmedUtxos = true; + server.createTx(txOpts, function(err, tx) { + should.exist(err); + err.code.should.equal('LOCKEDFUNDS'); + done(); + }); + }); + }); + }); + }); + it('should fail gracefully if unable to reach the blockchain', function(done) { blockchainExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, 'dummy error'); server.createAddress({}, function(err, address) {