refactor(backend): extract shared usdcToStroops helper to eliminate float rounding drift#188
Conversation
Closes Stellar-Ecosystem#13 BigInt(Math.round(parseFloat(amount) * 10_000_000)) was duplicated across four call sites in services.js and agents.js. Floating-point multiplication rounds incorrectly for USDC amounts with many decimal places (e.g. parseFloat of 0.0000001 * 10_000_000 can give 0.9999999... → rounds to 0). Added backend/src/lib/stroops.js with: - usdcToStroops(usdc): splits on decimal point and uses BigInt arithmetic (no floating-point math) to produce exact stroop values - stroopsToUsdc(stroops): inverse conversion with trailing-zero trimming Replaced all four inline conversions: - services.js (2): weatherPrice and searchPrice when calling recordPaymentOnChain - agents.js (2): can-spend route and payment record route
|
Warning Review limit reached
More reviews will be available in 1 second. Learn how PR review limits work. To continue reviewing without waiting, enable usage-based billing in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Fixes #13
Problem
BigInt(Math.round(parseFloat(amount) * 10_000_000))was duplicated across four call sites inservices.jsandagents.js. Floating-point multiplication gives incorrect results for some USDC amounts:parseFloat("0.0000001") * 10_000_000yields0.9999999...which rounds to0instead of1stroop.Solution
New file
backend/src/lib/stroops.jswith integer-only arithmetic:Replaced all four inline conversions:
services.js→weatherPriceandsearchPricecalls torecordPaymentOnChainagents.js→can-spendroute and payment record route🤖 Generated with Claude Code