Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions react/lib/components/PayButton/PayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,15 @@ export const PayButton = ({

useEffect(() => {
if (currencyObj && isFiat(currency) && price) {
if (!convertedCurrencyObj) {
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
}
} else if (!isFiat(currency) && randomSatoshis && !convertedCurrencyObj) {
const addressType: Currency = getCurrencyTypeFromAddress(to);
const convertedObj = getCurrencyObject(
currencyObj.float / price,
addressType,
randomSatoshis,
);
setCryptoAmount(convertedObj.string);
setConvertedCurrencyObj(convertedObj);
} else if (!isFiat(currency) && randomSatoshis) {
Comment on lines +370 to +378
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) {

const convertedObj = getCurrencyObject(
amount as number,
addressType,
Expand Down