Adds token helper functions.

This commit is contained in:
PhilipWafula 2021-10-25 21:01:25 +03:00
parent 3a97cf95e8
commit bbd43473b6
Signed by untrusted user: mango-habanero
GPG Key ID: B00CE9034DA19FB7

View File

@ -1,4 +1,5 @@
# standard imports
import hashlib
import json
import logging
from typing import Dict, Optional
@ -10,12 +11,34 @@ from cic_types.condiments import MetadataPointer
# local imports
from cic_ussd.account.chain import Chain
from cic_ussd.cache import cache_data_key, get_cached_data
from cic_ussd.error import SeppukuError
from cic_ussd.error import CachedDataNotFoundError, SeppukuError
from cic_ussd.metadata.tokens import query_token_info, query_token_metadata
logg = logging.getLogger(__name__)
def collate_token_metadata(token_info: dict, token_metadata: dict) -> dict:
"""
:param token_info:
:type token_info:
:param token_metadata:
:type token_metadata:
:return:
:rtype:
"""
description = token_info.get('description')
issuer = token_info.get('issuer')
location = token_metadata.get('location')
contact = token_metadata.get('contact')
return {
'description': description,
'issuer': issuer,
'location': location,
'contact': contact
}
def get_cached_default_token(chain_str: str) -> Optional[str]:
"""This function attempts to retrieve the default token's data from the redis cache.
:param chain_str: chain name and network id.
@ -49,6 +72,33 @@ def get_default_token_symbol():
raise SeppukuError(f'Could not retrieve default token for: {chain_str}')
def hashed_token_proof(token_proof: dict) -> str:
"""
:param token_proof:
:type token_proof:
:return:
:rtype:
"""
hash_object = hashlib.new("sha256")
hash_object.update(json.dumps(token_proof))
return hash_object.digest().hex()
def process_token_data(token_symbol: str):
"""
:param token_symbol:
:type token_symbol:
:return:
:rtype:
"""
identifier = token_symbol.encode('utf-8')
query_token_metadata(identifier=identifier)
token_info = query_token_info(identifier=identifier)
hashed_token_info = hashed_token_proof(token_proof=token_info)
query_token_data(hashed_proofs=[hashed_token_info], token_symbols=[token_symbol])
def query_default_token(chain_str: str):
"""This function synchronously queries cic-eth for the deployed system's default token.
:param chain_str: Chain name and network id.
@ -60,3 +110,19 @@ def query_default_token(chain_str: str):
cic_eth_api = Api(chain_str=chain_str)
default_token_request_task = cic_eth_api.default_token()
return default_token_request_task.get()
def query_token_data(hashed_proofs: list, token_symbols: list):
"""
:param hashed_proofs:
:type hashed_proofs:
:param token_symbols:
:type token_symbols:
:return:
:rtype:
"""
api = Api(callback_param='',
callback_queue='cic-ussd',
chain_str=Chain.spec.__str__(),
callback_task='cic_ussd.tasks.callback_handler.token_data_callback')
api.tokens(token_symbols=token_symbols, proof=hashed_proofs)