aftermath-ts-sdk - v3.3.3
    Preparing search index...

    Provides fixed-point conversions and token-decimal normalization.

    Direct fixed-point conversions use an 18-decimal scale. Normalization uses the caller-supplied token decimal scalar and keeps on-chain amounts as bigint values until a direct cast produces a JavaScript number.

    Index
    fixedOneB: bigint = ...

    The exact bigint scale for 18-decimal fixed-point values, 1000000000000000000n.

    fixedOneB9: bigint = ...

    The exact bigint scale for 9-decimal fixed-point values, 1000000000n.

    fixedOneN: number = 1_000_000_000_000_000_000

    The JavaScript-number scale for 18-decimal fixed-point values, 1e18.

    JavaScript numbers do not represent every integer at this magnitude exactly.

    fixedOneN9: 1000000000 = 1_000_000_000

    The JavaScript-number scale for 9-decimal fixed-point values, 1e9.

    • Normalizes a raw balance and converts it to the 18-decimal local scale.

      The method multiplies amount by decimalsScalar, converts the product to a JavaScript number, and divides by fixedOneN. Large products can lose precision during the number conversion.

      Parameters

      • decimalsScalar: bigint

        The token's scale factor as a bigint.

      • amount: bigint

        The raw on-chain balance.

      Returns number

      The normalized local value as a number.

    • Returns a clamped complement of a local number.

      For 0 <= n <= 1, the result is 1 - n. Negative inputs are treated as zero before subtraction, and inputs greater than 1 produce 0 after the result is clamped. NaN is not validated and produces NaN.

      Parameters

      • n: number

        The local number to complement.

      Returns number

      A value no less than 0 for finite inputs.

    • Converts a raw on-chain bigint to a JavaScript number without scaling.

      Values larger than JavaScript's safe integer range can lose precision.

      Parameters

      • n: bigint

        The raw on-chain integer.

      Returns number

      The raw value as a JavaScript number.

    • Converts a JavaScript number to a raw on-chain bigint without scaling.

      The method applies Math.floor, so negative fractional values round toward negative infinity rather than toward zero. NaN and infinite values throw during the bigint conversion.

      Parameters

      • n: number

        The local number to convert.

      Returns bigint

      The floored value as a bigint.

      RangeError when n is not finite.

    • Converts an 18-decimal fixed-point bigint to a JavaScript number.

      The method divides by fixedOneN; large values can lose precision when converted to number.

      Parameters

      • n: bigint

        The 18-decimal fixed-point integer.

      Returns number

      The unscaled local number.

    • Converts a JavaScript number to an 18-decimal fixed-point bigint.

      The method multiplies by fixedOneN and applies Math.floor before the bigint conversion. Negative fractional values therefore floor toward negative infinity, and JavaScript number precision applies before flooring.

      Parameters

      • n: number

        The local number to scale.

      Returns bigint

      The scaled value as a bigint.

      RangeError when the scaled value is not finite.

    • Multiplies a raw balance by a token decimal scalar.

      For a token with 9 decimals, pass 1000000000n as the scalar. The multiplication remains exact because both operands are bigint values.

      Parameters

      • decimalsScalar: bigint

        The token's scale factor, such as 1000000000n.

      • amount: bigint

        The raw on-chain balance.

      Returns bigint

      The normalized balance as a bigint.

    • Converts an 18-decimal local value back to a raw balance.

      The method first multiplies by fixedOneN and floors to a bigint, then divides by decimalsScalar. The first step is subject to JavaScript number precision; the second step discards any bigint remainder toward zero.

      Parameters

      • decimalsScalar: bigint

        The token's scale factor as a bigint.

      • normalizedAmount: number

        The local value in the 18-decimal domain.

      Returns bigint

      The raw balance as a bigint.

      RangeError when decimalsScalar is 0n or the scaled number is not finite.

    • Divides a normalized bigint by a token decimal scalar.

      Bigint division discards a remainder toward zero. A zero scalar throws a division-by-zero error.

      Parameters

      • decimalsScalar: bigint

        The token's scale factor as a bigint.

      • normalizedAmount: bigint

        The normalized balance.

      Returns bigint

      The raw balance as a bigint.

      RangeError when decimalsScalar is 0n.