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
- Enforce a maximum of 7 decimal places on input — reject or explicitly truncate values with more precision.
- 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)
- Expose
toContractUnits and fromContractUnits as pure functions with full test coverage.
- Add
addSafe, subtractSafe, multiplySafe, divideSafe wrappers that throw on overflow.
- 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
Scope
File:
src/utils/math.tsProblem
Soroban contract states are stored as 7-decimal fixed-point integers (scaled by 10^7). The current
toContractUnits/fromContractUnitsconversions lack overflow protection, do not validate input ranges, and silently truncate beyond 7 decimals. When providers author tariffs with rates like0.00000001(1e-8), the engine incorrectly rounds to zero.Requirements
validateScale(value: string): { valid: boolean; reason?: string }that checks:toContractUnitsandfromContractUnitsas pure functions with full test coverage.addSafe,subtractSafe,multiplySafe,divideSafewrappers that throw on overflow.src/__tests__/math.test.tscovering edge cases: zero, negative, max i128, very small values.Resolution Strategy
BigNumber.config({ DECIMAL_PLACES: 20, ROUNDING_MODE: BigNumber.ROUND_HALF_UP }).SCALE = 7andSCALE_FACTORconstants.Tags
utils, tariffs, math, precision