From 2ff9c94c62d1f20e408f89d7a41e2e66426b8634 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 5 Jul 2020 15:01:12 +0200 Subject: [PATCH] handle user set fee --- .../java/com/sparrowwallet/drongo/wallet/Wallet.java | 6 +++--- .../drongo/wallet/WalletTransaction.java | 12 ++++++++++++ 2 files changed, 15 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 f41d8ec..c2489e6 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) throws InsufficientFundsException { + public WalletTransaction createWalletTransaction(List utxoSelectors, Address recipientAddress, long recipientAmount, double feeRate, Long fee) throws InsufficientFundsException { long valueRequiredAmt = recipientAmount; while(true) { @@ -279,7 +279,7 @@ public class Wallet { //Add recipient output transaction.addOutput(recipientAmount, recipientAddress); int noChangeVSize = transaction.getVirtualSize(); - long noChangeFeeRequiredAmt = (long)(feeRate * noChangeVSize); + long noChangeFeeRequiredAmt = (fee == null ? (long)(feeRate * noChangeVSize) : fee); //Calculate what is left over from selected utxos after paying recipient long differenceAmt = totalSelectedAmt - recipientAmount; @@ -298,7 +298,7 @@ public class Wallet { if(changeAmt > dustThreshold) { //Change output is required, determine new fee once change output has been added int changeVSize = noChangeVSize + changeOutput.getLength(); - long changeFeeRequiredAmt = (long)(feeRate * changeVSize); + long changeFeeRequiredAmt = (fee == null ? (long)(feeRate * changeVSize) : fee); //Recalculate the change amount with the new fee changeAmt = differenceAmt - changeFeeRequiredAmt; diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java index 12356e1..f3a7e72 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletTransaction.java @@ -64,6 +64,10 @@ public class WalletTransaction { return changeNode; } + public Address getChangeAddress() { + return getWallet().getAddress(getChangeNode()); + } + public long getChangeAmount() { return changeAmount; } @@ -71,4 +75,12 @@ public class WalletTransaction { public long getFee() { return fee; } + + public long getTotal() { + return selectedUtxos.keySet().stream().mapToLong(BlockTransactionHashIndex::getValue).sum(); + } + + public double getFeePercentage() { + return (double)getFee() / getTotal(); + } }