Skip to content

6. The Currency Module

These are the various functions that are used to view or manipulate the currency module.

6.1 currency(currency_module)

This module prompts you to select the action that you want to do on the currency module. Then it will call the corresponding function and that function takes on.

Source code in modules/currency.py
def currency(currency_module):
    """
    This module prompts you to select the action that you want to do on the currency module. Then it will call the corresponding function and that function takes on.
    """

    # This is the list of the actions that you can do on the currency module.
    actions = prompt([
        {
            'type': 'list',
            'name': 'action',
            'message': 'What do you want to do?',
            'choices': [
                Separator('-- Actions --'),
                {
                    'name': 'See the balance of your account',
                    'value': 'balance',
                    'short': 'Balance'
                },
                {
                    'name': 'Transfer some tokens to another account',
                    'value': 'transfer',
                    'short': 'Transfer'
                },
                {
                    'name': 'Burn some tokens',
                    'value': 'burn',
                    'short': 'Burn'
                },
                {
                    'name': 'See the balance of another account',
                    'value': 'balance_other',
                    'short': 'Balance Other'
                },
                {
                    'name': 'Get the value of a given amount of tokens',
                    'value': 'value',
                    'short': 'Value'
                },
                {
                    'name': 'Get the total supply of tokens',
                    'value': 'total_supply',
                    'short': 'Total Supply'
                },
                {
                    'name': 'Mint some tokens',
                    'value': 'mint',
                    'short': 'Mint'
                }

            ]
        },
        {
            'type': 'input',
            'name': 'address',
            'message': 'Enter the address of the other account',
            'when': lambda answers: answers['action'] == 'balance_other'
        }
    ])

    # Calling the desired function.
    if actions['action'] == 'balance':
        from .currency_token.balance import balance
        balance(currency_module)

    elif actions['action'] == 'balance_other':
        from .currency_token.balance import balance_of
        balance_of(currency_module, actions['address'])

    elif actions['action'] == 'mint':
        from .currency_token.mint import mint
        mint(currency_module)

    elif actions['action'] == 'transfer':
        from .currency_token.transfer import transfer
        transfer(currency_module)

    elif actions['action'] == 'value':
        from .currency_token.value import value
        value(currency_module)

    elif actions['action'] == 'burn':
        from .currency_token.burn import burn
        burn(currency_module)

    elif actions['action'] == 'total_supply':
        from .currency_token.supply import supply
        supply(currency_module)

    else:
        raise Exception('Invalid action')

6.2 balance(currency_module)

This function is used to get your own current balance, i.e, get the number of tokens that you hold.

Source code in currency_token/balance.py
def balance(currency_module) -> None:
    """
    This function is used to get your own current balance, i.e, get the number of tokens that you hold.
    """
    details = get(currency_module)
    print(colored(
        f"The balance of current user is {currency_module.format_units(currency_module.balance(), details['decimals']) + ' ' + details['symbol']}.", 'green'))

6.3 balance_of(currency_module, address)

This module is used to get your someone's current balance, i.e, get the number of tokens that you hold.

Source code in currency_token/balance.py
def balance_of(currency_module, address) -> None:
    """
    This module is used to get your someone's current balance, i.e, get the number of tokens that you hold.
    """
    details = get(currency_module)
    print(colored(
        f"The balance of {address} is {currency_module.format_units(currency_module.balance_of(address), details['decimals']) + ' ' + details['symbol']}.", 'green'))

6.4 burn(currency_module)

This function is used to burn some tokens from your account.

Source code in currency_token/burn.py
def burn(currency_module):
    """
    This function is used to burn some tokens from your account.
    """

    # Get the amount of tokens to burn
    burn_data = prompt([
        {
            'type': 'input',
            'name': 'amount',
            'message': 'Enter the amount of tokens to  burn',
            'default': "1",
        },
        {
            'type': 'confirm',
            'name': 'confirmation',
            'message': 'Do you want to burn the selected tokens?',
            'default': False,
        },
    ])

    # Convert the amount to the correct decimal format
    amount = Decimal(burn_data['amount']) * \
        (10 ** get(currency_module)['decimals'])

    # Confirm the burn
    if burn_data['confirmation']:
        try:
            try:
                currency_module.burn(int(amount))
            except TimeExhausted:
                print(colored(
                    'A time exhaust has been detected. The tokens were most likely burnt. Please verify before trying again.', 'red'))
            print(colored('Tokens burned successfully!', 'green'))

        except Exception as e:
            print(colored('Tokens could not be burned. \n' + e, 'red'))

    else:
        print(colored('Tokens not burned!', 'blue'))

6.5 get(currency_module)

This helper function is used to get the details of the currency.

Source code in currency_token/get.py
def get(currency_module) -> Dict:
    """
    This helper function is used to get the details of the currency.
    """
    details = currency_module.get()
    return {"name": details.name, "symbol": details.symbol, "decimals": details.decimals}

6.6 supply(currency_module)

This function is used to get the total supply of the currency.

Source code in currency_token/supply.py
def supply(currency_module):
    """
    This function is used to get the total supply of the currency.
    """
    try:
        total_supply = Decimal(currency_module.total_supply(
        ) / (10 ** get(currency_module)['decimals']))
        print(colored(
            f"Total supply of {get(currency_module)['symbol']} is {total_supply}", 'green'))

    except Exception as e:
        print(colored('Can\'t get total supply \n' + e, 'red'))

6.7 transfer(currency_module)

This function is used to transfer some tokens from your account to another account.

Source code in currency_token/transfer.py
def transfer(currency_module):
    """
    This function is used to transfer some tokens from your account to another account.
    """
    transfer_data = prompt([
        {
            'type': 'input',
            'name': 'amount',
            'message': 'Enter the amount of tokens to  transfer',
            'default': "1",
        },
        {
            'type': 'input',
            'name': 'address',
            'message': 'Enter the address of the other account',
        },
        {
            'type': 'confirm',
            'name': 'confirmation',
            'message': 'Do you want to transfer the selected tokens?',
            'default': False,
        },
    ])

    amount = Decimal(transfer_data['amount']) * \
        (10 ** get(currency_module)['decimals'])

    if transfer_data['confirmation']:
        try:
            signer_addr = currency_module.get_signer_address()
            currency_module.set_allowance(signer_addr, amount)
            currency_module.transfer_from(signer_addr,
                                          transfer_data['address'], int(amount))
            print(colored('Tokens transferred successfully!', 'green'))

        except Exception as e:
            print(colored('Tokens could not be transferred. \n' + e, 'red'))

6.8 value(currency_module)

This function is used to get the value of the currency.

Source code in currency_token/value.py
def value(currency_module):
    """
    This function is used to get the value of the currency.
    """
    value_data = prompt([
        {
            'type': 'input',
            'name': 'amount',
            'message': 'Enter the amount of tokens to  get the value of',
            'default': "1",
        },
    ])

    amount = Decimal(value_data['amount']) * \
        (10 ** get(currency_module)['decimals'])

    try:
        print(colored(
            f"Value of {value_data['amount']} {get(currency_module)['symbol']}s is {currency_module.get_value(int(amount)).value} and its display value is {currency_module.get_value(int(amount)).display_value}", 'green'))

    except Exception as e:
        print(colored('Can\'t get value \n' + e, 'red'))

Last update: 2022-01-25
Authors: Arpan Pandey