electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 5376d37c24b5d60f4e8e7423326e6a42a92e2b90
parent 32af83b7aede02f0b9bb3e8294ef2dc0481fb6de
Author: SomberNight <somber.night@protonmail.com>
Date:   Sun, 18 Nov 2018 16:46:07 +0100

history export: include tx fee

closes #3504

Diffstat:
Melectrum/address_synchronizer.py | 15++++++++++++++-
Melectrum/gui/qt/history_list.py | 17+++++++++++++++--
Melectrum/util.py | 2+-
Melectrum/wallet.py | 9+++++++--
4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py @@ -25,7 +25,7 @@ import threading import asyncio import itertools from collections import defaultdict -from typing import TYPE_CHECKING, Dict +from typing import TYPE_CHECKING, Dict, Optional from . import bitcoin from .bitcoin import COINBASE_MATURITY, TYPE_ADDRESS, TYPE_PUBKEY @@ -712,6 +712,19 @@ class AddressSynchronizer(PrintError): fee = None return is_relevant, is_mine, v, fee + def get_tx_fee(self, tx: Transaction) -> Optional[int]: + if not tx: + return None + if hasattr(tx, '_cached_fee'): + return tx._cached_fee + is_relevant, is_mine, v, fee = self.get_wallet_delta(tx) + if fee is None: + txid = tx.txid() + fee = self.tx_fees.get(txid) + if fee is not None: + tx._cached_fee = fee + return fee + def get_addr_io(self, address): with self.lock, self.transaction_lock: h = self.get_address_history(address) diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py @@ -26,6 +26,7 @@ import webbrowser import datetime from datetime import date +from typing import TYPE_CHECKING from electrum.address_synchronizer import TX_HEIGHT_LOCAL from electrum.i18n import _ @@ -33,6 +34,9 @@ from electrum.util import block_explorer_URL, profiler, print_error, TxMinedStat from .util import * +if TYPE_CHECKING: + from electrum.wallet import Abstract_Wallet + try: from electrum.plot import plot_history, NothingToPlotException except: @@ -216,7 +220,7 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): @profiler def on_update(self): - self.wallet = self.parent.wallet + self.wallet = self.parent.wallet # type: Abstract_Wallet fx = self.parent.fx r = self.wallet.get_full_history(domain=self.get_domain(), from_timestamp=self.start_timestamp, to_timestamp=self.end_timestamp, fx=fx) self.transactions = r['transactions'] @@ -435,12 +439,21 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): item['confirmations'], item['value'], item.get('fiat_value', ''), + item.get('fee', ''), + item.get('fiat_fee', ''), item['date']]) with open(file_name, "w+", encoding='utf-8') as f: if is_csv: import csv transaction = csv.writer(f, lineterminator='\n') - transaction.writerow(["transaction_hash", "label", "confirmations", "value", "fiat_value", "timestamp"]) + transaction.writerow(["transaction_hash", + "label", + "confirmations", + "value", + "fiat_value", + "fee", + "fiat_fee", + "timestamp"]) for line in lines: transaction.writerow(line) else: diff --git a/electrum/util.py b/electrum/util.py @@ -156,7 +156,7 @@ class Fiat(object): return 'Fiat(%s)'% self.__str__() def __str__(self): - if self.value.is_nan(): + if self.value is None or self.value.is_nan(): return _('No Data') else: return "{:.2f}".format(self.value) + ' ' + self.ccy diff --git a/electrum/wallet.py b/electrum/wallet.py @@ -396,6 +396,7 @@ class Abstract_Wallet(AddressSynchronizer): continue if to_timestamp and (timestamp or now) >= to_timestamp: continue + tx = self.transactions.get(tx_hash) item = { 'txid': tx_hash, 'height': tx_mined_status.height, @@ -406,8 +407,9 @@ class Abstract_Wallet(AddressSynchronizer): 'date': timestamp_to_datetime(timestamp), 'label': self.get_label(tx_hash), } + tx_fee = self.get_tx_fee(tx) + item['fee'] = Satoshis(tx_fee) if tx_fee is not None else None if show_addresses: - tx = self.transactions.get(tx_hash) item['inputs'] = list(map(lambda x: dict((k, x[k]) for k in ('prevout_hash', 'prevout_n')), tx.inputs())) item['outputs'] = list(map(lambda x:{'address':x.address, 'value':Satoshis(x.value)}, tx.get_outputs_for_UI())) @@ -423,8 +425,11 @@ class Abstract_Wallet(AddressSynchronizer): if fx and fx.is_enabled() and fx.get_history_config(): fiat_value = self.get_fiat_value(tx_hash, fx.ccy) fiat_default = fiat_value is None - fiat_value = fiat_value if fiat_value is not None else value / Decimal(COIN) * self.price_at_timestamp(tx_hash, fx.timestamp_rate) # + fiat_rate = self.price_at_timestamp(tx_hash, fx.timestamp_rate) + fiat_value = fiat_value if fiat_value is not None else value / Decimal(COIN) * fiat_rate + fiat_fee = tx_fee / Decimal(COIN) * fiat_rate if tx_fee is not None else None item['fiat_value'] = Fiat(fiat_value, fx.ccy) + item['fiat_fee'] = Fiat(fiat_fee, fx.ccy) if fiat_fee else None item['fiat_default'] = fiat_default if value < 0: acquisition_price = - value / Decimal(COIN) * self.average_price(tx_hash, fx.timestamp_rate, fx.ccy)