From 1fa52f043ce2d1cca25c4fdb0bbe508e5a27e030 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 15 Dec 2022 17:04:01 +0200 Subject: [PATCH] add fee column to transactions csv for outgoing (spending) transactions --- .../wallet/TransactionsController.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java index 52d8dcc6..43507afa 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java @@ -3,6 +3,9 @@ package com.sparrowwallet.sparrow.wallet; import com.csvreader.CsvWriter; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.BitcoinUnit; +import com.sparrowwallet.drongo.protocol.TransactionInput; +import com.sparrowwallet.drongo.protocol.TransactionOutput; +import com.sparrowwallet.drongo.wallet.BlockTransaction; import com.sparrowwallet.sparrow.UnitFormat; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; @@ -122,13 +125,15 @@ public class TransactionsController extends WalletFormController implements Init if(file != null) { try(FileOutputStream outputStream = new FileOutputStream(file)) { CsvWriter writer = new CsvWriter(outputStream, ',', StandardCharsets.UTF_8); - writer.writeRecord(new String[] {"Date", "Label", "Value", "Balance", "Txid"}); + writer.writeRecord(new String[] {"Date", "Label", "Value", "Balance", "Fee", "Txid"}); for(Entry entry : walletTransactionsEntry.getChildren()) { TransactionEntry txEntry = (TransactionEntry)entry; writer.write(txEntry.getBlockTransaction().getDate() == null ? "Unconfirmed" : EntryCell.DATE_FORMAT.format(txEntry.getBlockTransaction().getDate())); writer.write(txEntry.getLabel()); writer.write(getCoinValue(txEntry.getValue())); writer.write(getCoinValue(txEntry.getBalance())); + Long fee = txEntry.getValue() < 0 ? getFee(txEntry.getBlockTransaction()) : null; + writer.write(fee == null ? "" : getCoinValue(fee)); writer.write(txEntry.getBlockTransaction().getHash().toString()); writer.endRecord(); } @@ -140,6 +145,28 @@ public class TransactionsController extends WalletFormController implements Init } } + private Long getFee(BlockTransaction blockTransaction) { + long fee = 0L; + for(TransactionInput txInput : blockTransaction.getTransaction().getInputs()) { + if(txInput.isCoinBase()) { + return 0L; + } + + BlockTransaction inputTx = getWalletForm().getWallet().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 String getCoinValue(Long value) { UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat(); return BitcoinUnit.BTC.equals(transactionsTable.getBitcoinUnit()) ? format.tableFormatBtcValue(value) : String.format(Locale.ENGLISH, "%d", value);