-
Notifications
You must be signed in to change notification settings - Fork 13
Refactor/ternary confirm view and abstract fee explainers #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b769a2b
f064a2d
a6b8fec
bb5201f
0b9c025
9b51ea7
f6f586a
bf8b7f1
a709c6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,12 +17,17 @@ import useClaimLink from '@/components/Claim/useClaimLink' | |||||||||||||||||||||||||||||||||||||
import Link from 'next/link' | ||||||||||||||||||||||||||||||||||||||
import { CrispButton } from '@/components/CrispChat' | ||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||
achFeeExplainer, | ||||||||||||||||||||||||||||||||||||||
claimLinkFeeExplainer, | ||||||||||||||||||||||||||||||||||||||
CrossChainDetails, | ||||||||||||||||||||||||||||||||||||||
IOfframpConfirmScreenProps, | ||||||||||||||||||||||||||||||||||||||
LiquidationAddress, | ||||||||||||||||||||||||||||||||||||||
OFFRAMP_IBAN_FEE_USD, | ||||||||||||||||||||||||||||||||||||||
OFFRAMP_NON_IBAN_FEE_USD, | ||||||||||||||||||||||||||||||||||||||
OfframpType, | ||||||||||||||||||||||||||||||||||||||
optimismChainId, | ||||||||||||||||||||||||||||||||||||||
PeanutAccount, | ||||||||||||||||||||||||||||||||||||||
sepaFeeExplainer, | ||||||||||||||||||||||||||||||||||||||
usdcAddressOptimism, | ||||||||||||||||||||||||||||||||||||||
} from '@/components/Offramp/Offramp.consts' | ||||||||||||||||||||||||||||||||||||||
import { FAQComponent } from '../Cashout/Components/Faq.comp' | ||||||||||||||||||||||||||||||||||||||
|
@@ -68,6 +73,35 @@ export const OfframpConfirmView = ({ | |||||||||||||||||||||||||||||||||||||
////////////////////// | ||||||||||||||||||||||||||||||||||||||
// state and context vars for claim link offramp | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
////////////////////// | ||||||||||||||||||||||||||||||||||||||
// utility JSX vars | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// type: 'iban' or other | ||||||||||||||||||||||||||||||||||||||
// TODO: standardize this type | ||||||||||||||||||||||||||||||||||||||
let accountType = user?.accounts.find((account) => account.account_identifier === offrampForm.recipient)?.account_type | ||||||||||||||||||||||||||||||||||||||
const fee = utils.returnOfframpFee( | ||||||||||||||||||||||||||||||||||||||
offrampType, | ||||||||||||||||||||||||||||||||||||||
accountType | ||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
const feeExplainer = utils.returnOfframpFeeExplainer( | ||||||||||||||||||||||||||||||||||||||
offrampType, | ||||||||||||||||||||||||||||||||||||||
accountType | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
let amount: number = 0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if (offrampType == OfframpType.CASHOUT) { | ||||||||||||||||||||||||||||||||||||||
if (accountType == 'iban') { | ||||||||||||||||||||||||||||||||||||||
amount = parseFloat(usdValue ?? tokenValue ?? '') - fee | ||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||
amount = parseFloat(usdValue ?? '') - fee | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
amount = parseFloat(usdValue ?? tokenValue ?? '') - fee | |
} else { | |
amount = parseFloat(usdValue ?? '') - fee | |
} | |
if (accountType == 'iban') { | |
const value = usdValue ?? tokenValue; | |
if (value) { | |
amount = parseFloat(value) - fee; | |
} else { | |
amount = 0; | |
} | |
} else { | |
if (usdValue) { | |
amount = parseFloat(usdValue) - fee; | |
} else { | |
amount = 0; | |
} | |
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './general.utils' | ||
export * from './sdkErrorHandler.utils' | ||
export * from './fetch.utils' | ||
export * from './cashout.utils' | ||
export * from './offramp.utils' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import * as utils from '@/utils' | |
import countries from 'i18n-iso-countries' | ||
import { generateKeysFromString } from '@squirrel-labs/peanut-sdk' | ||
import { getSquidRouteRaw } from '@squirrel-labs/peanut-sdk' | ||
import { achFeeExplainer, claimLinkFeeExplainer, OFFRAMP_IBAN_FEE_USD, OFFRAMP_NON_IBAN_FEE_USD, OfframpType, sepaFeeExplainer } from '@/components/Offramp/Offramp.consts' | ||
|
||
export const convertPersonaUrl = (url: string) => { | ||
const parsedUrl = new URL(url) | ||
|
@@ -527,3 +528,24 @@ export const fetchRouteRaw = async ( | |
return undefined | ||
} | ||
} | ||
|
||
export function returnOfframpFee(offrampType: OfframpType, accountType: any): number { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use specific types instead of 'any' for 'accountType' parameter Using Proposed change: Define a type for type AccountType = 'iban' | 'us'; Update the function signatures: -export function returnOfframpFee(offrampType: OfframpType, accountType: any): number {
+export function returnOfframpFee(offrampType: OfframpType, accountType: AccountType): number { Apply similar changes to the other functions: -export function returnOfframpFeeExplainer(offrampType: OfframpType, accountType: any): string {
+export function returnOfframpFeeExplainer(offrampType: OfframpType, accountType: AccountType): string { -export function returnOfframpTotalAmountAfterFees(
+export function returnOfframpTotalAmountAfterFees(
offrampType: OfframpType,
- accountType: any,
+ accountType: AccountType,
...
): number { Also applies to: 541-541, 553-553 |
||
let fee = 0; | ||
if (offrampType == OfframpType.CASHOUT) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use strict equality checks ('===') instead of loose equality ('==') For better type safety and to avoid unexpected type coercion, it's recommended to use strict equality checks ( Apply this change to all relevant comparisons: -if (offrampType == OfframpType.CASHOUT) {
+if (offrampType === OfframpType.CASHOUT) { -if (accountType == 'iban') {
+if (accountType === 'iban') { -if (offrampType == OfframpType.CLAIM && tokenPrice && tokenAmount) {
+if (offrampType === OfframpType.CLAIM && tokenPrice && tokenAmount) { Also applies to: 543-543, 564-564, 570-570 |
||
fee = accountType === 'iban' ? OFFRAMP_IBAN_FEE_USD : OFFRAMP_NON_IBAN_FEE_USD | ||
} | ||
// other types of offramp (eg. CLAIM link) do not have a fee | ||
return fee; | ||
} | ||
|
||
export function returnOfframpFeeExplainer(offrampType: OfframpType, accountType: any): string { | ||
let feeExplainer = '' | ||
if (offrampType == OfframpType.CASHOUT) { | ||
feeExplainer = accountType === 'iban' ? sepaFeeExplainer + ' ' + achFeeExplainer: achFeeExplainer + ' ' + sepaFeeExplainer | ||
} else { | ||
if (offrampType == OfframpType.CLAIM) { | ||
feeExplainer = claimLinkFeeExplainer | ||
} | ||
} | ||
return feeExplainer | ||
} | ||
Comment on lines
+541
to
+551
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve function structure for better readability and maintainability. The Consider refactoring the function as follows: export function returnOfframpFeeExplainer(offrampType: OfframpType, accountType: any): string {
switch (offrampType) {
case OfframpType.CASHOUT:
return accountType === 'iban'
? `${sepaFeeExplainer} ${achFeeExplainer}`
: `${achFeeExplainer} ${sepaFeeExplainer}`;
case OfframpType.CLAIM:
return claimLinkFeeExplainer;
default:
console.warn(`Unhandled offramp type: ${offrampType}`);
return '';
}
} This refactored version:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle undefined
accountType
to prevent potential errorsIf
accountType
isundefined
(e.g., if no matching account is found), passing it toutils.returnOfframpFee
andutils.returnOfframpFeeExplainer
might lead to unexpected results. Consider providing a default value or adding a check to ensureaccountType
is defined.Here's a possible fix: