Allow fiat_convert to use same symbol for Crypto and FIAT

This commit is contained in:
Gerald Lonlas 2018-06-01 20:58:07 -07:00
parent c9e49ed7b4
commit 638d98735f
2 changed files with 18 additions and 4 deletions

View File

@ -95,8 +95,11 @@ class CryptoToFiatConverter(object):
coinlistings = self._coinmarketcap.listings()
self._cryptomap = dict(map(lambda coin: (coin["symbol"], str(coin["id"])),
coinlistings["data"]))
except (ValueError, RequestException) as e:
logger.error("Could not load FIAT Cryptocurrency map for the following problem: %s", e)
except (ValueError, RequestException) as exception:
logger.error(
"Could not load FIAT Cryptocurrency map for the following problem: %s",
exception
)
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
"""
@ -188,6 +191,10 @@ class CryptoToFiatConverter(object):
if not self._is_supported_fiat(fiat=fiat_symbol):
raise ValueError('The fiat {} is not supported.'.format(fiat_symbol))
# No need to convert if both crypto and fiat are the same
if crypto_symbol == fiat_symbol:
return 1.0
if crypto_symbol not in self._cryptomap:
# return 0 for unsupported stake currencies (fiat-convert should not break the bot)
logger.warning("unsupported crypto-symbol %s - returning 0.0", crypto_symbol)
@ -199,6 +206,6 @@ class CryptoToFiatConverter(object):
convert=fiat_symbol
)['data']['quotes'][fiat_symbol.upper()]['price']
)
except BaseException as ex:
logger.error("Error in _find_price: %s", ex)
except BaseException as exception:
logger.error("Error in _find_price: %s", exception)
return 0.0

View File

@ -126,6 +126,13 @@ def test_fiat_convert_get_price(mocker):
assert fiat_convert._pairs[0]._expiration is not expiration
def test_fiat_convert_same_currencies(mocker):
patch_coinmarketcap(mocker)
fiat_convert = CryptoToFiatConverter()
assert fiat_convert.get_price(crypto_symbol='USD', fiat_symbol='USD') == 1.0
def test_loadcryptomap(mocker):
patch_coinmarketcap(mocker)