URI: 
       tImprove wallet history tab: - use json-serializable types - add toolbar to history tab - add button to display time interval - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 826cf467d8638b0e1d17150815738ecb5ffc6c36
   DIR parent c9ffe8d48a489028c4bed9f83e1374c84759b082
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Thu, 15 Feb 2018 14:59:05 +0100
       
       Improve wallet history tab:
       - use json-serializable types
       - add toolbar to history tab
       - add button to display time interval
       
       Diffstat:
         M gui/qt/history_list.py              |     206 +++++++++++++++++++++++++++----
         M gui/qt/main_window.py               |      63 +------------------------------
         M lib/commands.py                     |       2 +-
         M lib/plot.py                         |      11 +++++------
         M lib/util.py                         |      36 +++++++++++++++++++++++++++++++
         M lib/wallet.py                       |      80 +++++++++++++++++++-------------
       
       6 files changed, 273 insertions(+), 125 deletions(-)
       ---
   DIR diff --git a/gui/qt/history_list.py b/gui/qt/history_list.py
       t@@ -24,6 +24,7 @@
        # SOFTWARE.
        
        import webbrowser
       +import datetime
        
        from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL
        from .util import *
       t@@ -31,6 +32,10 @@ from electrum.i18n import _
        from electrum.util import block_explorer_URL
        from electrum.util import timestamp_to_datetime, profiler
        
       +try:
       +    from electrum.plot import plot_history
       +except:
       +    plot_history = None
        
        # note: this list needs to be kept in sync with another in kivy
        TX_ICONS = [
       t@@ -56,6 +61,9 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                AcceptFileDragDrop.__init__(self, ".txn")
                self.refresh_headers()
                self.setColumnHidden(1, True)
       +        self.start_timestamp = None
       +        self.end_timestamp = None
       +        self.years = []
        
            def refresh_headers(self):
                headers = ['', '', _('Date'), _('Description'), _('Amount'), _('Balance')]
       t@@ -73,41 +81,154 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                '''Replaced in address_dialog.py'''
                return self.wallet.get_addresses()
        
       +    def on_combo(self, x):
       +        s = self.period_combo.itemText(x)
       +        if s == _('All'):
       +            self.start_timestamp = None
       +            self.end_timestamp = None
       +        elif s == _('Custom'):
       +            start_date = self.select_date()
       +        else:
       +            try:
       +                year = int(s)
       +            except:
       +                return
       +            start_date = datetime.datetime(year, 1, 1)
       +            end_date = datetime.datetime(year+1, 1, 1)
       +            self.start_timestamp = time.mktime(start_date.timetuple())
       +            self.end_timestamp = time.mktime(end_date.timetuple())
       +        self.update()
       +
       +    def get_list_header(self):
       +        self.period_combo = QComboBox()
       +        self.period_combo.addItems([_('All'), _('Custom')])
       +        self.period_combo.activated.connect(self.on_combo)
       +        self.summary_button = QPushButton(_('Summary'))
       +        self.summary_button.pressed.connect(self.show_summary)
       +        self.export_button = QPushButton(_('Export'))
       +        self.export_button.pressed.connect(self.export_history_dialog)
       +        self.plot_button = QPushButton(_('Plot'))
       +        self.plot_button.pressed.connect(self.plot_history_dialog)
       +        return self.period_combo, self.summary_button, self.export_button, self.plot_button
       +
       +    def select_date(self):
       +        h = self.summary
       +        d = WindowModalDialog(self, _("Custom dates"))
       +        d.setMinimumSize(600, 150)
       +        d.b = True
       +        d.start_date = None
       +        d.end_date = None
       +        vbox = QVBoxLayout()
       +        grid = QGridLayout()
       +        start_edit = QPushButton()
       +        def on_start():
       +            start_edit.setText('')
       +            d.b = True
       +            d.start_date = None
       +        start_edit.pressed.connect(on_start)
       +        def on_end():
       +            end_edit.setText('')
       +            d.b = False
       +            d.end_date = None
       +        end_edit = QPushButton()
       +        end_edit.pressed.connect(on_end)
       +        grid.addWidget(QLabel(_("Start date")), 0, 0)
       +        grid.addWidget(start_edit, 0, 1)
       +        grid.addWidget(QLabel(_("End date")), 1, 0)
       +        grid.addWidget(end_edit, 1, 1)
       +        def on_date(date):
       +            ts = time.mktime(date.toPyDate().timetuple())
       +            if d.b:
       +                d.start_date = ts
       +                start_edit.setText(date.toString())
       +            else:
       +                d.end_date = ts
       +                end_edit.setText(date.toString())
       +        cal = QCalendarWidget()
       +        cal.setGridVisible(True)
       +        cal.clicked[QDate].connect(on_date)
       +        vbox.addLayout(grid)
       +        vbox.addWidget(cal)
       +        vbox.addLayout(Buttons(OkButton(d), CancelButton(d)))
       +        d.setLayout(vbox)
       +        if d.exec_():
       +            self.start_timestamp = d.start_date
       +            self.end_timestamp = d.end_date
       +            self.update()
       +
       +    def show_summary(self):
       +        h = self.summary
       +        format_amount = lambda x: self.parent.format_amount(x) + ' '+ self.parent.base_unit()
       +        d = WindowModalDialog(self, _("Summary"))
       +        d.setMinimumSize(600, 150)
       +        vbox = QVBoxLayout()
       +        grid = QGridLayout()
       +        grid.addWidget(QLabel(_("Start")), 0, 0)
       +        grid.addWidget(QLabel(h.get('start_date').isoformat(' ')), 0, 1)
       +        grid.addWidget(QLabel(_("End")), 1, 0)
       +        grid.addWidget(QLabel(h.get('end_date').isoformat(' ')), 1, 1)
       +        grid.addWidget(QLabel(_("Initial balance")), 2, 0)
       +        grid.addWidget(QLabel(format_amount(h['start_balance'].value)), 2, 1)
       +        grid.addWidget(QLabel(str(h.get('start_fiat_balance'))), 2, 2)
       +        grid.addWidget(QLabel(_("Final balance")), 4, 0)
       +        grid.addWidget(QLabel(format_amount(h['end_balance'].value)), 4, 1)
       +        grid.addWidget(QLabel(str(h.get('end_fiat_balance'))), 4, 2)
       +        grid.addWidget(QLabel(_("Income")), 6, 0)
       +        grid.addWidget(QLabel(str(h.get('fiat_income'))), 6, 2)
       +        grid.addWidget(QLabel(_("Capital gains")), 7, 0)
       +        grid.addWidget(QLabel(str(h.get('capital_gains'))), 7, 2)
       +        grid.addWidget(QLabel(_("Unrealized gains")), 8, 0)
       +        grid.addWidget(QLabel(str(h.get('unrealized_gains', ''))), 8, 2)
       +        vbox.addLayout(grid)
       +        vbox.addLayout(Buttons(CloseButton(d)))
       +        d.setLayout(vbox)
       +        d.exec_()
       +
       +    def plot_history_dialog(self):
       +        if plot_history is None:
       +            return
       +        if len(self.transactions) > 0:
       +            plt = plot_history(self.transactions)
       +            plt.show()
       +
            @profiler
            def on_update(self):
                self.wallet = self.parent.wallet
       -        h = self.wallet.get_history(self.get_domain())
       +        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']
       +        self.summary = r['summary']
       +        if not self.years and self.start_timestamp is None and self.end_timestamp is None:
       +            self.years = [str(i) for i in range(self.summary['start_date'].year, self.summary['end_date'].year + 1)]
       +            self.period_combo.insertItems(1, self.years)
                item = self.currentItem()
                current_tx = item.data(0, Qt.UserRole) if item else None
                self.clear()
       -        fx = self.parent.fx
                if fx: fx.history_used_spot = False
       -        for h_item in h:
       -            tx_hash, height, conf, timestamp, value, balance = h_item
       +        for tx_item in self.transactions:
       +            tx_hash = tx_item['txid']
       +            height = tx_item['height']
       +            conf = tx_item['confirmations']
       +            timestamp = tx_item['timestamp']
       +            value = tx_item['value'].value
       +            balance = tx_item['balance'].value
       +            label = tx_item['label']
                    status, status_str = self.wallet.get_tx_status(tx_hash, height, conf, timestamp)
                    has_invoice = self.wallet.invoices.paid.get(tx_hash)
                    icon = QIcon(":icons/" + TX_ICONS[status])
                    v_str = self.parent.format_amount(value, True, whitespaces=True)
                    balance_str = self.parent.format_amount(balance, whitespaces=True)
       -            label = self.wallet.get_label(tx_hash)
                    entry = ['', tx_hash, status_str, label, v_str, balance_str]
                    fiat_value = None
                    if value is not None and fx and fx.show_history():
                        date = timestamp_to_datetime(time.time() if conf <= 0 else timestamp)
       -                fiat_value = self.wallet.get_fiat_value(tx_hash, fx.ccy)
       -                if not fiat_value:
       -                    fiat_value = fx.historical_value(value, date)
       -                    fiat_default = True
       -                else:
       -                    fiat_default = False
       +                fiat_value = tx_item['fiat_value'].value
                        value_str = fx.format_fiat(fiat_value)
                        entry.append(value_str)
                        # fixme: should use is_mine
                        if value < 0:
       -                    ap, lp = self.wallet.capital_gain(tx_hash, fx.timestamp_rate, fx.ccy)
       -                    cg = None if lp is None or ap is None else lp - ap
       -                    entry.append(fx.format_fiat(ap))
       -                    entry.append(fx.format_fiat(cg))
       +                    entry.append(fx.format_fiat(tx_item['acquisition_price'].value))
       +                    entry.append(fx.format_fiat(tx_item['capital_gain'].value))
                    item = QTreeWidgetItem(entry)
                    item.setIcon(0, icon)
                    item.setToolTip(0, str(conf) + " confirmation" + ("s" if conf != 1 else ""))
       t@@ -121,7 +242,7 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                    if value and value < 0:
                        item.setForeground(3, QBrush(QColor("#BC1E1E")))
                        item.setForeground(4, QBrush(QColor("#BC1E1E")))
       -            if fiat_value and not fiat_default:
       +            if fiat_value and not tx_item['fiat_default']:
                        item.setForeground(6, QBrush(QColor("#1E1EFF")))
                    if tx_hash:
                        item.setData(0, Qt.UserRole, tx_hash)
       t@@ -183,25 +304,19 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                else:
                    column_title = self.headerItem().text(column)
                    column_data = item.text(column)
       -
                tx_URL = block_explorer_URL(self.config, 'tx', tx_hash)
                height, conf, timestamp = self.wallet.get_tx_height(tx_hash)
                tx = self.wallet.transactions.get(tx_hash)
                is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
                is_unconfirmed = height <= 0
                pr_key = self.wallet.invoices.paid.get(tx_hash)
       -
                menu = QMenu()
       -
                if height == TX_HEIGHT_LOCAL:
                    menu.addAction(_("Remove"), lambda: self.remove_local_tx(tx_hash))
       -
                menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(column_data))
                for c in self.editable_columns:
                    menu.addAction(_("Edit {}").format(self.headerItem().text(c)), lambda: self.editItem(item, c))
       -
                menu.addAction(_("Details"), lambda: self.parent.show_transaction(tx))
       -
                if is_unconfirmed and tx:
                    rbf = is_mine and not tx.is_final()
                    if rbf:
       t@@ -219,13 +334,11 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
            def remove_local_tx(self, delete_tx):
                to_delete = {delete_tx}
                to_delete |= self.wallet.get_depending_transactions(delete_tx)
       -
                question = _("Are you sure you want to remove this transaction?")
                if len(to_delete) > 1:
                    question = _(
                        "Are you sure you want to remove this transaction and {} child transactions?".format(len(to_delete) - 1)
                    )
       -
                answer = QMessageBox.question(self.parent, _("Please confirm"), question, QMessageBox.Yes, QMessageBox.No)
                if answer == QMessageBox.No:
                    return
       t@@ -246,3 +359,48 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                        self.wallet.save_transactions(write=True)
                        # need to update at least: history_list, utxo_list, address_list
                        self.parent.need_update.set()
       +
       +    def export_history_dialog(self):
       +        d = WindowModalDialog(self, _('Export History'))
       +        d.setMinimumSize(400, 200)
       +        vbox = QVBoxLayout(d)
       +        defaultname = os.path.expanduser('~/electrum-history.csv')
       +        select_msg = _('Select file to export your wallet transactions to')
       +        hbox, filename_e, csv_button = filename_field(self, self.config, defaultname, select_msg)
       +        vbox.addLayout(hbox)
       +        vbox.addStretch(1)
       +        hbox = Buttons(CancelButton(d), OkButton(d, _('Export')))
       +        vbox.addLayout(hbox)
       +        #run_hook('export_history_dialog', self, hbox)
       +        self.update()
       +        if not d.exec_():
       +            return
       +        filename = filename_e.text()
       +        if not filename:
       +            return
       +        try:
       +            self.do_export_history(self.wallet, filename, csv_button.isChecked())
       +        except (IOError, os.error) as reason:
       +            export_error_label = _("Electrum was unable to produce a transaction export.")
       +            self.parent.show_critical(export_error_label + "\n" + str(reason), title=_("Unable to export history"))
       +            return
       +        self.parent.show_message(_("Your wallet history has been successfully exported."))
       +
       +    def do_export_history(self, wallet, fileName, is_csv):
       +        history = self.transactions
       +        lines = []
       +        for item in history:
       +            if is_csv:
       +                lines.append([item['txid'], item.get('label', ''), item['confirmations'], item['value'], item['date']])
       +            else:
       +                lines.append(item)
       +        with open(fileName, "w+") as f:
       +            if is_csv:
       +                import csv
       +                transaction = csv.writer(f, lineterminator='\n')
       +                transaction.writerow(["transaction_hash","label", "confirmations", "value", "timestamp"])
       +                for line in lines:
       +                    transaction.writerow(line)
       +            else:
       +                from electrum.util import json_encode
       +                f.write(json_encode(history))
   DIR diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
       t@@ -52,10 +52,6 @@ from electrum import Transaction
        from electrum import util, bitcoin, commands, coinchooser
        from electrum import paymentrequest
        from electrum.wallet import Multisig_Wallet
       -try:
       -    from electrum.plot import plot_history
       -except:
       -    plot_history = None
        
        from .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit
        from .qrcodewidget import QRCodeWidget, QRDialog
       t@@ -490,9 +486,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
                contacts_menu.addAction(_("Import"), lambda: self.contact_list.import_contacts())
                invoices_menu = wallet_menu.addMenu(_("Invoices"))
                invoices_menu.addAction(_("Import"), lambda: self.invoice_list.import_invoices())
       -        hist_menu = wallet_menu.addMenu(_("&History"))
       -        hist_menu.addAction("Plot", self.plot_history_dialog).setEnabled(plot_history is not None)
       -        hist_menu.addAction("Export", self.export_history_dialog)
        
                wallet_menu.addSeparator()
                wallet_menu.addAction(_("Find"), self.toggle_search).setShortcut(QKeySequence("Ctrl+F"))
       t@@ -755,7 +748,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
                from .history_list import HistoryList
                self.history_list = l = HistoryList(self)
                l.searchable_list = l
       -        return l
       +        return self.create_list_tab(l, l.get_list_header())
        
            def show_address(self, addr):
                from . import address_dialog
       t@@ -2458,60 +2451,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
                except (IOError, os.error) as reason:
                    self.show_critical(_("Electrum was unable to export your labels.") + "\n" + str(reason))
        
       -    def export_history_dialog(self):
       -        d = WindowModalDialog(self, _('Export History'))
       -        d.setMinimumSize(400, 200)
       -        vbox = QVBoxLayout(d)
       -        defaultname = os.path.expanduser('~/electrum-history.csv')
       -        select_msg = _('Select file to export your wallet transactions to')
       -        hbox, filename_e, csv_button = filename_field(self, self.config, defaultname, select_msg)
       -        vbox.addLayout(hbox)
       -        vbox.addStretch(1)
       -        hbox = Buttons(CancelButton(d), OkButton(d, _('Export')))
       -        vbox.addLayout(hbox)
       -        run_hook('export_history_dialog', self, hbox)
       -        self.update()
       -        if not d.exec_():
       -            return
       -        filename = filename_e.text()
       -        if not filename:
       -            return
       -        try:
       -            self.do_export_history(self.wallet, filename, csv_button.isChecked())
       -        except (IOError, os.error) as reason:
       -            export_error_label = _("Electrum was unable to produce a transaction export.")
       -            self.show_critical(export_error_label + "\n" + str(reason), title=_("Unable to export history"))
       -            return
       -        self.show_message(_("Your wallet history has been successfully exported."))
       -
       -    def plot_history_dialog(self):
       -        if plot_history is None:
       -            return
       -        wallet = self.wallet
       -        history = wallet.get_history()
       -        if len(history) > 0:
       -            plt = plot_history(self.wallet, history)
       -            plt.show()
       -
       -    def do_export_history(self, wallet, fileName, is_csv):
       -        history = wallet.export_history(fx=self.fx)
       -        lines = []
       -        for item in history:
       -            if is_csv:
       -                lines.append([item['txid'], item.get('label', ''), item['confirmations'], item['value'], item['date']])
       -            else:
       -                lines.append(item)
       -
       -        with open(fileName, "w+") as f:
       -            if is_csv:
       -                transaction = csv.writer(f, lineterminator='\n')
       -                transaction.writerow(["transaction_hash","label", "confirmations", "value", "timestamp"])
       -                for line in lines:
       -                    transaction.writerow(line)
       -            else:
       -                import json
       -                f.write(json.dumps(lines, indent=4))
       -
            def sweep_key_dialog(self):
                d = WindowModalDialog(self, title=_('Sweep private keys'))
                d.setMinimumSize(600, 300)
   DIR diff --git a/lib/commands.py b/lib/commands.py
       t@@ -453,7 +453,7 @@ class Commands:
                    from .exchange_rate import FxThread
                    fx = FxThread(self.config, None)
                    kwargs['fx'] = fx
       -        return self.wallet.export_history(**kwargs)
       +        return self.wallet.get_full_history(**kwargs)
        
            @command('w')
            def setlabel(self, key, label):
   DIR diff --git a/lib/plot.py b/lib/plot.py
       t@@ -14,17 +14,16 @@ from matplotlib.patches import Ellipse
        from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker
        
        
       -def plot_history(wallet, history):
       +def plot_history(history):
            hist_in = defaultdict(int)
            hist_out = defaultdict(int)
            for item in history:
       -        tx_hash, height, confirmations, timestamp, value, balance = item
       -        if not confirmations:
       +        if not item['confirmations']:
                    continue
       -        if timestamp is None:
       +        if item['timestamp'] is None:
                    continue
       -        value = value*1./COIN
       -        date = datetime.datetime.fromtimestamp(timestamp)
       +        value = item['value'].value/COIN
       +        date = item['date']
                datenum = int(md.date2num(datetime.date(date.year, date.month, 1)))
                if value > 0:
                    hist_in[datenum] += value
   DIR diff --git a/lib/util.py b/lib/util.py
       t@@ -77,11 +77,47 @@ class UserCancelled(Exception):
            '''An exception that is suppressed from the user'''
            pass
        
       +class Satoshis(object):
       +    def __new__(cls, value):
       +        self = super(Satoshis, cls).__new__(cls)
       +        self.value = value
       +        return self
       +
       +    def __repr__(self):
       +        return 'Satoshis(%d)'%self.value
       +
       +    def __str__(self):
       +        return format_satoshis(self.value) + " BTC"
       +
       +class Fiat(object):
       +    def __new__(cls, value, ccy):
       +        self = super(Fiat, cls).__new__(cls)
       +        self.ccy = ccy
       +        self.value = value
       +        return self
       +
       +    def __repr__(self):
       +        return 'Fiat(%s)'% self.__str__()
       +
       +    def __str__(self):
       +        if self.value is None:
       +            return _('No Data')
       +        else:
       +            return "{:.2f}".format(self.value) + ' ' + self.ccy
       +
        class MyEncoder(json.JSONEncoder):
            def default(self, obj):
                from .transaction import Transaction
                if isinstance(obj, Transaction):
                    return obj.as_dict()
       +        if isinstance(obj, Satoshis):
       +            return str(obj)
       +        if isinstance(obj, Fiat):
       +            return str(obj)
       +        if isinstance(obj, Decimal):
       +            return str(obj)
       +        if isinstance(obj, datetime):
       +            return obj.isoformat(' ')[:-3]
                return super(MyEncoder, self).default(obj)
        
        class PrintError(object):
   DIR diff --git a/lib/wallet.py b/lib/wallet.py
       t@@ -948,13 +948,14 @@ class Abstract_Wallet(PrintError):
                # return last balance
                return balance
        
       -    def export_history(self, domain=None, from_timestamp=None, to_timestamp=None, fx=None, show_addresses=False):
       -        from .util import format_time, format_satoshis, timestamp_to_datetime
       -        h = self.get_history(domain)
       +    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
       +        end_balance = 0
                capital_gains = 0
                fiat_income = 0
       +        h = self.get_history(domain)
                for tx_hash, height, conf, timestamp, value, balance in h:
                    if from_timestamp and timestamp < from_timestamp:
                        continue
       t@@ -965,17 +966,15 @@ class Abstract_Wallet(PrintError):
                        'height':height,
                        'confirmations':conf,
                        'timestamp':timestamp,
       -                'value': format_satoshis(value, True) if value is not None else '--',
       -                'balance': format_satoshis(balance)
       +                'value': Satoshis(value),
       +                'balance': Satoshis(balance)
                    }
                    if init_balance is None:
                        init_balance = balance - value
       +                init_timestamp = timestamp
                    end_balance = balance
       -            if item['height']>0:
       -                date_str = format_time(timestamp) if timestamp is not None else _("unverified")
       -            else:
       -                date_str = _("unconfirmed")
       -            item['date'] = date_str
       +            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:
                        tx = self.transactions.get(tx_hash)
       t@@ -997,36 +996,44 @@ class Abstract_Wallet(PrintError):
                        fiat_value = self.get_fiat_value(tx_hash, fx.ccy)
                        if fiat_value is None:
                            fiat_value = fx.historical_value(value, date)
       -                item['fiat_value'] = fx.format_fiat(fiat_value)
       +                    fiat_default = True
       +                else:
       +                    fiat_default = False
       +                item['fiat_value'] = Fiat(fiat_value, fx.ccy)
       +                item['fiat_default'] = fiat_default
                        if value < 0:
                            ap, lp = self.capital_gain(tx_hash, fx.timestamp_rate, fx.ccy)
                            cg = None if lp is None or ap is None else lp - ap
       -                    item['acquisition_price'] = fx.format_fiat(ap)
       -                    item['capital_gain'] = fx.format_fiat(cg)
       +                    item['acquisition_price'] = Fiat(ap, fx.ccy)
       +                    item['capital_gain'] = Fiat(cg, fx.ccy)
                            if cg is not None:
                                capital_gains += cg
                        else:
                            if fiat_value is not None:
                                fiat_income += fiat_value
                    out.append(item)
       -
       -        if from_timestamp and to_timestamp:
       -            summary = {
       -                'start_date': format_time(from_timestamp),
       -                'end_date': format_time(to_timestamp),
       -                'start_balance': format_satoshis(init_balance),
       -                'end_balance': format_satoshis(end_balance),
       -                'capital_gains': fx.format_fiat(capital_gains),
       -                'fiat_income': fx.format_fiat(fiat_income)
       -            }
       -            if fx:
       -                start_date = timestamp_to_datetime(from_timestamp)
       -                end_date = timestamp_to_datetime(to_timestamp)
       -                summary['start_fiat_balance'] = fx.format_fiat(fx.historical_value(init_balance, start_date))
       -                summary['end_fiat_balance'] = fx.format_fiat(fx.historical_value(end_balance, end_date))
       -            out.append(summary)
       -
       -        return out
       +        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)
       +        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)
       +        }
       +        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, '')
       t@@ -1662,6 +1669,16 @@ class Abstract_Wallet(PrintError):
                height, conf, timestamp = self.get_tx_height(txid)
                return price_func(timestamp)
        
       +    def unrealized_gains(self, domain, price_func, ccy):
       +        coins = self.get_utxos(domain)
       +        now = time.time()
       +        p = price_func(now)
       +        if p is None:
       +            return
       +        ap = sum(self.coin_price(coin, price_func, ccy, self.txin_value(coin)) for coin in coins)
       +        lp = sum([coin['value'] for coin in coins]) * p / Decimal(COIN)
       +        return None if ap is None or lp is None else lp - ap
       +
            def capital_gain(self, txid, price_func, ccy):
                """
                Difference between the fiat price of coins leaving the wallet because of transaction txid,
       t@@ -1683,7 +1700,6 @@ class Abstract_Wallet(PrintError):
                    acquisition_price = None
                return acquisition_price, liquidation_price
        
       -
            def average_price(self, tx, price_func, ccy):
                """ average price of the inputs of a transaction """
                input_value = sum(self.txin_value(txin) for txin in tx.inputs()) / Decimal(COIN)