Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughThe PR refactors currency calculation functions in helpers.js to use BigInt arithmetic instead of floating-point operations, replacing function parameters from numeric amounts to string amounts. A new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@djed-sdk/src/helpers.js`:
- Around line 144-151: In getScAdaEquivalent, the multiplication of two
6-decimal fixed-point values (amount and adaPerSc) yields a 12-decimal scaled
value (eqPrice); before calling decimalScaling, rescale eqPrice down by
1_000_000n (i.e., divide by 1_000_000n) so decimalScaling(..., 6) receives a
6-decimal value, or alternatively call decimalScaling(eqPrice.toString(), 12) to
tell decimalScaling the value is 12-decimal; update the logic around eqPrice and
the decimalScaling call accordingly.
- Around line 117-119: The code computes adaPerUsd via
decimalToBigInt(coinsDetails.scaledScExchangeRate) and then uses it as a divisor
(e.g., when computing eqPrice), but it does not guard against adaPerUsd being 0
which will throw on BigInt division; after computing adaPerUsd in the functions
that use it (references: decimalToBigInt, coinsDetails.scaledScExchangeRate,
adaPerUsd, eqPrice) add a check that throws a clear error or returns a safe
value if adaPerUsd === 0n (e.g., throw new RangeError('scaledScExchangeRate
resulted in zero')), so all subsequent divisions by adaPerUsd are protected.
🧹 Nitpick comments (1)
djed-sdk/src/helpers.js (1)
114-116: Add type validation to prevent silent precision loss in amount conversions.Lines 115, 129, and 145 call
.toString()on theamountStrparameter before passing todecimalToBigInt. If a caller accidentally passes a number,Number.toString()can lose precision before BigInt conversion happens. Since the parameter name already documents that strings are expected, enforce this by adding a type check and rejecting non-strings with a clear error rather than silently converting.Suggested fix (apply to all three functions)
export function calculateBcUsdEquivalent(coinsDetails, amountStr) { + if (typeof amountStr !== "string") { + throw new TypeError("amountStr must be a decimal string"); + } - const amount = decimalToBigInt(amountStr.toString(), 6); + const amount = decimalToBigInt(amountStr, 6);Apply similarly to
calculateRcUsdEquivalent(line 129) andgetScAdaEquivalent(line 145).
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Refactor includes:
Fixed precision loss in USD conversion functions.
Removed use of parseFloat and floating-point math for currency calculations.
All calculations now use BigInt to keep values exact for large amounts.
Output formatting remains unchanged and backward-compatible.
This prevents rounding errors in financial calculations.
Closes #75
@Zahnentferner Please review
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.