wallet: cache NaN coin prices, clear cache on new history
This commit is contained in:
@@ -173,6 +173,7 @@ class ElectrumWindow(App):
|
|||||||
|
|
||||||
def on_history(self, d):
|
def on_history(self, d):
|
||||||
Logger.info("on_history")
|
Logger.info("on_history")
|
||||||
|
self.wallet.clear_coin_price_cache()
|
||||||
self._trigger_update_history()
|
self._trigger_update_history()
|
||||||
|
|
||||||
def on_fee_histogram(self, *args):
|
def on_fee_histogram(self, *args):
|
||||||
|
|||||||
@@ -222,6 +222,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
|||||||
self.fetch_alias()
|
self.fetch_alias()
|
||||||
|
|
||||||
def on_history(self, b):
|
def on_history(self, b):
|
||||||
|
self.wallet.clear_coin_price_cache()
|
||||||
self.new_fx_history_signal.emit()
|
self.new_fx_history_signal.emit()
|
||||||
|
|
||||||
def setup_exception_hook(self):
|
def setup_exception_hook(self):
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ class Abstract_Wallet(AddressSynchronizer):
|
|||||||
self.invoices = InvoiceStore(self.storage)
|
self.invoices = InvoiceStore(self.storage)
|
||||||
self.contacts = Contacts(self.storage)
|
self.contacts = Contacts(self.storage)
|
||||||
|
|
||||||
self.coin_price_cache = {}
|
self._coin_price_cache = {}
|
||||||
|
|
||||||
def load_and_cleanup(self):
|
def load_and_cleanup(self):
|
||||||
self.load_keystore()
|
self.load_keystore()
|
||||||
@@ -1178,6 +1178,9 @@ class Abstract_Wallet(AddressSynchronizer):
|
|||||||
total_price += self.coin_price(ser.split(':')[0], price_func, ccy, v)
|
total_price += self.coin_price(ser.split(':')[0], price_func, ccy, v)
|
||||||
return total_price / (input_value/Decimal(COIN))
|
return total_price / (input_value/Decimal(COIN))
|
||||||
|
|
||||||
|
def clear_coin_price_cache(self):
|
||||||
|
self._coin_price_cache = {}
|
||||||
|
|
||||||
def coin_price(self, txid, price_func, ccy, txin_value):
|
def coin_price(self, txid, price_func, ccy, txin_value):
|
||||||
"""
|
"""
|
||||||
Acquisition price of a coin.
|
Acquisition price of a coin.
|
||||||
@@ -1185,17 +1188,13 @@ class Abstract_Wallet(AddressSynchronizer):
|
|||||||
"""
|
"""
|
||||||
if txin_value is None:
|
if txin_value is None:
|
||||||
return Decimal('NaN')
|
return Decimal('NaN')
|
||||||
# FIXME: this mutual recursion will be really slow and might even reach
|
|
||||||
# max recursion depth if there are no FX rates available as then
|
|
||||||
# nothing will be cached.
|
|
||||||
cache_key = "{}:{}:{}".format(str(txid), str(ccy), str(txin_value))
|
cache_key = "{}:{}:{}".format(str(txid), str(ccy), str(txin_value))
|
||||||
result = self.coin_price_cache.get(cache_key, None)
|
result = self._coin_price_cache.get(cache_key, None)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
return result
|
return result
|
||||||
if self.txi.get(txid, {}) != {}:
|
if self.txi.get(txid, {}) != {}:
|
||||||
result = self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN)
|
result = self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN)
|
||||||
if not result.is_nan():
|
self._coin_price_cache[cache_key] = result
|
||||||
self.coin_price_cache[cache_key] = result
|
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
fiat_value = self.get_fiat_value(txid, ccy)
|
fiat_value = self.get_fiat_value(txid, ccy)
|
||||||
|
|||||||
Reference in New Issue
Block a user