# Copyright (C) 2015-2022 Clearmatics Technologies Ltd - All Rights Reserved.

"""
Utilities related to formatting token and Auton balances.
Constants related to known token precisions.
"""

from decimal import Decimal
from web3.types import Wei


AUTON_DECIMALS = 18

# TODO: Change this to 18 once the Autonity node supports that.
NEWTON_DECIMALS = 18


def format_quantity(units: int, decimals: int) -> str:
    """
    Given some quantity of atomic "token units" of a currency
    (e.g. Attoton for Auton), and the decimals used to represent the
    value of such a unit, return a string representation of the number
    of tokens.
    """
    adjusted = Decimal(units) * pow(Decimal(10), -decimals)
    return f"{adjusted:f}"


def format_auton_quantity(wei: Wei) -> str:
    """
    Format a quantity of Wei as a string representing a quantity of
    Auton, with decimal places.
    """
    return format_quantity(wei, AUTON_DECIMALS)


def format_newton_quantity(units: int) -> str:
    """
    Format a quantity of Newton units as a string representing a
    quantity of whole Newton, with decimal places.
    """
    return format_quantity(units, NEWTON_DECIMALS)
