Interface Amount

An amount consists of a value and number of decimals. It wraps a bigint value, and thus has the same limitations as a bigint in terms of size and precision.

The Amount interface also has methods to perform arithmetic operations on the amount with preserved precision.

interface Amount {
    compare: ((other) => number);
    decimals: number;
    dividedBy: ((other, decimals?) => Amount);
    encodeGtv: (() => RawGtv);
    eq: ((other) => boolean);
    equals: ((other) => boolean);
    gt: ((other) => boolean);
    gte: ((other) => boolean);
    lt: ((other) => boolean);
    lte: ((other) => boolean);
    minus: ((other) => Amount);
    plus: ((other) => Amount);
    times: ((other, decimals?) => Amount);
    toString: ((removeTrailingZeroes?) => string);
    value: bigint;
    format(which, digits, removeTrailingZeroes?): string;
    format(which, digits, removeTrailingZeroes?, groupDigits?): string;
}

Properties

compare: ((other) => number)

Can be used as compareFn with Array.sort().

Type declaration

    • (other): number
    • Parameters

      Returns number

Returns

0 if other is equal to this 1 if other should come before this when sorted -1 if other should come after this when sorted

decimals: number
dividedBy: ((other, decimals?) => Amount)

Produces a new amount which is the quotient of this amount and the specified value (divisor), optionally specifying the number of decimals the end result should have.

Type declaration

    • (other, decimals?): Amount
    • Parameters

      • other: SupportedNumber

        the value to divide this amount with

      • Optional decimals: number | "max"

        the number of decimals the end result should have, or "max" for the highest value between this.decimals and other.decimals, that is Math.max(this.decimals, other.decimals). Defaults to this.decimals

      Returns Amount

Returns

new amount with the value "this amount" / "other amount"

encodeGtv: (() => RawGtv)

Serializes this amount to its gtv representation to be used when submitting it to the blockchain

Type declaration

    • (): RawGtv
    • Returns RawGtv

eq: ((other) => boolean)

Compares this amount with another value and returns true if this amount is equal to the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

equals: ((other) => boolean)

Compares this amount with another value and returns true if this amount is equal to the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

gt: ((other) => boolean)

Compares this amount with another value and returns true if this amount is greater than the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

gte: ((other) => boolean)

Compares this amount with another value and returns true if this amount is greater than or equal to the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

lt: ((other) => boolean)

Compares this amount with another value and returns true if this amount is lesser than the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

lte: ((other) => boolean)

Compares this amount with another value and returns true if this amount is lesser than or equal to the other value

Type declaration

    • (other): boolean
    • Parameters

      Returns boolean

minus: ((other) => Amount)

Produces a new amount which is the difference of this amount and the specified value

Type declaration

Returns

new amount with the value "this amount" - "other amount"

plus: ((other) => Amount)

Produces a new amount which is the sum of this amount and the specified value

Type declaration

Returns

new amount with the value "this amount" + "other amount"

times: ((other, decimals?) => Amount)

Produces a new amount which is the product of this amount and the specified value, optionally specifying the number of decimals the end result should have.

Type declaration

    • (other, decimals?): Amount
    • Parameters

      • other: SupportedNumber

        the value to multiply this amount with

      • Optional decimals: number | "max"

        the number of decimals the end result should have, or "max" for the highest value between this.decimals and other.decimals, that is Math.max(this.decimals, other.decimals). Defaults to this.decimals

      Returns Amount

Returns

new amount with the value "this amount" * "other amount"

toString: ((removeTrailingZeroes?) => string)

To be used if you want the precise value, down to the last digit. It must return a string, as a Number could still be overflowed and floating point numbers aren't precise in JS.

Type declaration

    • (removeTrailingZeroes?): string
    • Parameters

      • Optional removeTrailingZeroes: boolean

        if true, trailing zeroes will be removed (0.800 -> 0.8). Defaults to true.

      Returns string

value: bigint

Methods

  • Returns the amount formatted as a readable string

    Parameters

    • which: scientific

      the DecimalFormat to use

    • digits: number

      the number of digits to display

    • Optional removeTrailingZeroes: boolean

      if true, trailing zeroes will be removed (0.800 -> 0.8)

    Returns string

    The formatted string. WILL LOSE LOWER DIGIT PRECISION

    Example

    scientific, 3-digits: 1.00e+18, 2.47e+3, 5.70e+0, 2.30e-4
    fixed decimals, 3-digits: 13.750, 1987352342.435, 0.000, 12
    mixed, 3-digits: 1.00e+18, 1.23e+3, 345, 12.0, 1.45e-1, 1.15e-2 !!NOT 0.01!!
  • returns the amount formatted as a readable string

    Parameters

    • which: fixedDecimals | mixed

      the DecimalFormat to use

    • digits: number

      the number of digits to display

    • Optional removeTrailingZeroes: boolean

      if true, trailing zeroes will be removed (0.800 -> 0.8)

    • Optional groupDigits: boolean

      if true, when the format is not scientific, digits will be grouped by three (1234.5678 -> 1 234.567 8)

    Returns string

    The formatted string. WILL LOSE LOWER DIGIT PRECISION

    Example

    scientific, 3-digits: 1.00e+18, 2.47e+3, 5.70e+0, 2.30e-4
    fixed decimals, 3-digits: 13.750, 1987352342.435, 0.000, 12
    mixed, 3-digits: 1.00e+18, 1.23e+3, 345, 12.0, 1.45e-1, 1.15e-2 !!NOT 0.01!!