Dynamically load cryptomap

This commit is contained in:
Matthias Voppichler 2018-05-13 19:46:08 +02:00
parent 263d34ae82
commit d07491ceb2

View File

@ -5,6 +5,7 @@ e.g BTC to USD
import logging import logging
import time import time
from typing import Dict
from coinmarketcap import Market from coinmarketcap import Market
@ -73,12 +74,7 @@ class CryptoToFiatConverter(object):
"RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD" "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD"
] ]
CRYPTOMAP = { _cryptomap: Dict = {}
'BTC': 'bitcoin',
'ETH': 'ethereum',
'USDT': 'thether',
'BNB': 'binance-coin'
}
def __new__(cls): def __new__(cls):
if CryptoToFiatConverter.__instance is None: if CryptoToFiatConverter.__instance is None:
@ -91,6 +87,15 @@ class CryptoToFiatConverter(object):
def __init__(self) -> None: def __init__(self) -> None:
self._pairs = [] self._pairs = []
self._load_cryptomap()
def _load_cryptomap(self) -> None:
try:
coinlistings = self._coinmarketcap.listings()
self._cryptomap = dict(map(lambda coin: (coin["symbol"], coin["id"]),
coinlistings["data"]))
except ValueError:
logger.error("Could not load FIAT Cryptocurrency map")
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float: def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
""" """
@ -182,14 +187,14 @@ class CryptoToFiatConverter(object):
if not self._is_supported_fiat(fiat=fiat_symbol): if not self._is_supported_fiat(fiat=fiat_symbol):
raise ValueError('The fiat {} is not supported.'.format(fiat_symbol)) raise ValueError('The fiat {} is not supported.'.format(fiat_symbol))
if crypto_symbol not in self.CRYPTOMAP: if crypto_symbol not in self._cryptomap:
# return 0 for unsupported stake currencies (fiat-convert should not break the bot) # return 0 for unsupported stake currencies (fiat-convert should not break the bot)
logger.warning("unsupported crypto-symbol %s - returning 0.0", crypto_symbol) logger.warning("unsupported crypto-symbol %s - returning 0.0", crypto_symbol)
return 0.0 return 0.0
try: try:
return float( return float(
self._coinmarketcap.ticker( self._coinmarketcap.ticker(
currency=self.CRYPTOMAP[crypto_symbol], currency=self._cryptomap[crypto_symbol],
convert=fiat_symbol convert=fiat_symbol
)[0]['price_' + fiat_symbol.lower()] )[0]['price_' + fiat_symbol.lower()]
) )