Skip to content

fix(backend): round owed amounts via half-even policy in loanController.ts roundToCents (#1372) - #1585

Merged
ogazboiz merged 9 commits into
LabsCrypt:mainfrom
jotel-dev:#1372-backend-owed-values-are-always-rounded-down
Aug 10, 2026
Merged

fix(backend): round owed amounts via half-even policy in loanController.ts roundToCents (#1372)#1585
ogazboiz merged 9 commits into
LabsCrypt:mainfrom
jotel-dev:#1372-backend-owed-values-are-always-rounded-down

Conversation

@jotel-dev

Copy link
Copy Markdown
Contributor

Fix: Use money policy half-even rounding in loanController roundToCents (#1372)

Description

Fixes issue #1372, where roundToCents in backend/src/controllers/loanController.ts previously used Math.floor, causing owed amounts and amortization portions to be systematically floored down (undercharging borrowers/protocol).

Replacing Math.floor with standard Math.round would have violated the repository's money policy (backend/src/money/policy.generated.ts), which defines half_even (banker's rounding) for settlement and balance calculations.

This PR delegates cent rounding to the central money module (backend/src/money/decimal.ts) using half_even tie-breaking.

Key Changes

  • Money Module (backend/src/money/decimal.ts):
    • Added roundToCents(value: number, mode?: RoundingMode) helper which converts floating point values into stroops (toStroops) and divides by 100_000n using roundDiv under half_even mode.
  • Loan Controller (backend/src/controllers/loanController.ts):
    • Replaced the local Math.floor implementation of roundToCents with the money policy's half_even roundToCents function.
  • Unit Tests:
    • Added backend/src/__tests__/roundToCents.test.ts and updated backend/src/money/__tests__/decimal.test.ts to cover .5 tie-breaking half-even scenarios (e.g., 0.1250.12, 0.1350.14, 10.00510.00, -0.125-0.12).
    • Fixed jest globals import in backend/src/__tests__/loanConfig.test.ts.

Pull Request Checklist

  • I have read the CONTRIBUTING.md document.
  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation accordingly.
  • I have verified the changes locally.

Verification

Executed local suite verifications:

npm run typecheck  # Passed cleanly with 0 errors
npm run lint       # ESLint passed cleanly
npm test           # All 53 test suites & 335 tests passed


closes #1372 

@ogazboiz
ogazboiz merged commit d3598f1 into LabsCrypt:main Aug 10, 2026
14 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.

2 participants