electrum

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

commit 9f7e256e39176905715a0fe4018e151a8dedfccd
parent 1703e0036a78888f73d896ac94ee34e6230ae8b8
Author: ThomasV <thomasv@electrum.org>
Date:   Wed, 21 Feb 2018 11:52:40 +0100

cleanup get_full_history. fix #3939

Diffstat:
Mgui/qt/history_list.py | 4++--
Mlib/exchange_rate.py | 4----
Mlib/util.py | 5+----
Mlib/wallet.py | 60++++++++++++++++++++++++++++++------------------------------
4 files changed, 33 insertions(+), 40 deletions(-)

diff --git a/gui/qt/history_list.py b/gui/qt/history_list.py @@ -203,8 +203,8 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): self.transactions = r['transactions'] self.summary = r['summary'] if not self.years and self.start_timestamp is None and self.end_timestamp is None: - start_date = self.summary['start_date'] - end_date = self.summary['end_date'] + start_date = self.summary.get('start_date') + end_date = self.summary.get('end_date') if start_date and end_date: self.years = [str(i) for i in range(start_date.year, end_date.year + 1)] self.period_combo.insertItems(1, self.years) diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py @@ -108,8 +108,6 @@ class ExchangeBase(PrintError): return [] def historical_rate(self, ccy, d_t): - if d_t is None: - return 'NaN' return self.history.get(ccy, {}).get(d_t.strftime('%Y-%m-%d'), 'NaN') def get_currencies(self): @@ -520,8 +518,6 @@ class FxThread(ThreadJob): return "%s" % (self.ccy_amount_str(value, True)) def history_rate(self, d_t): - if d_t is None: - return Decimal('NaN') rate = self.exchange.historical_rate(self.ccy, d_t) # Frequently there is no rate for today, until tomorrow :) # Use spot quotes in that case diff --git a/lib/util.py b/lib/util.py @@ -416,10 +416,7 @@ def format_satoshis(x, is_diff=False, num_zeros = 0, decimal_point = 8, whitespa return result def timestamp_to_datetime(timestamp): - try: - return datetime.fromtimestamp(timestamp) - except: - return None + return datetime.fromtimestamp(timestamp) def format_time(timestamp): date = timestamp_to_datetime(timestamp) diff --git a/lib/wallet.py b/lib/wallet.py @@ -969,11 +969,6 @@ class Abstract_Wallet(PrintError): def get_full_history(self, domain=None, from_timestamp=None, to_timestamp=None, fx=None, show_addresses=False): from .util import timestamp_to_datetime, Satoshis, Fiat out = [] - init_balance = None - init_timestamp = None - end_balance = None - end_timestamp = None - end_balance = 0 capital_gains = 0 fiat_income = 0 h = self.get_history(domain) @@ -990,11 +985,6 @@ class Abstract_Wallet(PrintError): 'value': Satoshis(value), 'balance': Satoshis(balance) } - if init_balance is None: - init_balance = balance - value - init_timestamp = timestamp - end_balance = balance - end_timestamp = timestamp item['date'] = timestamp_to_datetime(timestamp) if timestamp is not None else None item['label'] = self.get_label(tx_hash) if show_addresses: @@ -1032,28 +1022,38 @@ class Abstract_Wallet(PrintError): if fiat_value is not None: fiat_income += fiat_value out.append(item) - result = {'transactions': out} - if from_timestamp is not None and to_timestamp is not None: - start_date = timestamp_to_datetime(from_timestamp) - end_date = timestamp_to_datetime(to_timestamp) + # add summary + if out: + start_balance = out[0]['balance'].value - out[0]['value'].value + end_balance = out[-1]['balance'].value + if from_timestamp is not None and to_timestamp is not None: + start_date = timestamp_to_datetime(from_timestamp) + end_date = timestamp_to_datetime(to_timestamp) + else: + start_date = out[0]['date'] + end_date = out[-1]['date'] + + summary = { + 'start_date': start_date, + 'end_date': end_date, + 'start_balance': Satoshis(start_balance), + 'end_balance': Satoshis(end_balance) + } + if fx: + unrealized = self.unrealized_gains(domain, fx.timestamp_rate, fx.ccy) + summary['capital_gains'] = Fiat(capital_gains, fx.ccy) + summary['fiat_income'] = Fiat(fiat_income, fx.ccy) + summary['unrealized_gains'] = Fiat(unrealized, fx.ccy) + if start_date: + summary['start_fiat_balance'] = Fiat(fx.historical_value(start_balance, start_date), fx.ccy) + if end_date: + summary['end_fiat_balance'] = Fiat(fx.historical_value(end_balance, end_date), fx.ccy) else: - start_date = timestamp_to_datetime(init_timestamp) - end_date = timestamp_to_datetime(end_timestamp) - summary = { - 'start_date': start_date, - 'end_date': end_date, - 'start_balance': Satoshis(init_balance), - 'end_balance': Satoshis(end_balance) + summary = {} + return { + 'transactions': out, + 'summary': summary } - result['summary'] = summary - if fx: - unrealized = self.unrealized_gains(domain, fx.timestamp_rate, fx.ccy) - summary['start_fiat_balance'] = Fiat(fx.historical_value(init_balance, start_date), fx.ccy) - summary['end_fiat_balance'] = Fiat(fx.historical_value(end_balance, end_date), fx.ccy) - summary['capital_gains'] = Fiat(capital_gains, fx.ccy) - summary['fiat_income'] = Fiat(fiat_income, fx.ccy) - summary['unrealized_gains'] = Fiat(unrealized, fx.ccy) - return result def get_label(self, tx_hash): label = self.labels.get(tx_hash, '')