MathHelpers<V>: {
    doAdd: ((left: V, right: V) => V);
    doCoerce: ((allegedValue: V) => V);
    doIsEmpty: ((value: V) => boolean);
    doIsEqual: ((left: V, right: V) => boolean);
    doIsGTE: ((left: V, right: V) => boolean);
    doMakeEmpty: (() => V);
    doSubtract: ((left: V, right: V) => V);
}

All of the difference in how digital asset amount are manipulated can be reduced to the behavior of the math on values. We extract this custom logic into mathHelpers. MathHelpers are about value arithmetic, whereas AmountMath is about amounts, which are the values labeled with a brand. AmountMath use mathHelpers to do their value arithmetic, and then brand the results, making a new amount.

The MathHelpers are designed to be called only from AmountMath, and so all methods but coerce can assume their inputs are valid. They only need to do output validation, and only when there is a possibility of invalid output.

Type Parameters

Type declaration

  • doAdd: ((left: V, right: V) => V)

    Return the left combined with the right.

      • (left, right): V
      • Parameters

        • left: V
        • right: V

        Returns V

  • doCoerce: ((allegedValue: V) => V)

    Check the kind of this value and throw if it is not the expected kind.

      • (allegedValue): V
      • Parameters

        • allegedValue: V

        Returns V

  • doIsEmpty: ((value: V) => boolean)

    Is the value the identity element?

      • (value): boolean
      • Parameters

        • value: V

        Returns boolean

  • doIsEqual: ((left: V, right: V) => boolean)

    Does left equal right?

      • (left, right): boolean
      • Parameters

        • left: V
        • right: V

        Returns boolean

  • doIsGTE: ((left: V, right: V) => boolean)

    Is the left greater than or equal to the right?

      • (left, right): boolean
      • Parameters

        • left: V
        • right: V

        Returns boolean

  • doMakeEmpty: (() => V)

    Get the representation for the identity element (often 0 or an empty array)

      • (): V
      • Returns V

  • doSubtract: ((left: V, right: V) => V)

    Return what remains after removing the right from the left. If something in the right was not in the left, we throw an error.

      • (left, right): V
      • Parameters

        • left: V
        • right: V

        Returns V