Improve wallet history tab:
- use json-serializable types - add toolbar to history tab - add button to display time interval
This commit is contained in:
parent
c9ffe8d48a
commit
826cf467d8
@ -24,6 +24,7 @@
|
|||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
import datetime
|
||||||
|
|
||||||
from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL
|
from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL
|
||||||
from .util import *
|
from .util import *
|
||||||
@ -31,6 +32,10 @@ from electrum.i18n import _
|
|||||||
from electrum.util import block_explorer_URL
|
from electrum.util import block_explorer_URL
|
||||||
from electrum.util import timestamp_to_datetime, profiler
|
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
|
# note: this list needs to be kept in sync with another in kivy
|
||||||
TX_ICONS = [
|
TX_ICONS = [
|
||||||
@ -56,6 +61,9 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
AcceptFileDragDrop.__init__(self, ".txn")
|
AcceptFileDragDrop.__init__(self, ".txn")
|
||||||
self.refresh_headers()
|
self.refresh_headers()
|
||||||
self.setColumnHidden(1, True)
|
self.setColumnHidden(1, True)
|
||||||
|
self.start_timestamp = None
|
||||||
|
self.end_timestamp = None
|
||||||
|
self.years = []
|
||||||
|
|
||||||
def refresh_headers(self):
|
def refresh_headers(self):
|
||||||
headers = ['', '', _('Date'), _('Description'), _('Amount'), _('Balance')]
|
headers = ['', '', _('Date'), _('Description'), _('Amount'), _('Balance')]
|
||||||
@ -73,41 +81,154 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
'''Replaced in address_dialog.py'''
|
'''Replaced in address_dialog.py'''
|
||||||
return self.wallet.get_addresses()
|
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
|
@profiler
|
||||||
def on_update(self):
|
def on_update(self):
|
||||||
self.wallet = self.parent.wallet
|
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()
|
item = self.currentItem()
|
||||||
current_tx = item.data(0, Qt.UserRole) if item else None
|
current_tx = item.data(0, Qt.UserRole) if item else None
|
||||||
self.clear()
|
self.clear()
|
||||||
fx = self.parent.fx
|
|
||||||
if fx: fx.history_used_spot = False
|
if fx: fx.history_used_spot = False
|
||||||
for h_item in h:
|
for tx_item in self.transactions:
|
||||||
tx_hash, height, conf, timestamp, value, balance = h_item
|
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)
|
status, status_str = self.wallet.get_tx_status(tx_hash, height, conf, timestamp)
|
||||||
has_invoice = self.wallet.invoices.paid.get(tx_hash)
|
has_invoice = self.wallet.invoices.paid.get(tx_hash)
|
||||||
icon = QIcon(":icons/" + TX_ICONS[status])
|
icon = QIcon(":icons/" + TX_ICONS[status])
|
||||||
v_str = self.parent.format_amount(value, True, whitespaces=True)
|
v_str = self.parent.format_amount(value, True, whitespaces=True)
|
||||||
balance_str = self.parent.format_amount(balance, 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]
|
entry = ['', tx_hash, status_str, label, v_str, balance_str]
|
||||||
fiat_value = None
|
fiat_value = None
|
||||||
if value is not None and fx and fx.show_history():
|
if value is not None and fx and fx.show_history():
|
||||||
date = timestamp_to_datetime(time.time() if conf <= 0 else timestamp)
|
date = timestamp_to_datetime(time.time() if conf <= 0 else timestamp)
|
||||||
fiat_value = self.wallet.get_fiat_value(tx_hash, fx.ccy)
|
fiat_value = tx_item['fiat_value'].value
|
||||||
if not fiat_value:
|
|
||||||
fiat_value = fx.historical_value(value, date)
|
|
||||||
fiat_default = True
|
|
||||||
else:
|
|
||||||
fiat_default = False
|
|
||||||
value_str = fx.format_fiat(fiat_value)
|
value_str = fx.format_fiat(fiat_value)
|
||||||
entry.append(value_str)
|
entry.append(value_str)
|
||||||
# fixme: should use is_mine
|
# fixme: should use is_mine
|
||||||
if value < 0:
|
if value < 0:
|
||||||
ap, lp = self.wallet.capital_gain(tx_hash, fx.timestamp_rate, fx.ccy)
|
entry.append(fx.format_fiat(tx_item['acquisition_price'].value))
|
||||||
cg = None if lp is None or ap is None else lp - ap
|
entry.append(fx.format_fiat(tx_item['capital_gain'].value))
|
||||||
entry.append(fx.format_fiat(ap))
|
|
||||||
entry.append(fx.format_fiat(cg))
|
|
||||||
item = QTreeWidgetItem(entry)
|
item = QTreeWidgetItem(entry)
|
||||||
item.setIcon(0, icon)
|
item.setIcon(0, icon)
|
||||||
item.setToolTip(0, str(conf) + " confirmation" + ("s" if conf != 1 else ""))
|
item.setToolTip(0, str(conf) + " confirmation" + ("s" if conf != 1 else ""))
|
||||||
@ -121,7 +242,7 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
if value and value < 0:
|
if value and value < 0:
|
||||||
item.setForeground(3, QBrush(QColor("#BC1E1E")))
|
item.setForeground(3, QBrush(QColor("#BC1E1E")))
|
||||||
item.setForeground(4, 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")))
|
item.setForeground(6, QBrush(QColor("#1E1EFF")))
|
||||||
if tx_hash:
|
if tx_hash:
|
||||||
item.setData(0, Qt.UserRole, tx_hash)
|
item.setData(0, Qt.UserRole, tx_hash)
|
||||||
@ -183,25 +304,19 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
else:
|
else:
|
||||||
column_title = self.headerItem().text(column)
|
column_title = self.headerItem().text(column)
|
||||||
column_data = item.text(column)
|
column_data = item.text(column)
|
||||||
|
|
||||||
tx_URL = block_explorer_URL(self.config, 'tx', tx_hash)
|
tx_URL = block_explorer_URL(self.config, 'tx', tx_hash)
|
||||||
height, conf, timestamp = self.wallet.get_tx_height(tx_hash)
|
height, conf, timestamp = self.wallet.get_tx_height(tx_hash)
|
||||||
tx = self.wallet.transactions.get(tx_hash)
|
tx = self.wallet.transactions.get(tx_hash)
|
||||||
is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
|
is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
|
||||||
is_unconfirmed = height <= 0
|
is_unconfirmed = height <= 0
|
||||||
pr_key = self.wallet.invoices.paid.get(tx_hash)
|
pr_key = self.wallet.invoices.paid.get(tx_hash)
|
||||||
|
|
||||||
menu = QMenu()
|
menu = QMenu()
|
||||||
|
|
||||||
if height == TX_HEIGHT_LOCAL:
|
if height == TX_HEIGHT_LOCAL:
|
||||||
menu.addAction(_("Remove"), lambda: self.remove_local_tx(tx_hash))
|
menu.addAction(_("Remove"), lambda: self.remove_local_tx(tx_hash))
|
||||||
|
|
||||||
menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(column_data))
|
menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(column_data))
|
||||||
for c in self.editable_columns:
|
for c in self.editable_columns:
|
||||||
menu.addAction(_("Edit {}").format(self.headerItem().text(c)), lambda: self.editItem(item, c))
|
menu.addAction(_("Edit {}").format(self.headerItem().text(c)), lambda: self.editItem(item, c))
|
||||||
|
|
||||||
menu.addAction(_("Details"), lambda: self.parent.show_transaction(tx))
|
menu.addAction(_("Details"), lambda: self.parent.show_transaction(tx))
|
||||||
|
|
||||||
if is_unconfirmed and tx:
|
if is_unconfirmed and tx:
|
||||||
rbf = is_mine and not tx.is_final()
|
rbf = is_mine and not tx.is_final()
|
||||||
if rbf:
|
if rbf:
|
||||||
@ -219,13 +334,11 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
def remove_local_tx(self, delete_tx):
|
def remove_local_tx(self, delete_tx):
|
||||||
to_delete = {delete_tx}
|
to_delete = {delete_tx}
|
||||||
to_delete |= self.wallet.get_depending_transactions(delete_tx)
|
to_delete |= self.wallet.get_depending_transactions(delete_tx)
|
||||||
|
|
||||||
question = _("Are you sure you want to remove this transaction?")
|
question = _("Are you sure you want to remove this transaction?")
|
||||||
if len(to_delete) > 1:
|
if len(to_delete) > 1:
|
||||||
question = _(
|
question = _(
|
||||||
"Are you sure you want to remove this transaction and {} child transactions?".format(len(to_delete) - 1)
|
"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)
|
answer = QMessageBox.question(self.parent, _("Please confirm"), question, QMessageBox.Yes, QMessageBox.No)
|
||||||
if answer == QMessageBox.No:
|
if answer == QMessageBox.No:
|
||||||
return
|
return
|
||||||
@ -246,3 +359,48 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
|
|||||||
self.wallet.save_transactions(write=True)
|
self.wallet.save_transactions(write=True)
|
||||||
# need to update at least: history_list, utxo_list, address_list
|
# need to update at least: history_list, utxo_list, address_list
|
||||||
self.parent.need_update.set()
|
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))
|
||||||
|
|||||||
@ -52,10 +52,6 @@ from electrum import Transaction
|
|||||||
from electrum import util, bitcoin, commands, coinchooser
|
from electrum import util, bitcoin, commands, coinchooser
|
||||||
from electrum import paymentrequest
|
from electrum import paymentrequest
|
||||||
from electrum.wallet import Multisig_Wallet
|
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 .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit
|
||||||
from .qrcodewidget import QRCodeWidget, QRDialog
|
from .qrcodewidget import QRCodeWidget, QRDialog
|
||||||
@ -490,9 +486,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
|||||||
contacts_menu.addAction(_("Import"), lambda: self.contact_list.import_contacts())
|
contacts_menu.addAction(_("Import"), lambda: self.contact_list.import_contacts())
|
||||||
invoices_menu = wallet_menu.addMenu(_("Invoices"))
|
invoices_menu = wallet_menu.addMenu(_("Invoices"))
|
||||||
invoices_menu.addAction(_("Import"), lambda: self.invoice_list.import_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.addSeparator()
|
||||||
wallet_menu.addAction(_("Find"), self.toggle_search).setShortcut(QKeySequence("Ctrl+F"))
|
wallet_menu.addAction(_("Find"), self.toggle_search).setShortcut(QKeySequence("Ctrl+F"))
|
||||||
@ -755,7 +748,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
|||||||
from .history_list import HistoryList
|
from .history_list import HistoryList
|
||||||
self.history_list = l = HistoryList(self)
|
self.history_list = l = HistoryList(self)
|
||||||
l.searchable_list = l
|
l.searchable_list = l
|
||||||
return l
|
return self.create_list_tab(l, l.get_list_header())
|
||||||
|
|
||||||
def show_address(self, addr):
|
def show_address(self, addr):
|
||||||
from . import address_dialog
|
from . import address_dialog
|
||||||
@ -2458,60 +2451,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
|||||||
except (IOError, os.error) as reason:
|
except (IOError, os.error) as reason:
|
||||||
self.show_critical(_("Electrum was unable to export your labels.") + "\n" + str(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):
|
def sweep_key_dialog(self):
|
||||||
d = WindowModalDialog(self, title=_('Sweep private keys'))
|
d = WindowModalDialog(self, title=_('Sweep private keys'))
|
||||||
d.setMinimumSize(600, 300)
|
d.setMinimumSize(600, 300)
|
||||||
|
|||||||
@ -453,7 +453,7 @@ class Commands:
|
|||||||
from .exchange_rate import FxThread
|
from .exchange_rate import FxThread
|
||||||
fx = FxThread(self.config, None)
|
fx = FxThread(self.config, None)
|
||||||
kwargs['fx'] = fx
|
kwargs['fx'] = fx
|
||||||
return self.wallet.export_history(**kwargs)
|
return self.wallet.get_full_history(**kwargs)
|
||||||
|
|
||||||
@command('w')
|
@command('w')
|
||||||
def setlabel(self, key, label):
|
def setlabel(self, key, label):
|
||||||
|
|||||||
11
lib/plot.py
11
lib/plot.py
@ -14,17 +14,16 @@ from matplotlib.patches import Ellipse
|
|||||||
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker
|
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker
|
||||||
|
|
||||||
|
|
||||||
def plot_history(wallet, history):
|
def plot_history(history):
|
||||||
hist_in = defaultdict(int)
|
hist_in = defaultdict(int)
|
||||||
hist_out = defaultdict(int)
|
hist_out = defaultdict(int)
|
||||||
for item in history:
|
for item in history:
|
||||||
tx_hash, height, confirmations, timestamp, value, balance = item
|
if not item['confirmations']:
|
||||||
if not confirmations:
|
|
||||||
continue
|
continue
|
||||||
if timestamp is None:
|
if item['timestamp'] is None:
|
||||||
continue
|
continue
|
||||||
value = value*1./COIN
|
value = item['value'].value/COIN
|
||||||
date = datetime.datetime.fromtimestamp(timestamp)
|
date = item['date']
|
||||||
datenum = int(md.date2num(datetime.date(date.year, date.month, 1)))
|
datenum = int(md.date2num(datetime.date(date.year, date.month, 1)))
|
||||||
if value > 0:
|
if value > 0:
|
||||||
hist_in[datenum] += value
|
hist_in[datenum] += value
|
||||||
|
|||||||
36
lib/util.py
36
lib/util.py
@ -77,11 +77,47 @@ class UserCancelled(Exception):
|
|||||||
'''An exception that is suppressed from the user'''
|
'''An exception that is suppressed from the user'''
|
||||||
pass
|
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):
|
class MyEncoder(json.JSONEncoder):
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
from .transaction import Transaction
|
from .transaction import Transaction
|
||||||
if isinstance(obj, Transaction):
|
if isinstance(obj, Transaction):
|
||||||
return obj.as_dict()
|
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)
|
return super(MyEncoder, self).default(obj)
|
||||||
|
|
||||||
class PrintError(object):
|
class PrintError(object):
|
||||||
|
|||||||
@ -948,13 +948,14 @@ class Abstract_Wallet(PrintError):
|
|||||||
# return last balance
|
# return last balance
|
||||||
return balance
|
return balance
|
||||||
|
|
||||||
def export_history(self, domain=None, from_timestamp=None, to_timestamp=None, fx=None, show_addresses=False):
|
def get_full_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
|
from .util import timestamp_to_datetime, Satoshis, Fiat
|
||||||
h = self.get_history(domain)
|
|
||||||
out = []
|
out = []
|
||||||
init_balance = None
|
init_balance = None
|
||||||
|
end_balance = 0
|
||||||
capital_gains = 0
|
capital_gains = 0
|
||||||
fiat_income = 0
|
fiat_income = 0
|
||||||
|
h = self.get_history(domain)
|
||||||
for tx_hash, height, conf, timestamp, value, balance in h:
|
for tx_hash, height, conf, timestamp, value, balance in h:
|
||||||
if from_timestamp and timestamp < from_timestamp:
|
if from_timestamp and timestamp < from_timestamp:
|
||||||
continue
|
continue
|
||||||
@ -965,17 +966,15 @@ class Abstract_Wallet(PrintError):
|
|||||||
'height':height,
|
'height':height,
|
||||||
'confirmations':conf,
|
'confirmations':conf,
|
||||||
'timestamp':timestamp,
|
'timestamp':timestamp,
|
||||||
'value': format_satoshis(value, True) if value is not None else '--',
|
'value': Satoshis(value),
|
||||||
'balance': format_satoshis(balance)
|
'balance': Satoshis(balance)
|
||||||
}
|
}
|
||||||
if init_balance is None:
|
if init_balance is None:
|
||||||
init_balance = balance - value
|
init_balance = balance - value
|
||||||
|
init_timestamp = timestamp
|
||||||
end_balance = balance
|
end_balance = balance
|
||||||
if item['height']>0:
|
end_timestamp = timestamp
|
||||||
date_str = format_time(timestamp) if timestamp is not None else _("unverified")
|
item['date'] = timestamp_to_datetime(timestamp) if timestamp is not None else None
|
||||||
else:
|
|
||||||
date_str = _("unconfirmed")
|
|
||||||
item['date'] = date_str
|
|
||||||
item['label'] = self.get_label(tx_hash)
|
item['label'] = self.get_label(tx_hash)
|
||||||
if show_addresses:
|
if show_addresses:
|
||||||
tx = self.transactions.get(tx_hash)
|
tx = self.transactions.get(tx_hash)
|
||||||
@ -997,36 +996,44 @@ class Abstract_Wallet(PrintError):
|
|||||||
fiat_value = self.get_fiat_value(tx_hash, fx.ccy)
|
fiat_value = self.get_fiat_value(tx_hash, fx.ccy)
|
||||||
if fiat_value is None:
|
if fiat_value is None:
|
||||||
fiat_value = fx.historical_value(value, date)
|
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:
|
if value < 0:
|
||||||
ap, lp = self.capital_gain(tx_hash, fx.timestamp_rate, fx.ccy)
|
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
|
cg = None if lp is None or ap is None else lp - ap
|
||||||
item['acquisition_price'] = fx.format_fiat(ap)
|
item['acquisition_price'] = Fiat(ap, fx.ccy)
|
||||||
item['capital_gain'] = fx.format_fiat(cg)
|
item['capital_gain'] = Fiat(cg, fx.ccy)
|
||||||
if cg is not None:
|
if cg is not None:
|
||||||
capital_gains += cg
|
capital_gains += cg
|
||||||
else:
|
else:
|
||||||
if fiat_value is not None:
|
if fiat_value is not None:
|
||||||
fiat_income += fiat_value
|
fiat_income += fiat_value
|
||||||
out.append(item)
|
out.append(item)
|
||||||
|
result = {'transactions': out}
|
||||||
if from_timestamp and to_timestamp:
|
if from_timestamp is not None and to_timestamp is not None:
|
||||||
summary = {
|
start_date = timestamp_to_datetime(from_timestamp)
|
||||||
'start_date': format_time(from_timestamp),
|
end_date = timestamp_to_datetime(to_timestamp)
|
||||||
'end_date': format_time(to_timestamp),
|
else:
|
||||||
'start_balance': format_satoshis(init_balance),
|
start_date = timestamp_to_datetime(init_timestamp)
|
||||||
'end_balance': format_satoshis(end_balance),
|
end_date = timestamp_to_datetime(end_timestamp)
|
||||||
'capital_gains': fx.format_fiat(capital_gains),
|
summary = {
|
||||||
'fiat_income': fx.format_fiat(fiat_income)
|
'start_date': start_date,
|
||||||
}
|
'end_date': end_date,
|
||||||
if fx:
|
'start_balance': Satoshis(init_balance),
|
||||||
start_date = timestamp_to_datetime(from_timestamp)
|
'end_balance': Satoshis(end_balance)
|
||||||
end_date = timestamp_to_datetime(to_timestamp)
|
}
|
||||||
summary['start_fiat_balance'] = fx.format_fiat(fx.historical_value(init_balance, start_date))
|
result['summary'] = summary
|
||||||
summary['end_fiat_balance'] = fx.format_fiat(fx.historical_value(end_balance, end_date))
|
if fx:
|
||||||
out.append(summary)
|
unrealized = self.unrealized_gains(domain, fx.timestamp_rate, fx.ccy)
|
||||||
|
summary['start_fiat_balance'] = Fiat(fx.historical_value(init_balance, start_date), fx.ccy)
|
||||||
return out
|
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):
|
def get_label(self, tx_hash):
|
||||||
label = self.labels.get(tx_hash, '')
|
label = self.labels.get(tx_hash, '')
|
||||||
@ -1662,6 +1669,16 @@ class Abstract_Wallet(PrintError):
|
|||||||
height, conf, timestamp = self.get_tx_height(txid)
|
height, conf, timestamp = self.get_tx_height(txid)
|
||||||
return price_func(timestamp)
|
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):
|
def capital_gain(self, txid, price_func, ccy):
|
||||||
"""
|
"""
|
||||||
Difference between the fiat price of coins leaving the wallet because of transaction txid,
|
Difference between the fiat price of coins leaving the wallet because of transaction txid,
|
||||||
@ -1683,7 +1700,6 @@ class Abstract_Wallet(PrintError):
|
|||||||
acquisition_price = None
|
acquisition_price = None
|
||||||
return acquisition_price, liquidation_price
|
return acquisition_price, liquidation_price
|
||||||
|
|
||||||
|
|
||||||
def average_price(self, tx, price_func, ccy):
|
def average_price(self, tx, price_func, ccy):
|
||||||
""" average price of the inputs of a transaction """
|
""" average price of the inputs of a transaction """
|
||||||
input_value = sum(self.txin_value(txin) for txin in tx.inputs()) / Decimal(COIN)
|
input_value = sum(self.txin_value(txin) for txin in tx.inputs()) / Decimal(COIN)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user