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