allow to use exchange rates while offline
This commit is contained in:
parent
cc19de9db3
commit
b2c0350240
|
@ -449,6 +449,10 @@ class Commands:
|
||||||
end_date = datetime.datetime(year+1, 1, 1)
|
end_date = datetime.datetime(year+1, 1, 1)
|
||||||
kwargs['from_timestamp'] = time.mktime(start_date.timetuple())
|
kwargs['from_timestamp'] = time.mktime(start_date.timetuple())
|
||||||
kwargs['to_timestamp'] = time.mktime(end_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)
|
return self.wallet.export_history(**kwargs)
|
||||||
|
|
||||||
@command('w')
|
@command('w')
|
||||||
|
|
|
@ -121,13 +121,12 @@ class Daemon(DaemonThread):
|
||||||
self.config = config
|
self.config = config
|
||||||
if config.get('offline'):
|
if config.get('offline'):
|
||||||
self.network = None
|
self.network = None
|
||||||
self.fx = None
|
|
||||||
else:
|
else:
|
||||||
self.network = Network(config)
|
self.network = Network(config)
|
||||||
self.network.start()
|
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.network.add_jobs([self.fx])
|
||||||
|
|
||||||
self.gui = None
|
self.gui = None
|
||||||
self.wallets = {}
|
self.wallets = {}
|
||||||
# Setup JSONRPC server
|
# Setup JSONRPC server
|
||||||
|
|
|
@ -61,9 +61,10 @@ class ExchangeBase(PrintError):
|
||||||
t.setDaemon(True)
|
t.setDaemon(True)
|
||||||
t.start()
|
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)
|
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:
|
try:
|
||||||
with open(filename, 'r') as f:
|
with open(filename, 'r') as f:
|
||||||
h = json.loads(f.read())
|
h = json.loads(f.read())
|
||||||
|
@ -71,7 +72,15 @@ class ExchangeBase(PrintError):
|
||||||
h = None
|
h = None
|
||||||
else:
|
else:
|
||||||
h = None
|
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:
|
try:
|
||||||
self.print_error("requesting fx history for", ccy)
|
self.print_error("requesting fx history for", ccy)
|
||||||
h = self.request_history(ccy)
|
h = self.request_history(ccy)
|
||||||
|
@ -397,8 +406,8 @@ class FxThread(ThreadJob):
|
||||||
self.history_used_spot = False
|
self.history_used_spot = False
|
||||||
self.ccy_combo = None
|
self.ccy_combo = None
|
||||||
self.hist_checkbox = None
|
self.hist_checkbox = None
|
||||||
self.set_exchange(self.config_exchange())
|
|
||||||
self.cache_dir = os.path.join(config.path, 'cache')
|
self.cache_dir = os.path.join(config.path, 'cache')
|
||||||
|
self.set_exchange(self.config_exchange())
|
||||||
if not os.path.exists(self.cache_dir):
|
if not os.path.exists(self.cache_dir):
|
||||||
os.mkdir(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 new exchange means new fx quotes, initially empty. Force
|
||||||
# a quote refresh
|
# a quote refresh
|
||||||
self.timeout = 0
|
self.timeout = 0
|
||||||
|
self.exchange.read_historical_rates(self.ccy, self.cache_dir)
|
||||||
|
|
||||||
def on_quotes(self):
|
def on_quotes(self):
|
||||||
self.network.trigger_callback('on_quotes')
|
if self.network:
|
||||||
|
self.network.trigger_callback('on_quotes')
|
||||||
|
|
||||||
def on_history(self):
|
def on_history(self):
|
||||||
self.network.trigger_callback('on_history')
|
if self.network:
|
||||||
|
self.network.trigger_callback('on_history')
|
||||||
|
|
||||||
def exchange_rate(self):
|
def exchange_rate(self):
|
||||||
'''Returns None, or the exchange rate as a Decimal'''
|
'''Returns None, or the exchange rate as a Decimal'''
|
||||||
|
@ -514,6 +526,11 @@ class FxThread(ThreadJob):
|
||||||
rate = self.history_rate(d_t)
|
rate = self.history_rate(d_t)
|
||||||
return self.value_str(satoshis, rate)
|
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):
|
def timestamp_rate(self, timestamp):
|
||||||
from electrum.util import timestamp_to_datetime
|
from electrum.util import timestamp_to_datetime
|
||||||
date = timestamp_to_datetime(timestamp)
|
date = timestamp_to_datetime(timestamp)
|
||||||
|
|
Loading…
Reference in New Issue