From be8b56e355852fd14b45f6264425721e9a1be5ec Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 14 Apr 2025 16:27:12 +0200 Subject: [PATCH] fix inclusion of fees on wallet label exports --- .../sparrow/io/WalletLabels.java | 28 ++++++++++++++++--- .../sparrow/io/WalletTransactions.java | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/WalletLabels.java b/src/main/java/com/sparrowwallet/sparrow/io/WalletLabels.java index 94ab976a..1cb8050c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/WalletLabels.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/WalletLabels.java @@ -6,9 +6,7 @@ import com.sparrowwallet.drongo.KeyDerivation; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.OutputDescriptor; import com.sparrowwallet.drongo.Utils; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.protocol.Sha256Hash; -import com.sparrowwallet.drongo.protocol.Transaction; +import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; @@ -81,7 +79,7 @@ public class WalletLabels implements WalletImport, WalletExport { BlockTransaction blkTx = txEntry.getBlockTransaction(); labels.add(new TransactionLabel(blkTx.getHashAsString(), blkTx.getLabel(), origin, txEntry.isConfirming() ? null : blkTx.getHeight(), blkTx.getDate(), - blkTx.getFee() == null || blkTx.getFee() == 0 ? null : blkTx.getFee(), txEntry.getValue(), + getFee(walletTransactionsEntry.getWallet(), blkTx), txEntry.getValue(), getFiatValue(blkTx.getDate(), Transaction.SATOSHIS_PER_BITCOIN, fiatRates))); if(txEntry.isConfirming()) { confirmingTxs.add(blkTx.getHash()); @@ -346,6 +344,28 @@ public class WalletLabels implements WalletImport, WalletExport { return true; } + private Long getFee(Wallet wallet, BlockTransaction blockTransaction) { + long fee = 0L; + for(TransactionInput txInput : blockTransaction.getTransaction().getInputs()) { + if(txInput.isCoinBase()) { + return 0L; + } + + BlockTransaction inputTx = wallet.getWalletTransaction(txInput.getOutpoint().getHash()); + if(inputTx == null || inputTx.getTransaction().getOutputs().size() <= txInput.getOutpoint().getIndex()) { + return null; + } + TransactionOutput spentOutput = inputTx.getTransaction().getOutputs().get((int)txInput.getOutpoint().getIndex()); + fee += spentOutput.getValue(); + } + + for(TransactionOutput txOutput : blockTransaction.getTransaction().getOutputs()) { + fee -= txOutput.getValue(); + } + + return fee; + } + private Map getFiatRates(List walletForms) { ExchangeSource exchangeSource = getExchangeSource(); Currency fiatCurrency = getFiatCurrency(); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/WalletTransactions.java b/src/main/java/com/sparrowwallet/sparrow/io/WalletTransactions.java index 1afab356..81a99ed0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/WalletTransactions.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/WalletTransactions.java @@ -93,7 +93,7 @@ public class WalletTransactions implements WalletExport { writer.write(txEntry.getLabel()); writer.write(getCoinValue(bitcoinUnit, txEntry.getValue())); writer.write(getCoinValue(bitcoinUnit, txEntry.getBalance())); - Long fee = txEntry.getValue() < 0 ? getFee(wallet, txEntry.getBlockTransaction()) : null; + Long fee = getFee(wallet, txEntry.getBlockTransaction()); writer.write(fee == null ? "" : getCoinValue(bitcoinUnit, fee)); if(fiatCurrency != null) { Double fiatValue = getFiatValue(txEntry, fiatRates);