Browse Source

exchange_rate: guard against garbage hist data coming from exchange

See discussion at 583089d57b (r104678577)
CoinGecko for PLN gives "None" str as rate (instead of null) for two months mid-2014:
```
  2.29 | D | exchange_rate.CoinGecko | found corrupted historical_rate: rate='None'. for ccy='PLN' at 2014-05-10
```
Thanks to @lukasz1992 for reporting.
master
SomberNight 3 years ago
parent
commit
0a5d18634c
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 7
      electrum/exchange_rate.py

7
electrum/exchange_rate.py

@ -161,8 +161,13 @@ class ExchangeBase(Logger):
return [] return []
def historical_rate(self, ccy: str, d_t: datetime) -> Decimal: def historical_rate(self, ccy: str, d_t: datetime) -> Decimal:
rate = self._history.get(ccy, {}).get(d_t.strftime('%Y-%m-%d')) or 'NaN' date_str = d_t.strftime('%Y-%m-%d')
rate = self._history.get(ccy, {}).get(date_str) or 'NaN'
try:
return Decimal(rate) return Decimal(rate)
except Exception: # guard against garbage coming from exchange
#self.logger.debug(f"found corrupted historical_rate: {rate=!r}. for {ccy=} at {date_str}")
return Decimal('NaN')
async def request_history(self, ccy: str) -> Dict[str, Union[str, float]]: async def request_history(self, ccy: str) -> Dict[str, Union[str, float]]:
raise NotImplementedError() # implemented by subclasses raise NotImplementedError() # implemented by subclasses

Loading…
Cancel
Save