send all utxos support

This commit is contained in:
Craig Raw 2020-07-07 07:42:36 +02:00
parent 2ff9c94c62
commit 5d14be5c9c
2 changed files with 33 additions and 1 deletions

View file

@ -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<BlockTransactionHashIndex> presetUtxos;
public PresetUtxoSelector(Collection<BlockTransactionHashIndex> presetUtxos) {
this.presetUtxos = presetUtxos;
}
@Override
public Collection<BlockTransactionHashIndex> select(long targetValue, Collection<BlockTransactionHashIndex> candidates) {
List<BlockTransactionHashIndex> utxos = new ArrayList<>(presetUtxos);
utxos.retainAll(candidates);
return utxos;
}
public Collection<BlockTransactionHashIndex> getPresetUtxos() {
return presetUtxos;
}
}

View file

@ -248,7 +248,7 @@ public class Wallet {
}
}
public WalletTransaction createWalletTransaction(List<UtxoSelector> utxoSelectors, Address recipientAddress, long recipientAmount, double feeRate, Long fee) throws InsufficientFundsException {
public WalletTransaction createWalletTransaction(List<UtxoSelector> 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;