From 1ba599723871880fc7ecbd3bced25f5aad0eee6d Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 12 Mar 2021 18:29:00 +0100 Subject: [PATCH] qt lightning_tx_dialog: use historical fx rate for fiat amounts --- electrum/exchange_rate.py | 28 ++++++++++++++++---------- electrum/gui/qt/lightning_tx_dialog.py | 7 ++++--- electrum/gui/qt/main_window.py | 8 ++++---- electrum/util.py | 2 +- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/electrum/exchange_rate.py b/electrum/exchange_rate.py index c6c8bd153..17f5429f0 100644 --- a/electrum/exchange_rate.py +++ b/electrum/exchange_rate.py @@ -614,12 +614,18 @@ class FxThread(ThreadJob): return Decimal('NaN') return Decimal(rate) - def format_amount(self, btc_balance): - rate = self.exchange_rate() + def format_amount(self, btc_balance, *, timestamp: int = None) -> str: + if timestamp is None: + rate = self.exchange_rate() + else: + rate = self.timestamp_rate(timestamp) return '' if rate.is_nan() else "%s" % self.value_str(btc_balance, rate) - def format_amount_and_units(self, btc_balance): - rate = self.exchange_rate() + def format_amount_and_units(self, btc_balance, *, timestamp: int = None) -> str: + if timestamp is None: + rate = self.exchange_rate() + else: + rate = self.timestamp_rate(timestamp) return '' if rate.is_nan() else "%s %s" % (self.value_str(btc_balance, rate), self.ccy) def get_fiat_status_text(self, btc_balance, base_unit, decimal_point): @@ -627,18 +633,18 @@ class FxThread(ThreadJob): return _(" (No FX rate available)") if rate.is_nan() else " 1 %s~%s %s" % (base_unit, self.value_str(COIN / (10**(8 - decimal_point)), rate), self.ccy) - def fiat_value(self, satoshis, rate): + def fiat_value(self, satoshis, rate) -> Decimal: return Decimal('NaN') if satoshis is None else Decimal(satoshis) / COIN * Decimal(rate) - def value_str(self, satoshis, rate): + def value_str(self, satoshis, rate) -> str: return self.format_fiat(self.fiat_value(satoshis, rate)) - def format_fiat(self, value): + def format_fiat(self, value: Decimal) -> str: if value.is_nan(): return _("No data") return "%s" % (self.ccy_amount_str(value, True)) - def history_rate(self, d_t): + def history_rate(self, d_t: Optional[datetime]) -> Decimal: if d_t is None: return Decimal('NaN') rate = self.exchange.historical_rate(self.ccy, d_t) @@ -651,13 +657,13 @@ class FxThread(ThreadJob): rate = 'NaN' return Decimal(rate) - def historical_value_str(self, satoshis, d_t): + def historical_value_str(self, satoshis, d_t: Optional[datetime]) -> str: return self.format_fiat(self.historical_value(satoshis, d_t)) - def historical_value(self, satoshis, d_t): + def historical_value(self, satoshis, d_t: Optional[datetime]) -> Decimal: return self.fiat_value(satoshis, self.history_rate(d_t)) - def timestamp_rate(self, timestamp): + def timestamp_rate(self, timestamp: Optional[int]) -> Decimal: from .util import timestamp_to_datetime date = timestamp_to_datetime(timestamp) return self.history_rate(date) diff --git a/electrum/gui/qt/lightning_tx_dialog.py b/electrum/gui/qt/lightning_tx_dialog.py index 9407d40e0..52423182c 100644 --- a/electrum/gui/qt/lightning_tx_dialog.py +++ b/electrum/gui/qt/lightning_tx_dialog.py @@ -64,11 +64,12 @@ class LightningTxDialog(WindowModalDialog): vbox = QVBoxLayout() self.setLayout(vbox) - # FIXME fiat values here are using today's FX rate instead of historical - vbox.addWidget(QLabel(_("Amount") + ": " + self.parent.format_amount_and_units(self.amount))) + amount_str = self.parent.format_amount_and_units(self.amount, timestamp=self.timestamp) + vbox.addWidget(QLabel(_("Amount") + f": {amount_str}")) if self.is_sent: fee = Decimal(tx_item['fee_msat']) / 1000 - vbox.addWidget(QLabel(_("Fee") + ": " + self.parent.format_amount_and_units(fee))) + fee_str = self.parent.format_amount_and_units(fee, timestamp=self.timestamp) + vbox.addWidget(QLabel(_("Fee") + f": {fee_str}")) time_str = datetime.datetime.fromtimestamp(self.timestamp).isoformat(' ')[:-3] vbox.addWidget(QLabel(_("Date") + ": " + time_str)) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index e7b1c6fc7..f13b5486f 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -878,14 +878,14 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): """ return self.config.format_amount(amount_sat, is_diff=is_diff, whitespaces=whitespaces) - def format_amount_and_units(self, amount_sat) -> str: + def format_amount_and_units(self, amount_sat, *, timestamp: int = None) -> str: """Returns string with both bitcoin and fiat amounts, in desired units. E.g. 500_000 -> '0.005 BTC (191.42 EUR)' """ text = self.config.format_amount_and_units(amount_sat) - x = self.fx.format_amount_and_units(amount_sat) if self.fx else None - if text and x: - text += ' (%s)'%x + fiat = self.fx.format_amount_and_units(amount_sat, timestamp=timestamp) if self.fx else None + if text and fiat: + text += f' ({fiat})' return text def format_fiat_and_units(self, amount_sat) -> str: diff --git a/electrum/util.py b/electrum/util.py index cc5d38ed6..a6c20c768 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -700,7 +700,7 @@ def quantize_feerate(fee) -> Union[None, Decimal, int]: return Decimal(fee).quantize(_feerate_quanta, rounding=decimal.ROUND_HALF_DOWN) -def timestamp_to_datetime(timestamp): +def timestamp_to_datetime(timestamp: Optional[int]) -> Optional[datetime]: if timestamp is None: return None return datetime.fromtimestamp(timestamp)