feat: replace floating-point monetary storage with exact BigInt stroops - #145
Merged
3m1n3nc3 merged 1 commit intoAug 21, 2026
Merged
Conversation
…ault#123) Replace all binary floating-point monetary fields with exact-integer stroop values (1 XLM = 10,000,000 stroops) across the Prisma schema, services, controller, and tests. Schema changes (prisma/schema.prisma): - Module.reward Float → rewardStroops BigInt - Transaction.amount Float → amountStroops BigInt - Referral.bonusAmount Float? → bonusAmountStroops BigInt? - Add assetCode / assetIssuer / assetNetwork to Module and Transaction Migration (prisma/migrations/20260821000000_exact_monetary_storage): - Converts legacy float values using ROUND(old * 10_000_000)::BIGINT - Adds CHECK constraints (non-negative stroops) - Wrapped in a BEGIN/COMMIT transaction - Rollback notes documented in SQL comments New utility module (src/utils/money.ts): - xlmToStroops, stroopsToXlmString, xlmStringToStroops - legacyFloatXlmToStroops (explicit rounding, throws on NaN/Infinity/negative) - addStroops, subtractStroops (throws on underflow), multiplyStroops (rational), clampStroops, formatStroops, assertInRange, isValidStroopAmount, MoneyError Service (src/services/reward.service.ts): - All internal amounts use bigint stroops - Difficulty multipliers expressed as [numerator, denominator] rational pairs - Stellar SDK receives 7-decimal XLM strings via stroopsToXlmString Controller (src/controllers/reward.controller.ts): - Parses incoming XLM string amounts via xlmStringToStroops - Serialises bigint → XLM string at every API response boundary Types (src/types/reward.types.ts): - Transaction.amount / Balance fields changed to string (7-decimal XLM) Tests: - tests/unit/money.test.ts: 83 tests covering conversion, rounding, overflow, boundary, arithmetic, and integration scenarios - tests/unit/reward.service.test.ts: fully updated to use BigInt stroops - tests/unit/reward.controller.test.ts: mock data updated to BigInt/XLM string CI: pnpm lint clean, pnpm test:coverage 741 passed / 3 skipped (DB tests) Closes learnault#123
7 tasks
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.
Summary
Closes #123
Replaces all binary floating-point monetary fields with exact-integer stroop values (1 XLM = 10,000,000 stroops) across the Prisma schema, service layer, controller, API types, and tests. This eliminates silent IEEE-754 precision loss in reward calculations and makes asset identity explicit on every monetary record.
Changes
Prisma schema (
prisma/schema.prisma)Modulereward FloatrewardStroops BigInt+assetCode,assetIssuer,assetNetworkTransactionamount FloatamountStroops BigInt+assetCode,assetIssuer,assetNetworkReferralbonusAmount Float?bonusAmountStroops BigInt?Migration (
prisma/migrations/20260821000000_exact_monetary_storage/migration.sql)ROUND(old * 10_000_000)::BIGINT(nearest stroop)CHECK (col >= 0)constraints on all stroop columnsBEGIN / COMMIT— atomicNew utility module (
src/utils/money.ts)xlmToStroops,stroopsToXlmString,xlmStringToStroopslegacyFloatXlmToStroops— explicit rounding, throws onNaN/Infinity/ negativeaddStroops,subtractStroops(throws on underflow),multiplyStroops(rational BigInt),clampStroopsformatStroops,assertInRange,isValidStroopAmount,MoneyErrorService (
src/services/reward.service.ts)bigintstroops throughout[numerator, denominator]rational pairs — no floatssendPaymentreceives 7-decimal XLM strings viastroopsToXlmStringController (
src/controllers/reward.controller.ts)xlmStringToStroops(rejects > 7 decimal places)bigint→ XLM string at every API response boundaryTypes (
src/types/reward.types.ts)Transaction.amount/Balancefields changed fromnumbertostring(7-decimal XLM)Tests
tests/unit/money.test.tstests/unit/reward.service.test.tstests/unit/reward.controller.test.tsVerification
The 3 skipped tests are DB integration tests that have always been skipped in the test environment without a running Postgres instance — unrelated to this change.