From b0d37fe8fe6a4bec756e8c24bee562d1a377cf10 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 21 Apr 2021 09:53:53 +0200 Subject: [PATCH] include all utxos when sending max amount --- .../java/com/sparrowwallet/drongo/wallet/Wallet.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index a9911d0..2599dee 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -461,11 +461,12 @@ public class Wallet { } public WalletTransaction createWalletTransaction(List utxoSelectors, List utxoFilters, List payments, double feeRate, double longTermFeeRate, Long fee, Integer currentBlockHeight, boolean groupByAddress, boolean includeMempoolOutputs, boolean includeSpentMempoolOutputs) throws InsufficientFundsException { + boolean sendMax = payments.stream().anyMatch(Payment::isSendMax); long totalPaymentAmount = payments.stream().map(Payment::getAmount).mapToLong(v -> v).sum(); long valueRequiredAmt = totalPaymentAmount; while(true) { - Map selectedUtxos = selectInputs(utxoSelectors, utxoFilters, valueRequiredAmt, feeRate, longTermFeeRate, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs); + Map selectedUtxos = selectInputs(utxoSelectors, utxoFilters, valueRequiredAmt, feeRate, longTermFeeRate, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs, sendMax); long totalSelectedAmt = selectedUtxos.keySet().stream().mapToLong(BlockTransactionHashIndex::getValue).sum(); Transaction transaction = new Transaction(); @@ -501,7 +502,7 @@ public class Wallet { throw new InsufficientFundsException("Not enough combined value in selected UTXOs for fee of " + noChangeFeeRequiredAmt); } - Optional optMaxPayment = payments.stream().filter(payment -> payment.isSendMax()).findFirst(); + Optional optMaxPayment = payments.stream().filter(Payment::isSendMax).findFirst(); if(optMaxPayment.isPresent()) { Payment maxPayment = optMaxPayment.get(); maxSendAmt = maxSendAmt - payments.stream().filter(payment -> !maxPayment.equals(payment)).map(Payment::getAmount).mapToLong(v -> v).sum(); @@ -567,7 +568,7 @@ public class Wallet { } } - private Map selectInputs(List utxoSelectors, List utxoFilters, Long targetValue, double feeRate, double longTermFeeRate, boolean groupByAddress, boolean includeMempoolOutputs, boolean includeSpentMempoolOutputs) throws InsufficientFundsException { + private Map selectInputs(List utxoSelectors, List utxoFilters, Long targetValue, double feeRate, double longTermFeeRate, boolean groupByAddress, boolean includeMempoolOutputs, boolean includeSpentMempoolOutputs, boolean sendMax) throws InsufficientFundsException { List utxoPool = getGroupedUtxos(utxoFilters, feeRate, longTermFeeRate, groupByAddress, includeSpentMempoolOutputs); List filters = new ArrayList<>(); @@ -577,6 +578,10 @@ public class Wallet { filters.add(new OutputGroup.Filter(0, 0)); } + if(sendMax) { + Collections.reverse(filters); + } + for(OutputGroup.Filter filter : filters) { List filteredPool = utxoPool.stream().filter(filter::isEligible).collect(Collectors.toList());