UNPKG

@stabilis/c9-shape-liquidity-getter

Version:

A library for calculating redemption values of concentrated liquidity positions for C9 shape liquidity.

131 lines (130 loc) 4.66 kB
import { Decimal } from "decimal.js"; /** * I192 class to mimic Scrypto Decimal's I192 behavior * * This class handles 192-bit representation of fixed-scale decimal numbers with 18 decimal places, * matching Scrypto's Decimal type as closely as possible. * * Key features: * 1. Represents numbers with exactly 18 decimal places of precision * 2. Performs truncation (toward zero) after each operation to match Scrypto's behavior * 3. Enforces the same value range as Scrypto's Decimal type * 4. Always outputs values with exactly 18 decimal places * * Truncation vs. Rounding: * - Scrypto's Decimal type truncates values toward zero after each operation * - For positive numbers, this means flooring the value (removing all digits beyond 18 decimals) * - For negative numbers, this means ceiling the value (removing all digits beyond 18 decimals) * - This is different from banker's rounding (which rounds to nearest, with ties to even) * - Truncation creates consistent, predictable behavior for financial calculations */ export declare class I192 { private static readonly DECIMALS; private static readonly DECIMAL_FACTOR; private static readonly MAX_VALUE; private static readonly MIN_VALUE; private value; /** * Constructor * @param value A string, number, or Decimal value */ constructor(value: string | number | Decimal); /** * Truncate a value to exactly 18 decimal places (floor for positive, ceiling for negative) * This mimics Scrypto's behavior of truncation toward zero after each operation. * * Examples: * 123.4567890123456789123 -> 123.456789012345678900 (truncated) * -123.4567890123456789123 -> -123.456789012345678900 (truncated) */ private truncateToDecimals; /** * Creates a new I192 instance from a value */ static from(value: string | number | Decimal | I192): I192; /** * Creates an I192 with value 0 */ static zero(): I192; /** * Creates an I192 with value 1 */ static one(): I192; /** * Addition with intermediate truncation * * Both the input and the result are truncated to 18 decimal places, * mimicking Scrypto's behavior of truncating after each operation. */ add(other: I192 | string | number | Decimal): I192; /** * Subtraction with intermediate truncation * * Both the input and the result are truncated to 18 decimal places, * mimicking Scrypto's behavior of truncating after each operation. */ subtract(other: I192 | string | number | Decimal): I192; /** * Multiplication with intermediate truncation * * Both the input and the result are truncated to 18 decimal places, * mimicking Scrypto's behavior of truncating after each operation. */ multiply(other: I192 | string | number | Decimal): I192; /** * Division with intermediate truncation * * Both the input and the result are truncated to 18 decimal places, * mimicking Scrypto's behavior of truncating after each operation. * * This operation is particularly sensitive to truncation, which can * cause different results compared to regular rounding methods. */ divide(other: I192 | string | number | Decimal): I192; /** * Returns true if this value is greater than other */ greaterThan(other: I192 | string | number | Decimal): boolean; /** * Returns true if this value is greater than or equal to other */ greaterThanOrEqualTo(other: I192 | string | number | Decimal): boolean; /** * Returns true if this value is less than other */ lessThan(other: I192 | string | number | Decimal): boolean; /** * Returns true if this value is less than or equal to other */ lessThanOrEqualTo(other: I192 | string | number | Decimal): boolean; /** * Returns true if this value is equal to other */ equals(other: I192 | string | number | Decimal): boolean; /** * Returns true if this value is zero */ isZero(): boolean; /** * Returns true if this value is negative */ isNegative(): boolean; /** * Returns true if this value is positive */ isPositive(): boolean; /** * Returns a string representation with full 18 decimal places * Format matches Scrypto Decimal's exact precision */ toString(): string; /** * Returns the decimal value */ toDecimal(): Decimal; /** * Ensures the value is within the valid I192 range * Throws an error if out of range */ private checkRange; }