commit b2c035024006f72efc711b37b39010067a5487cf
parent cc19de9db3f3a64f5234c47b1b3cc2ac17f03c34
Author: ThomasV <thomasv@electrum.org>
Date: Wed, 14 Feb 2018 10:40:11 +0100
allow to use exchange rates while offline
Diffstat:
3 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/lib/commands.py b/lib/commands.py
@@ -449,6 +449,10 @@ class Commands:
end_date = datetime.datetime(year+1, 1, 1)
kwargs['from_timestamp'] = time.mktime(start_date.timetuple())
kwargs['to_timestamp'] = time.mktime(end_date.timetuple())
+ if show_fiat:
+ from .exchange_rate import FxThread
+ fx = FxThread(self.config, None)
+ kwargs['fx'] = fx
return self.wallet.export_history(**kwargs)
@command('w')
diff --git a/lib/daemon.py b/lib/daemon.py
@@ -121,13 +121,12 @@ class Daemon(DaemonThread):
self.config = config
if config.get('offline'):
self.network = None
- self.fx = None
else:
self.network = Network(config)
self.network.start()
- self.fx = FxThread(config, self.network)
+ self.fx = FxThread(config, self.network)
+ if self.network:
self.network.add_jobs([self.fx])
-
self.gui = None
self.wallets = {}
# Setup JSONRPC server
diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py
@@ -61,9 +61,10 @@ class ExchangeBase(PrintError):
t.setDaemon(True)
t.start()
- def get_historical_rates_safe(self, ccy, cache_dir):
+ def read_historical_rates(self, ccy, cache_dir):
filename = os.path.join(cache_dir, self.name() + '_'+ ccy)
- if os.path.exists(filename) and (time.time() - os.stat(filename).st_mtime) < 24*3600:
+ if os.path.exists(filename):
+ timestamp = os.stat(filename).st_mtime
try:
with open(filename, 'r') as f:
h = json.loads(f.read())
@@ -71,7 +72,15 @@ class ExchangeBase(PrintError):
h = None
else:
h = None
- if h is None:
+ timestamp = False
+ if h:
+ self.history[ccy] = h
+ self.on_history()
+ return h, timestamp
+
+ def get_historical_rates_safe(self, ccy, cache_dir):
+ h, timestamp = self.read_historical_rates()
+ if h is None or time.time() - timestamp < 24*3600:
try:
self.print_error("requesting fx history for", ccy)
h = self.request_history(ccy)
@@ -397,8 +406,8 @@ class FxThread(ThreadJob):
self.history_used_spot = False
self.ccy_combo = None
self.hist_checkbox = None
- self.set_exchange(self.config_exchange())
self.cache_dir = os.path.join(config.path, 'cache')
+ self.set_exchange(self.config_exchange())
if not os.path.exists(self.cache_dir):
os.mkdir(self.cache_dir)
@@ -471,12 +480,15 @@ class FxThread(ThreadJob):
# A new exchange means new fx quotes, initially empty. Force
# a quote refresh
self.timeout = 0
+ self.exchange.read_historical_rates(self.ccy, self.cache_dir)
def on_quotes(self):
- self.network.trigger_callback('on_quotes')
+ if self.network:
+ self.network.trigger_callback('on_quotes')
def on_history(self):
- self.network.trigger_callback('on_history')
+ if self.network:
+ self.network.trigger_callback('on_history')
def exchange_rate(self):
'''Returns None, or the exchange rate as a Decimal'''
@@ -514,6 +526,11 @@ class FxThread(ThreadJob):
rate = self.history_rate(d_t)
return self.value_str(satoshis, rate)
+ def historical_value(self, satoshis, d_t):
+ rate = self.history_rate(d_t)
+ if rate:
+ return Decimal(satoshis) / COIN * Decimal(rate)
+
def timestamp_rate(self, timestamp):
from electrum.util import timestamp_to_datetime
date = timestamp_to_datetime(timestamp)