Amount is a value object representing an amount of given Asset.

Remarks

Amount hides all the complexity of dealing with the specific precision constraints of different assets. It offers a consistent interface to perform arithmetic operations on amounts.

Amount is immutable. All arithmetic operations return a new Amount instance.

Type Parameters

Properties

asset: T

Methods

  • Creates a new Amount using the rate: Amount<C> as conversion factor.

    The new Amount will have the Asset of the rate parameter.

    Type Parameters

    Parameters

    Returns Amount<C>

    Example

    Create the Fiat equivalent of an Ether Amount given the ETH-FIAT rate:

    const etherAmount = Amount.ether('1'); // Amount<Ether>

    const fiatAsset = fiat({ name: 'US Dollar', symbol: 'USD' }); // Fiat

    const rate = Amount.fiat(fiatAsset, '0.0006'); // Amount<Fiat>

    const fiatAmount = etherAmount.convert(rate); // Amount<Fiat>
  • Formats the Amount value using fixed-point notation.

    Optionally you can specify the number of decimals to return.

    Parameters

    • decimals: number = ...

    Returns string

    the internal value as string truncated at this Amount Asset max precision.

  • Converts the Amount value as less safe JS number.

    Use at your own risk.

    Type coercion with, for example, JavaScript's unary plus operator will also work.

    Returns number

    the internal value as number that has the potential to lose precision.

  • Formats the Amount value to its maximum precision using a fixed-point notation.

    Optionally you can specify the number of significant digits to approximate the value to.

    Parameters

    • significantDigits: number = ...

    Returns string

    the internal value as string truncated at this Amount Asset max precision.