Repository navigation
fix(settlement): use bigint contract units for payout and invoice math - #1527
Merged
greatest0fallt1me merged 2 commits intoOct 24, 2026
Conversation
Closes CalloraOrg#1512 ## What Replace all float-point USDC arithmetic in settlement, invoicing, and usage tracking with exact bigint contract units (1 USDC = 10_000_000). ### New shared library - src/lib/usdcUnits.ts — parseUsdcToUnits / formatUnitsToUsdc / formatUnitsToUsdcFixed + fast-check property tests ### Domain type changes - UsageEvent.amountUsdc: number → bigint - Settlement.amount: number → bigint - RevenueSummary fields: number → bigint - new_api_call webhook amountUsdc: number → string (boundary) ### Service changes - usageStore.ts: bigint sums, SQL read boundary via parseUsdcToUnits, write via formatUnitsToUsdc, snapshot totals bigint - settlementStore.ts: mapSettlementRow parses amount_usdc string→bigint, create writes formatUnitsToUsdc decimal string - sorobanSettlement.ts: removed convertUsdcToStroops; distribute() and buildSorobanSettlementInvocation accept bigint; i128 = String(units) - revenueSettlementService.ts: bigint reduce sum, minPayout converted via parseUsdcToUnits, webhook amount via formatUnitsToUsdcFixed - InvoiceService.ts: SUM(amount_usdc)::text→parseUsdcToUnits, insert formatUnitsToUsdc, webhook formatUnitsToUsdcFixed - billing.ts: delegates to usdcUnits helpers ### Boundary consumers - proxyRoutes.ts, gatewayRoutes.ts: parseUsdcToUnits(priceUsdc.toFixed(7)) at ingest; SSE payload uses formatUnitsToUsdc string - developerRoutes.ts: bigint sums, Number(units)/10_000_000 for JSON ### Lint guard - eslint.config.js: no-restricted-syntax bans parseFloat / Number( / .toFixed( in the five USDC-sensitive service files ### Tests added / updated - New: src/lib/usdcUnits.test.ts (unit + fast-check property tests) - New: src/services/usageStore.test.ts - Updated: revenueSettlementService, sorobanSettlement, InvoiceService, settlementStore ×2, settlementStatusSyncJob, developerRevenue, persistentStores, usageMetering, complete-integration, proxy.integration, developerData, developerRoutes, event.emitter ### Docs - docs/guides/SETTLEMENT_STORE_DOCUMENTATION.md: USDC contract units section, boundary rules table, lint guard note, conservation invariant
olubukolatanko209-doc
requested a review
from greatest0fallt1me
as a code owner
October 19, 2026 13:59
|
@olubukolatanko209-doc Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # src/__tests__/revenueSettlementService.test.ts # src/lib/usdcUnits.test.ts # src/lib/usdcUnits.ts # src/services/InvoiceService.ts # src/services/billing.ts # src/services/revenueSettlementService.ts # src/services/usageStore.ts
Closed
4 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 #1512
Replaces all floating-point USDC arithmetic in settlement, invoicing and usage tracking with exact
bigintcontract units (1 USDC = 10,000,000 units). This guarantees that payout amount == Σ event amounts == Σ invoice line items, to the stroop, for any set of events.Problem
RevenueSettlementServicesummede.amountUsdcfloats;convertUsdcToStroopsusedMath.round(... + Number.EPSILON);InvoiceServicetotalled withsum + Number(item.amount). With many micro-charges these sums drift, and amounts above 2^53 stroops lose precision entirely.Regression proof: 10,000 charges of 0.0000001 USDC as floats gives 0.0010000000000000002 drift. As bigint units (1n each): exactly 10000n. Test plan row 1 covers this.
Changes
New shared library —
src/lib/usdcUnits.tsparseUsdcToUnits(str)formatUnitsToUsdc(units)formatUnitsToUsdcFixed(units)Domain types changed
UsageEvent.amountUsdc:number→bigintSettlement.amount:number→bigintRevenueSummaryfields:number→bigintnew_api_callwebhookamountUsdc:number→string(external boundary)Service files
usageStore.tsparseUsdcToUnitson SQL read,formatUnitsToUsdcon writesettlementStore.tsmapSettlementRowparses string→bigint;createwrites decimal stringsorobanSettlement.tsconvertUsdcToStroops;distribute(bigint); i128 = String(units)revenueSettlementService.tsparseUsdcToUnits; emitformatUnitsToUsdcFixedInvoiceService.tsSUM::textcast +parseUsdcToUnits; insertformatUnitsToUsdc; webhook fixedbilling.tsusdcUnitsBoundary consumers
proxyRoutes.ts/gatewayRoutes.ts:parseUsdcToUnits(priceUsdc.toFixed(7))at ingestdeveloperRoutes.ts: bigint sums;Number(units) / 10_000_000for JSON (OpenAPItype: numberunchanged)Lint guard
eslint.config.jsaddsno-restricted-syntaxbanningparseFloat,Number(and.toFixed(in the five USDC-sensitive service files. Any reintroduction is a build-time error.Test plan coverage
Files changed
33 files changed, 879 insertions(+), 415 deletions(-)