fix: /default_token

This commit is contained in:
William Luke 2021-12-15 16:03:01 +03:00
parent 47af8993d3
commit 3e2947b301
2 changed files with 14 additions and 11 deletions

View File

@ -14,6 +14,7 @@ logg = logging.getLogger(__file__)
class Cache:
store: Redis = None
def get_token_data(token_symbol: str):
"""
:param token_symbol:
@ -46,19 +47,23 @@ def set_token_data(token_symbol: str, data: dict):
cache_data(key, json.dumps(data))
logg.debug(f'Cached token data for: {token_symbol} at: {key}')
def get_default_token() -> Optional[str]:
"""This function attempts to retrieve the default token's data from the redis cache.
:param chain_str: chain name and network id.
:type chain_str: str
:return:
:rtype:
"""
logg.debug(f'Retrieving default token from cache for chain:')
logg.debug(f'Retrieving default token from cache')
# TODO: What should the identifier be?
key = cache_data_key(identifier="ff".encode('utf-8'),
salt=MetadataPointer.TOKEN_DEFAULT)
token_data = json.loads(get_cached_data(key=key))
logg.debug(f'Retrieved default token data: {token_data}')
return token_data
default_token_str = get_cached_data(key=key)
if default_token_str is None:
logg.debug(f'No cached default token found: {key}')
return None
default_token = json.loads(default_token_str)
logg.debug(f'Retrieved default token data: {default_token}')
return default_token
def set_default_token(default_token: dict):
@ -74,8 +79,6 @@ def set_default_token(default_token: dict):
cache_data(key, json.dumps(default_token))
def cache_data(key: str, data: str):
"""
:param key:

View File

@ -98,10 +98,10 @@ def handle_tokens(start_response, query: dict) -> List[bytes]:
def handle_default_token(start_response, _query) -> List[bytes]:
data = cache.get_default_token()
if data == None:
if data is None:
data = celery.call('default_token')
cache.set_default_token(data)
cache.set_token_data(data.get('token_symbol'), data)
if data is not None:
cache.set_default_token(data)
return uwsgi.respond(start_response, data)