Makes decimal explicitly declared for transactional operations.
This commit is contained in:
parent
be31de0a8a
commit
02f27cd5e8
@ -1,7 +1,6 @@
|
|||||||
# standard import
|
# standard import
|
||||||
import decimal
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
|
from math import trunc
|
||||||
from typing import Dict, Tuple
|
from typing import Dict, Tuple
|
||||||
|
|
||||||
# external import
|
# external import
|
||||||
@ -9,8 +8,6 @@ from cic_eth.api import Api
|
|||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
# local import
|
# local import
|
||||||
from cic_ussd.account.chain import Chain
|
|
||||||
from cic_ussd.account.tokens import get_cached_default_token
|
|
||||||
from cic_ussd.db.models.account import Account
|
from cic_ussd.db.models.account import Account
|
||||||
from cic_ussd.db.models.base import SessionBase
|
from cic_ussd.db.models.base import SessionBase
|
||||||
from cic_ussd.error import UnknownUssdRecipient
|
from cic_ussd.error import UnknownUssdRecipient
|
||||||
@ -55,32 +52,32 @@ def aux_transaction_data(preferred_language: str, transaction: dict) -> dict:
|
|||||||
return transaction
|
return transaction
|
||||||
|
|
||||||
|
|
||||||
def from_wei(value: int) -> float:
|
def from_wei(decimals: int, value: int) -> float:
|
||||||
"""This function converts values in Wei to a token in the cic network.
|
"""This function converts values in Wei to a token in the cic network.
|
||||||
|
:param decimals: The decimals required for wei values.
|
||||||
|
:type decimals: int
|
||||||
:param value: Value in Wei
|
:param value: Value in Wei
|
||||||
:type value: int
|
:type value: int
|
||||||
:return: SRF equivalent of value in Wei
|
:return: SRF equivalent of value in Wei
|
||||||
:rtype: float
|
:rtype: float
|
||||||
"""
|
"""
|
||||||
cached_token_data = json.loads(get_cached_default_token(Chain.spec.__str__()))
|
value = float(value) / (10**decimals)
|
||||||
token_decimals: int = cached_token_data.get('decimals')
|
|
||||||
value = float(value) / (10**token_decimals)
|
|
||||||
return truncate(value=value, decimals=2)
|
return truncate(value=value, decimals=2)
|
||||||
|
|
||||||
|
|
||||||
def to_wei(value: int) -> int:
|
def to_wei(decimals: int, value: int) -> int:
|
||||||
"""This functions converts values from a token in the cic network to Wei.
|
"""This functions converts values from a token in the cic network to Wei.
|
||||||
|
:param decimals: The decimals required for wei values.
|
||||||
|
:type decimals: int
|
||||||
:param value: Value in SRF
|
:param value: Value in SRF
|
||||||
:type value: int
|
:type value: int
|
||||||
:return: Wei equivalent of value in SRF
|
:return: Wei equivalent of value in SRF
|
||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
cached_token_data = json.loads(get_cached_default_token(Chain.spec.__str__()))
|
return int(value * (10**decimals))
|
||||||
token_decimals: int = cached_token_data.get('decimals')
|
|
||||||
return int(value * (10**token_decimals))
|
|
||||||
|
|
||||||
|
|
||||||
def truncate(value: float, decimals: int):
|
def truncate(value: float, decimals: int) -> float:
|
||||||
"""This function truncates a value to a specified number of decimals places.
|
"""This function truncates a value to a specified number of decimals places.
|
||||||
:param value: The value to be truncated.
|
:param value: The value to be truncated.
|
||||||
:type value: float
|
:type value: float
|
||||||
@ -89,9 +86,8 @@ def truncate(value: float, decimals: int):
|
|||||||
:return: The truncated value.
|
:return: The truncated value.
|
||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
decimal.getcontext().rounding = decimal.ROUND_DOWN
|
stepper = 10.0**decimals
|
||||||
contextualized_value = decimal.Decimal(value)
|
return trunc(stepper*value) / stepper
|
||||||
return round(contextualized_value, decimals)
|
|
||||||
|
|
||||||
|
|
||||||
def transaction_actors(transaction: dict) -> Tuple[Dict, Dict]:
|
def transaction_actors(transaction: dict) -> Tuple[Dict, Dict]:
|
||||||
@ -166,14 +162,16 @@ class OutgoingTransaction:
|
|||||||
self.from_address = from_address
|
self.from_address = from_address
|
||||||
self.to_address = to_address
|
self.to_address = to_address
|
||||||
|
|
||||||
def transfer(self, amount: int, token_symbol: str):
|
def transfer(self, amount: int, decimals: int, token_symbol: str):
|
||||||
"""This function initiates standard transfers between one account to another
|
"""This function initiates standard transfers between one account to another
|
||||||
:param amount: The amount of tokens to be sent
|
:param amount: The amount of tokens to be sent
|
||||||
:type amount: int
|
:type amount: int
|
||||||
|
:param decimals: The decimals for the token being transferred.
|
||||||
|
:type decimals: int
|
||||||
:param token_symbol: ERC20 token symbol of token to send
|
:param token_symbol: ERC20 token symbol of token to send
|
||||||
:type token_symbol: str
|
:type token_symbol: str
|
||||||
"""
|
"""
|
||||||
self.cic_eth_api.transfer(from_address=self.from_address,
|
self.cic_eth_api.transfer(from_address=self.from_address,
|
||||||
to_address=self.to_address,
|
to_address=self.to_address,
|
||||||
value=to_wei(value=amount),
|
value=to_wei(decimals=decimals, value=amount),
|
||||||
token_symbol=token_symbol)
|
token_symbol=token_symbol)
|
||||||
|
Loading…
Reference in New Issue
Block a user