From 4be0937e5b6788e9150170ffea46be1ac92d51fe Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 15 May 2017 10:24:15 -0300 Subject: [PATCH] fix resulting amount below dust --- lib/server.js | 10 ++++++++-- test/integration/server.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index e227447..743893b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1339,9 +1339,15 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) { if (_.isEmpty(txp.inputs)) return cb(null, info); + var fee = txp.getEstimatedFee(); + var amount = _.sum(txp.inputs, 'satoshis') - fee; + + if (amount < Defaults.MIN_OUTPUT_AMOUNT) return cb(null, info); + info.size = txp.getEstimatedSize(); - info.fee = txp.getEstimatedFee(); - info.amount = _.sum(txp.inputs, 'satoshis') - info.fee; + info.fee = fee; + info.amount = amount; + if (opts.returnInputs) { info.inputs = _.shuffle(txp.inputs); } diff --git a/test/integration/server.js b/test/integration/server.js index 1f639b1..d6407a1 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -4433,6 +4433,35 @@ describe('Wallet service', function() { }); }); }); + it('should correctly get send max info when resulting amount is below dust', function(done) { + helpers.stubUtxos(server, wallet, [300e-6, 300e-6], function() { + server.getSendMaxInfo({ + feePerKb: 500e2, + returnInputs: true, + }, function(err, info) { + should.not.exist(err); + should.exist(info); + info.size.should.equal(700); + info.fee.should.equal(350e2); + info.amount.should.equal(250e2); + + var _min_output_amount = Defaults.MIN_OUTPUT_AMOUNT; + Defaults.MIN_OUTPUT_AMOUNT = 300e2; + server.getSendMaxInfo({ + feePerKb: 500e2, + returnInputs: true, + }, function(err, info) { + should.not.exist(err); + should.exist(info); + info.size.should.equal(0); + info.amount.should.equal(0); + Defaults.MIN_OUTPUT_AMOUNT = _min_output_amount; + done(); + }); + }); + }); + }); + describe('Fee level', function() { it('should correctly get send max info using feeLevel', function(done) { helpers.stubFeeLevels({