Refactors balance.py

- Moves it into account package
- Makes functions names cic-compliant.
- Adds docstrings.
This commit is contained in:
PhilipWafula 2021-05-11 09:49:50 +03:00
parent 8a6d332bca
commit ff0ed7b99c
Signed by untrusted user: mango-habanero
GPG Key ID: B00CE9034DA19FB7
1 changed files with 24 additions and 21 deletions

View File

@ -33,12 +33,12 @@ class BalanceManager:
def get_balances(self, asynchronous: bool = False) -> Union[celery.Task, dict]:
"""
This function queries cic-eth for an account's balances, It provides a means to receive the balance either
asynchronously or synchronously depending on the provided value for teh asynchronous parameter. It returns a
dictionary containing network, outgoing and incoming balances.
asynchronously or synchronously depending on the provided asynchronous value from the function's caller. It
returns a dictionary containing network, outgoing and incoming balances when synchronously called.
:param asynchronous: Boolean value checking whether to return balances asynchronously
:type asynchronous: bool
:return:
:rtype:
:return: dict containing network, outgoing and incoming balances | an async result object from celery.
:rtype: dict|celery.Task
"""
if asynchronous:
cic_eth_api = Api(
@ -47,7 +47,7 @@ class BalanceManager:
callback_task='cic_ussd.tasks.callback_handler.process_balances_callback',
callback_param=''
)
cic_eth_api.balance(address=self.address, token_symbol=self.token_symbol)
return cic_eth_api.balance(address=self.address, token_symbol=self.token_symbol)
else:
cic_eth_api = Api(chain_str=self.chain_str)
balance_request_task = cic_eth_api.balance(
@ -56,27 +56,30 @@ class BalanceManager:
return balance_request_task.get()[0]
def compute_operational_balance(balances: dict) -> float:
"""This function calculates the right balance given incoming and outgoing
:param balances:
:type balances:
:return:
:rtype:
def operational_balance(balances: dict) -> float:
"""This function computes the operational balance at an instance in the system by providing the difference of the
outgoing balance from the sum of incoming and network balances.
:param balances: A dictionary containing incoming, outgoing and network balances.
:type balances: dict
:return: The operational balance of the account at the instance of querying.
:rtype: float
"""
incoming_balance = balances.get('balance_incoming')
outgoing_balance = balances.get('balance_outgoing')
network_balance = balances.get('balance_network')
operational_balance = (network_balance + incoming_balance) - outgoing_balance
return from_wei(value=operational_balance)
balance = (network_balance + incoming_balance) - outgoing_balance
return from_wei(value=balance)
def get_cached_operational_balance(blockchain_address: str):
"""
:param blockchain_address:
:type blockchain_address:
:return:
:rtype:
def cached_operational_balance(blockchain_address: str) -> float:
"""This function retrieves the cached balances data from redis cache and computes the operational balance from
the cached data.
:param blockchain_address: Ethereum address of the account whose balance data is being retrieved.
:type blockchain_address: str, 0x-hex
:return: The operational balance of the account as per cached balance data.
:rtype: float
:raises CachedDataNotFoundError
"""
key = create_cached_data_key(
identifier=bytes.fromhex(blockchain_address[2:]),
@ -84,7 +87,7 @@ def get_cached_operational_balance(blockchain_address: str):
)
cached_balance = get_cached_data(key=key)
if cached_balance:
operational_balance = compute_operational_balance(balances=json.loads(cached_balance))
return operational_balance
balance = operational_balance(balances=json.loads(cached_balance))
return balance
else:
raise CachedDataNotFoundError('Cached operational balance not found.')