electrum

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

commit b94a7920af0837df84fcb76968e754cbbe5cf972
parent f2d2d61894d1dcbf9929060e9ae2be005d52575e
Author: ThomasV <thomasv@electrum.org>
Date:   Wed,  8 Jun 2016 11:06:51 +0200

factorize code used for tx dialogs

Diffstat:
Mgui/kivy/uix/dialogs/tx_dialog.py | 45++++++++++-----------------------------------
Mgui/qt/transaction_dialog.py | 65++++++++++++++++-------------------------------------------------
Mlib/wallet.py | 45+++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 71 insertions(+), 84 deletions(-)

diff --git a/gui/kivy/uix/dialogs/tx_dialog.py b/gui/kivy/uix/dialogs/tx_dialog.py @@ -104,42 +104,17 @@ class TxDialog(Factory.Popup): self.update() def update(self): - is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(self.tx) - self.can_broadcast = False - if self.tx.is_complete(): - self.tx_hash = self.tx.hash() - self.description = self.wallet.get_label(self.tx_hash) - if self.tx_hash in self.wallet.transactions.keys(): - height, conf, timestamp = self.wallet.get_tx_height(self.tx_hash) - if conf: - self.status_str = _("%d confirmations")%conf - self.date_str = datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] - else: - self.status_str = _('Unconfirmed') - if fee is None: - fee = self.wallet.tx_fees.get(tx_hash) - else: - self.can_broadcast = self.app.network is not None - self.status_str = _('Signed') - else: - s, r = self.tx.signature_count() - self.status_str = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r) - - self.is_mine = is_mine - if is_relevant: - if is_mine: - if fee is not None: - self.amount_str = self.app.format_amount_and_units(-v-fee) - self.fee_str = self.app.format_amount_and_units(fee) - else: - self.amount_str = self.app.format_amount_and_units(-v) - self.fee_str = _("unknown") - else: - self.amount_str = self.app.format_amount_and_units(v) - self.fee_str = '' - else: + self.tx_hash, self.status_str, self.description, self.can_broadcast, amount, fee, height, conf, timestamp = self.wallet.get_tx_info(self.tx) + self.date_str = datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] if timestamp else '' + if amount is None: self.amount_str = _("Transaction unrelated to your wallet") - self.fee_str = '' + elif amount > 0: + self.is_mine = False + self.amount_str = self.app.format_amount_and_units(amount) + else: + self.is_mine = True + self.amount_str = self.app.format_amount_and_units(-amount) + self.fee_str = self.app.format_amount_and_units(fee) if fee is not None else _('unknown') self.can_sign = self.wallet.can_sign(self.tx) self.ids.output_list.update(self.tx.outputs()) diff --git a/gui/qt/transaction_dialog.py b/gui/qt/transaction_dialog.py @@ -175,43 +175,18 @@ class TxDialog(QDialog, MessageBoxMixin): def update(self): + desc = self.desc base_unit = self.main_window.base_unit() format_amount = self.main_window.format_amount - is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(self.tx) - desc = self.desc - time_str = None - exp_n = None - self.broadcast_button.hide() - - if self.tx.is_complete(): - tx_hash = self.tx.hash() - if tx_hash in self.wallet.transactions.keys(): - desc = self.wallet.get_label(tx_hash) - height, conf, timestamp = self.wallet.get_tx_height(tx_hash) - if height > 0: - if conf: - status = _("%d confirmations") % conf - time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] - else: - status = _('Not verified') - else: - status = _('Unconfirmed') - if fee is None: - fee = self.wallet.tx_fees.get(tx_hash) - if fee: - size = self.tx.estimated_size() - fee_per_kb = fee * 1000 / size - exp_n = self.wallet.network.reverse_dynfee(fee_per_kb) - else: - status = _("Signed") - self.broadcast_button.show() - # cannot broadcast when offline - if self.main_window.network is None: - self.broadcast_button.setEnabled(False) + tx_hash, status, label, can_broadcast, amount, fee, height, conf, timestamp = self.wallet.get_tx_info(self.tx) + + if can_broadcast: + self.broadcast_button.show() + # cannot broadcast when offline + if self.main_window.network is None: + self.broadcast_button.setEnabled(False) else: - s, r = self.tx.signature_count() - status = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r) - tx_hash = _('Unknown'); + self.broadcast_button.hide() if self.wallet.can_sign(self.tx): self.sign_button.show() @@ -226,7 +201,8 @@ class TxDialog(QDialog, MessageBoxMixin): self.tx_desc.show() self.status_label.setText(_('Status:') + ' ' + status) - if time_str is not None: + if timestamp is not None: + time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] self.date_label.setText(_("Date: %s")%time_str) self.date_label.show() elif exp_n: @@ -234,27 +210,18 @@ class TxDialog(QDialog, MessageBoxMixin): self.date_label.show() else: self.date_label.hide() - # if we are not synchronized, we cannot tell if not self.wallet.up_to_date: return - - if is_relevant: - if is_mine: - if fee is not None: - amount_str = _("Amount sent:") + ' %s'% format_amount(-v-fee) + ' ' + base_unit - else: - amount_str = _("Amount sent:") + ' %s'% format_amount(-v) + ' ' + base_unit - else: - amount_str = _("Amount received:") + ' %s'% format_amount(v) + ' ' + base_unit - else: + if amount is None: amount_str = _("Transaction unrelated to your wallet") - + elif amount > 0: + amount_str = _("Amount received:") + ' %s'% format_amount(amount) + ' ' + base_unit + else: + amount_str = _("Amount sent:") + ' %s'% format_amount(-amount) + ' ' + base_unit fee_str = _("Transaction fee") + ': %s'% (format_amount(fee) + ' ' + base_unit if fee is not None else _('unknown')) - self.amount_label.setText(amount_str) self.fee_label.setText(fee_str) - run_hook('transaction_dialog_update', self) diff --git a/lib/wallet.py b/lib/wallet.py @@ -601,6 +601,51 @@ class Abstract_Wallet(PrintError): fee = None return is_relevant, is_mine, v, fee + def get_tx_info(self, tx): + is_relevant, is_mine, v, fee = self.get_wallet_delta(tx) + exp_n = None + can_broadcast = False + label = None + if tx.is_complete(): + tx_hash = tx.hash() + if tx_hash in self.transactions.keys(): + label = self.get_label(tx_hash) + height, conf, timestamp = self.get_tx_height(tx_hash) + if height > 0: + if conf: + status = _("%d confirmations") % conf + else: + status = _('Not verified') + else: + status = _('Unconfirmed') + if fee is None: + fee = self.tx_fees.get(tx_hash) + if fee: + size = tx.estimated_size() + fee_per_kb = fee * 1000 / size + exp_n = self.network.reverse_dynfee(fee_per_kb) + else: + status = _("Signed") + can_broadcast = self.network is not None + else: + s, r = tx.signature_count() + status = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r) + tx_hash = _('Unknown') + + if is_relevant: + if is_mine: + if fee is not None: + amount = v + fee + else: + amount = v + else: + amount = v + else: + amount = None + + return tx_hash, status, label, can_broadcast, amount, fee, height, conf, timestamp + + def get_addr_io(self, address): h = self.history.get(address, []) received = {}