Edit on GitHub

communex.balance

 1from typing import Any, TypeVar
 2
 3DECIMALS = 9
 4UNIT_NAME = "COMAI"
 5
 6
 7def from_nano(amount: int) -> float:
 8    """
 9    Converts from nano to j
10    """
11
12    return amount / (10**DECIMALS)
13
14
15def to_nano(amount: float) -> int:
16    """
17    Converts from j to nano
18    """
19
20    return int(amount * (10**DECIMALS))
21
22
23def from_horus(amount: int, subnet_tempo: int = 100) -> float:
24    """
25    Converts from horus to j
26    """
27
28    return amount / (10**DECIMALS * subnet_tempo)
29
30
31def repr_j(amount: int):
32    """
33    Given an amount in nano, returns a representation of it in tokens/COMAI.
34
35    E.g. "103.2J".
36    """
37
38    return f"{from_nano(amount)} {UNIT_NAME}"
39
40
41T = TypeVar("T")
42def dict_from_nano(dict_data: dict[T, Any], fields_to_convert: list[T]):
43    """
44    Converts specified fields in a dictionary from nano to J. Only works for
45    fields that are integers. Fields not found are silently ignored.
46    Recursively searches nested dictionaries.
47    """
48    transformed_dict: dict[T, Any] = {}
49    for key, value in dict_data.items():
50        if isinstance(value, dict):
51            transformed_dict[key] = dict_from_nano(value, fields_to_convert) # type: ignore
52        elif key in fields_to_convert:
53            if not (isinstance(value, int) or value is None):
54                raise ValueError(
55                    f"Field {key} is not an integer in the dictionary."
56                )
57            transformed_dict[key] = repr_j(value)
58        else:
59            transformed_dict[key] = value
60
61    return transformed_dict
DECIMALS = 9
UNIT_NAME = 'COMAI'
def from_nano(amount: int) -> float:
 8def from_nano(amount: int) -> float:
 9    """
10    Converts from nano to j
11    """
12
13    return amount / (10**DECIMALS)

Converts from nano to j

def to_nano(amount: float) -> int:
16def to_nano(amount: float) -> int:
17    """
18    Converts from j to nano
19    """
20
21    return int(amount * (10**DECIMALS))

Converts from j to nano

def from_horus(amount: int, subnet_tempo: int = 100) -> float:
24def from_horus(amount: int, subnet_tempo: int = 100) -> float:
25    """
26    Converts from horus to j
27    """
28
29    return amount / (10**DECIMALS * subnet_tempo)

Converts from horus to j

def repr_j(amount: int):
32def repr_j(amount: int):
33    """
34    Given an amount in nano, returns a representation of it in tokens/COMAI.
35
36    E.g. "103.2J".
37    """
38
39    return f"{from_nano(amount)} {UNIT_NAME}"

Given an amount in nano, returns a representation of it in tokens/COMAI.

E.g. "103.2J".

def dict_from_nano(dict_data: dict[~T, typing.Any], fields_to_convert: list[~T]):
43def dict_from_nano(dict_data: dict[T, Any], fields_to_convert: list[T]):
44    """
45    Converts specified fields in a dictionary from nano to J. Only works for
46    fields that are integers. Fields not found are silently ignored.
47    Recursively searches nested dictionaries.
48    """
49    transformed_dict: dict[T, Any] = {}
50    for key, value in dict_data.items():
51        if isinstance(value, dict):
52            transformed_dict[key] = dict_from_nano(value, fields_to_convert) # type: ignore
53        elif key in fields_to_convert:
54            if not (isinstance(value, int) or value is None):
55                raise ValueError(
56                    f"Field {key} is not an integer in the dictionary."
57                )
58            transformed_dict[key] = repr_j(value)
59        else:
60            transformed_dict[key] = value
61
62    return transformed_dict

Converts specified fields in a dictionary from nano to J. Only works for fields that are integers. Fields not found are silently ignored. Recursively searches nested dictionaries.