Skip to content

Conversation

@Klakurka
Copy link
Member

@Klakurka Klakurka commented Dec 28, 2025

Description

Amount wouldn't change even after closing the dialog and opening up a new one with a different fiat value; this fixes that.

Test plan

I was only able to reproduce the issue on coin.dance/support which has since been updated to use this branch... not sure how else to test the issue in master.

Summary by CodeRabbit

  • Refactor
    • Improved currency conversion logic in the payment button. Enhanced calculation flow for both fiat and non-fiat currencies to ensure converted amounts are always recalculated directly from current values. Streamlined conditional branches to make the update process more direct, reliable, and efficient.

✏️ Tip: You can customize this high-level summary in your review settings.

@Klakurka Klakurka added this to the Phase 3 milestone Dec 28, 2025
@Klakurka Klakurka requested a review from chedieck December 28, 2025 19:50
@Klakurka Klakurka self-assigned this Dec 28, 2025
@Klakurka Klakurka added the bug Something isn't working label Dec 28, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 28, 2025

📝 Walkthrough

Walkthrough

The useEffect handling currency conversion in PayButton.tsx was simplified. The fiat path now always computes converted values instead of conditionally, and the non-fiat path with randomSatoshis now follows a more direct computation path, reducing overall conditional branching while potentially altering recalculation timing.

Changes

Cohort / File(s) Summary
Currency Conversion Logic
react/lib/components/PayButton/PayButton.tsx
Modified useEffect: fiat path now unconditionally computes convertedObj and updates state; non-fiat path with randomSatoshis computes convertedObj directly; removed conditional guards and fallback branches, streamlining control flow

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With conditional branches trimmed and logic made lean,
The fiat and crypto now flow more serene,
No guards left behind, just conversion so clear,
A simpler PayButton for all we hold dear!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: amount not being set correctly when using fiat value' directly and specifically describes the main bug fix in the changeset—addressing an issue where amounts weren't updating with fiat values.
Description check ✅ Passed The description covers the main issue and test plan, though it lacks the 'Related to #' link and provides limited detail on how the fix works, making it somewhat incomplete against the template structure.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/amount-with-fiat

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.

❤️ Share

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

@Klakurka Klakurka requested a review from lissavxo December 28, 2025 19:53
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
react/lib/components/PayButton/PayButton.tsx (1)

379-389: Missing dependency in useEffect array.

The non-fiat branch at line 381 uses the state variable addressType, but addressType is not included in the dependency array at line 389. This means the effect won't re-run when addressType state changes, potentially causing stale conversions when the address type is updated (e.g., by the PaymentDialog component).

🔎 Add missing dependency
-  }, [price, currencyObj, amount, currency, randomSatoshis, to]);
+  }, [price, currencyObj, amount, currency, randomSatoshis, to, addressType]);

Note: If you implement Option 2 from the previous comment (computing addressType from to in all branches), you can remove the state dependency and rely on to instead.

🧹 Nitpick comments (1)
react/lib/components/PayButton/PayButton.tsx (1)

370-370: Use more precise type annotation.

getCurrencyTypeFromAddress returns CryptoCurrency (per the relevant code snippet), but the variable is annotated as the broader Currency type. While not incorrect, using the more specific type would improve clarity.

🔎 More precise type
-    const addressType: Currency = getCurrencyTypeFromAddress(to);
+    const addressType: CryptoCurrency = getCurrencyTypeFromAddress(to);
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d16a8b and 48c1548.

📒 Files selected for processing (1)
  • react/lib/components/PayButton/PayButton.tsx
🧰 Additional context used
🧬 Code graph analysis (1)
react/lib/components/PayButton/PayButton.tsx (4)
react/lib/util/types.ts (1)
  • Currency (140-140)
react/lib/util/address.ts (1)
  • getCurrencyTypeFromAddress (26-34)
react/lib/util/satoshis.ts (1)
  • getCurrencyObject (8-41)
react/lib/util/currency.ts (1)
  • isFiat (4-6)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Run build

Comment on lines +370 to +378
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Variable shadowing creates inconsistency with non-fiat branch.

Line 370 declares a local addressType variable that shadows the state variable (line 121), but this local variable is only used in the fiat branch. The non-fiat branch at lines 379-385 uses the state addressType variable. This inconsistency makes the code harder to reason about and could lead to subtle bugs if the state and derived values diverge.

Additionally, getCurrencyTypeFromAddress can throw an error for invalid addresses (per the relevant code snippet), but there's no error handling here.

🔎 Suggested improvements

Consider one of these approaches:

Option 1: Use a distinct name for the local variable

-    const addressType: Currency = getCurrencyTypeFromAddress(to);
+    const derivedAddressType = getCurrencyTypeFromAddress(to);
     const convertedObj = getCurrencyObject(
       currencyObj.float / price,
-      addressType,
+      derivedAddressType,
       randomSatoshis,
     );

Option 2: Update the state variable and use it consistently

+    const derivedAddressType = getCurrencyTypeFromAddress(to);
+    setAddressType(derivedAddressType);
     const convertedObj = getCurrencyObject(
       currencyObj.float / price,
-      addressType,
+      derivedAddressType,
       randomSatoshis,
     );

Option 3: Add error handling

+    try {
       const addressType: Currency = getCurrencyTypeFromAddress(to);
       const convertedObj = getCurrencyObject(
         currencyObj.float / price,
         addressType,
         randomSatoshis,
       );
       setCryptoAmount(convertedObj.string);
       setConvertedCurrencyObj(convertedObj);
+    } catch (error) {
+      console.error('Invalid address type:', error);
+      // Handle error appropriately
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
const derivedAddressType = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
derivedAddressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
Suggested change
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
const derivedAddressType = getCurrencyTypeFromAddress(to);
setAddressType(derivedAddressType);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
derivedAddressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
Suggested change
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
try {
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} catch (error) {
console.error('Invalid address type:', error);
// Handle error appropriately
}
} else if (!isFiat(currency) && randomSatoshis) {

@Klakurka Klakurka merged commit 15b910a into master Dec 30, 2025
3 checks passed
@Klakurka Klakurka linked an issue Dec 30, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XEC/BCH amount not changing when opening a 2nd, 3rd dialog

3 participants