Skip to content

feat: multi-tariff decimal scaling engine with overflow protection (#3) - #87

Merged
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
real-venus:feat/decimal-scaling-math
Jun 25, 2026
Merged

feat: multi-tariff decimal scaling engine with overflow protection (#3)#87
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
real-venus:feat/decimal-scaling-math

Conversation

@real-venus

Copy link
Copy Markdown
Contributor

Multi-Tariff Decimal Scaling Engine Using Big-Number Arithmetic (#3)

Closes #3

Changes (src/utils/math.ts)

  • validateScale(value): { valid, reason? } — regex + BigNumber checks:
    • ≤ 7 digits after the decimal point,
    • exponential notation only when it denotes a safe integer (so fractions can't smuggle in sub-1e-7 precision),
    • integer part ≤ 10^12.
      Surfaces 0.00000001 as invalid rather than rounding it to zero.
  • toContractUnits(value, { strict? }) — explicitly truncates excess precision (ROUND_DOWN, no silent round-up), throws OverflowError past i128, and guards invalid numeric input. strict: true rejects values that fail validateScale.
  • fromContractUnits(value) — pure 7-decimal descaling.
  • addSafe / subtractSafe / multiplySafe / divideSafe — throw OverflowError on i128 overflow and on divide-by-zero.
  • New exports: I128_MAX, I128_MIN, MAX_INTEGER_PART, OverflowError, ScaleValidationError (plus existing SCALE/SCALE_FACTOR). The original helper surface (add/subtract/compare/formatDisplay/…) is preserved.

…tility-Protocol#3)

Hardens src/utils/math.ts for 7-decimal Soroban fixed-point conversions: input
range validation, explicit truncation instead of silent rounding, and i128
overflow guards.

- validateScale(value): rejects >7 decimal places, exponential notation that
  isn't a safe integer, and integer parts beyond 10^12 (the round-to-zero bug
  for rates like 0.00000001 is now surfaced)
- toContractUnits: explicitly truncates excess precision (ROUND_DOWN), supports
  a strict mode that rejects invalid scale, and throws OverflowError past i128;
  guards invalid numeric input
- fromContractUnits: pure 7-decimal descaling
- addSafe/subtractSafe/multiplySafe/divideSafe: throw OverflowError on i128
  overflow and on divide-by-zero
- adds I128_MAX/I128_MIN/MAX_INTEGER_PART constants and OverflowError/
  ScaleValidationError; preserves the existing helper surface
- tests cover zero, negative, max i128, very small values, exponential forms,
  truncation, overflow and round-trips (placed under tests/unit since vitest
  only collects tests/**, not src/__tests__)
@elizabetheonoja-art
elizabetheonoja-art merged commit 53a8c95 into Utility-Protocol:main Jun 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants