feat: multi-tariff decimal scaling engine with overflow protection (#3) - #87
Merged
elizabetheonoja-art merged 1 commit intoJun 25, 2026
Conversation
…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
approved these changes
Jun 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Multi-Tariff Decimal Scaling Engine Using Big-Number Arithmetic (#3)
Closes #3
Changes (
src/utils/math.ts)validateScale(value): { valid, reason? }— regex + BigNumber checks:10^12.Surfaces
0.00000001as invalid rather than rounding it to zero.toContractUnits(value, { strict? })— explicitly truncates excess precision (ROUND_DOWN, no silent round-up), throwsOverflowErrorpast i128, and guards invalid numeric input.strict: truerejects values that failvalidateScale.fromContractUnits(value)— pure 7-decimal descaling.addSafe/subtractSafe/multiplySafe/divideSafe— throwOverflowErroron i128 overflow and on divide-by-zero.I128_MAX,I128_MIN,MAX_INTEGER_PART,OverflowError,ScaleValidationError(plus existingSCALE/SCALE_FACTOR). The original helper surface (add/subtract/compare/formatDisplay/…) is preserved.