Skip to content

refactor(backend): extract shared usdcToStroops helper to eliminate float rounding drift#188

Open
leocagli wants to merge 1 commit into
Stellar-Ecosystem:mainfrom
leocagli:refactor/stroop-conversion-helper
Open

refactor(backend): extract shared usdcToStroops helper to eliminate float rounding drift#188
leocagli wants to merge 1 commit into
Stellar-Ecosystem:mainfrom
leocagli:refactor/stroop-conversion-helper

Conversation

@leocagli

Copy link
Copy Markdown
Contributor

Fixes #13

Problem

BigInt(Math.round(parseFloat(amount) * 10_000_000)) was duplicated across four call sites in services.js and agents.js. Floating-point multiplication gives incorrect results for some USDC amounts: parseFloat("0.0000001") * 10_000_000 yields 0.9999999... which rounds to 0 instead of 1 stroop.

Solution

New file backend/src/lib/stroops.js with integer-only arithmetic:

export function usdcToStroops(usdc) {
  // Splits on '.', pads/truncates fractional part to 7 digits,
  // then assembles with BigInt arithmetic — no floating-point involved.
  const [intPart, rawFrac = ''] = str.split('.');
  const frac = rawFrac.slice(0, 7).padEnd(7, '0');
  return BigInt(intPart) * 10_000_000n + BigInt(frac);
}

export function stroopsToUsdc(stroops) { ... }

Replaced all four inline conversions:

  • services.jsweatherPrice and searchPrice calls to recordPaymentOnChain
  • agents.jscan-spend route and payment record route

🤖 Generated with Claude Code

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
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@leocagli, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8e792346-675f-46e5-871d-ee03d2ebe1fd

📥 Commits

Reviewing files that changed from the base of the PR and between 6fb6da9 and 5a6948d.

📒 Files selected for processing (3)
  • backend/src/lib/stroops.js
  • backend/src/routes/agents.js
  • backend/src/routes/services.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant