Class Amount

This module contains the 'Amount' class, which is a useful representation of a Currency value.

Remarks

The 'Amount' class provides the following methods add and subtract to perform basic arithmetic operations between amounts of the same currency.

It also provides the following comparison methods lessThan,equals and notEqual.

Best practice: Before manipulating several Amount objects, you should always check if they use the same Currency.

Example

const dollar = new Currency("dollar", 2);
const price = new Amount(1034, dollar);

Hierarchy

  • Amount

Implements

Constructors

  • Creates a new Amount.

    Parameters

    • value: number = 0

      The amount value.

    • currency: Currency = ...

      The currency (optional).

    Returns Amount

Properties

currency: Currency = ...

The currency (optional).

value: number = 0

The amount value.

Methods

  • Adds two amounts and return results in a new one.

    Parameters

    • other: Amount

      The other Amount to add.

    Returns Result<Amount>

    A Result object containing:

    • The new Amount if operation was successful.
    • An error message if:
      • the Amount objects are not the same.
      • the addition would overflow of the 'u64.MAX_VALUE' limit.

    See

    Result for more information about the return type.

  • Deserializes an Amount from an 'array of bytes'.

    Parameters

    • data: StaticArray<number>

      The 'array of bytes' to deserialize.

    • offset: number

      The position in the 'array of bytes' to start reading from.

    Returns Result<number>

    Result containing either an the new offset of the byte array or an Error.

  • Tests if two amounts are identical.

    Parameters

    • other: Amount

      Amount to check against.

    Returns boolean

    true if the amounts are identical.

    Remarks

    Two amounts are identical if they have the same value as well as the same currency type!

  • Check if existent amount is greater than given one.

    Parameters

    • other: Amount

      Amount to check against.

    Returns bool

    true if the amount is greater than the given one.

    Remarks

    Comparison between amounts with different Currency will result in returning false.

  • Check if existent amount is lower than given one.

    Parameters

    • other: Amount

      Amount to check against.

    Returns bool

    true if the amount is lower than the given one.

    Remarks

    Comparison between amounts with different Currency will result in returning false.

  • Tests if two amounts are different.

    Parameters

    • other: Amount

      Amount to check against.

    Returns boolean

    true if the amounts are different.

    Remarks

    Two amounts are different if they have the same currency type even if they have the same value!

  • Serializes an Amount into an 'array of bytes'.

    Returns StaticArray<number>

    the serialized data as a 'StaticArray'.

  • Subtracts two amounts and return results in a new one.

    Parameters

    • other: Amount

      Amount to subtract.

    Returns Result<Amount>

    A Result object containing:

    • The new Amount if the operation was successful.
    • An error message if:
      • the currency is not the same between the two amounts.
      • the subtraction would underflow .

    See

    Result for more information about the return type.

Generated using TypeDoc