nectar.amount module

class nectar.amount.Amount(amount: str | int | float | Decimal | list | dict | Amount, asset: str | Asset | None = None, fixed_point_arithmetic: bool = False, new_appbase_format: bool = True, blockchain_instance: Any = None, json_str: bool = False, **kwargs)

Bases: dict

This class deals with Amounts of any asset to simplify dealing with the tuple:

(amount, asset)
Parameters:
  • args (list) – Allows to deal with different representations of an amount

  • amount (float) – Let’s create an instance with a specific amount

  • asset (str) – Let’s you create an instance with a specific asset (symbol)

  • fixed_point_arithmetic (boolean) – when set to True, all operations are fixed point operations and the amount is always be rounded down to the precision

  • blockchain_instance (Blockchain) – Blockchain instance

Returns:

All data required to represent an Amount/Asset

Return type:

dict

Raises:

ValueError – if the data provided is not recognized

Way to obtain a proper instance:

  • args can be a string, e.g.: “1 HBD”

  • args can be a dictionary containing amount and asset_id

  • args can be a dictionary containing amount and asset

  • args can be a list of a float and str (symbol)

  • args can be a list of a float and a nectar.asset.Asset

  • amount and asset are defined manually

An instance is a dictionary and comes with the following keys:

Instances of this class can be used in regular mathematical expressions (+-*/%) such as:

from nectar.amount import Amount
from nectar.asset import Asset
a = Amount("1 HIVE")
b = Amount(1, "HIVE")
c = Amount("20", Asset("HIVE"))
a + b
a * 2
a += b
a /= 2.0
2.000 HIVE
2.000 HIVE
property amount: float

Returns the amount as float

property amount_decimal: Decimal

Returns the amount as decimal

as_tuple() Tuple[float, str]
property asset: Asset

Return the Asset object for this Amount, constructing it lazily if missing.

If the internal ‘asset’ entry is falsy, this creates a nectar.asset.Asset using the stored symbol and this Amount’s blockchain instance, stores it in ‘asset’, and returns it. Always returns an Asset instance.

copy() Amount

Copy the instance and make sure not to use a reference

json() str | dict | list
property symbol: str

Returns the symbol of the asset

nectar.amount.check_asset(other: Any, self: Any, hv: Any) None

Assert that two asset representations refer to the same asset.

If both other and self are dicts containing an “asset” key, each asset id is wrapped in an Asset using the provided blockchain instance and compared for equality. Otherwise the two values are compared directly. Raises AssertionError if the values do not match.

nectar.amount.quantize(amount: str | int | float | Decimal, precision: int) Decimal