Skip to content

Issue 3: Multi-Tariff Decimal Scaling Engine Using Big-Number Arithmetic #3

Description

@elizabetheonoja-art

Scope

File: src/utils/math.ts

Problem

Soroban contract states are stored as 7-decimal fixed-point integers (scaled by 10^7). The current toContractUnits / fromContractUnits conversions lack overflow protection, do not validate input ranges, and silently truncate beyond 7 decimals. When providers author tariffs with rates like 0.00000001 (1e-8), the engine incorrectly rounds to zero.

Requirements

  1. Enforce a maximum of 7 decimal places on input — reject or explicitly truncate values with more precision.
  2. Add validateScale(value: string): { valid: boolean; reason?: string } that checks:
    • No more than 7 digits after the decimal point
    • No exponential notation unless it represents a safe integer
    • Total integer part does not exceed 10^12 (safe bound for i128)
  3. Expose toContractUnits and fromContractUnits as pure functions with full test coverage.
  4. Add addSafe, subtractSafe, multiplySafe, divideSafe wrappers that throw on overflow.
  5. Write unit tests in src/__tests__/math.test.ts covering edge cases: zero, negative, max i128, very small values.

Resolution Strategy

  • Use BigNumber.config({ DECIMAL_PLACES: 20, ROUNDING_MODE: BigNumber.ROUND_HALF_UP }).
  • Add regex-based validation before conversion.
  • Export SCALE = 7 and SCALE_FACTOR constants.

Tags

utils, tariffs, math, precision

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions