From 5d14be5c9c8cfaaa0fbf9d362b6609873dce38e0 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 7 Jul 2020 07:42:36 +0200 Subject: [PATCH] send all utxos support --- .../drongo/wallet/PresetUtxoSelector.java | 25 +++++++++++++++++++ .../sparrowwallet/drongo/wallet/Wallet.java | 9 ++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/sparrowwallet/drongo/wallet/PresetUtxoSelector.java diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/PresetUtxoSelector.java b/src/main/java/com/sparrowwallet/drongo/wallet/PresetUtxoSelector.java new file mode 100644 index 0000000..82895df --- /dev/null +++ b/src/main/java/com/sparrowwallet/drongo/wallet/PresetUtxoSelector.java @@ -0,0 +1,25 @@ +package com.sparrowwallet.drongo.wallet; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class PresetUtxoSelector implements UtxoSelector { + private final Collection presetUtxos; + + public PresetUtxoSelector(Collection presetUtxos) { + this.presetUtxos = presetUtxos; + } + + @Override + public Collection select(long targetValue, Collection candidates) { + List utxos = new ArrayList<>(presetUtxos); + utxos.retainAll(candidates); + + return utxos; + } + + public Collection getPresetUtxos() { + return presetUtxos; + } +} diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index c2489e6..0cd9037 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -248,7 +248,7 @@ public class Wallet { } } - public WalletTransaction createWalletTransaction(List utxoSelectors, Address recipientAddress, long recipientAmount, double feeRate, Long fee) throws InsufficientFundsException { + public WalletTransaction createWalletTransaction(List utxoSelectors, Address recipientAddress, long recipientAmount, double feeRate, Long fee, boolean sendAll) throws InsufficientFundsException { long valueRequiredAmt = recipientAmount; while(true) { @@ -281,6 +281,13 @@ public class Wallet { int noChangeVSize = transaction.getVirtualSize(); long noChangeFeeRequiredAmt = (fee == null ? (long)(feeRate * noChangeVSize) : fee); + //If sending all selected utxos, set the recipient amount to equal to total of those utxos less the no change fee + long maxSendAmt = totalSelectedAmt - noChangeFeeRequiredAmt; + if(sendAll && recipientAmount != maxSendAmt) { + recipientAmount = maxSendAmt; + continue; + } + //Calculate what is left over from selected utxos after paying recipient long differenceAmt = totalSelectedAmt - recipientAmount;