Browse Source

qt lightning_tx_dialog: use historical fx rate for fiat amounts

patch-4
SomberNight 4 years ago
parent
commit
1ba5997238
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 28
      electrum/exchange_rate.py
  2. 7
      electrum/gui/qt/lightning_tx_dialog.py
  3. 8
      electrum/gui/qt/main_window.py
  4. 2
      electrum/util.py

28
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)

7
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))

8
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:

2
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)

Loading…
Cancel
Save