From 3e2ba23651e41c92bde6af7d3d2dd6e571769e15 Mon Sep 17 00:00:00 2001 From: Jhonatan Gonzalez Date: Wed, 1 May 2024 18:01:17 +0900 Subject: [PATCH] fix possible null balance checks --- .../sale/functions/fetchFundingBalances.ts | 59 +++++++++++-------- .../widgets/sale/hooks/useFundingBalances.ts | 2 +- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/checkout/widgets-lib/src/widgets/sale/functions/fetchFundingBalances.ts b/packages/checkout/widgets-lib/src/widgets/sale/functions/fetchFundingBalances.ts index 0d1f89f82d..3562b07353 100644 --- a/packages/checkout/widgets-lib/src/widgets/sale/functions/fetchFundingBalances.ts +++ b/packages/checkout/widgets-lib/src/widgets/sale/functions/fetchFundingBalances.ts @@ -56,34 +56,43 @@ export const fetchFundingBalances = async ( } }; - const balancePromises = currencies.map(async (currency) => { - const amount = getAmountByCurrency(currency) || '0'; - const itemRequirements = getERC20ItemRequirement( - amount, - spenderAddress, - currency.address, - ); + const balancePromises: Promise[] = currencies + .map(async (currency) => { + const amount = getAmountByCurrency(currency); - const transactionOrGasAmount = getIsGasless() - ? undefined - : getGasEstimate(); + if (!amount) { + return null; + } + + const itemRequirements = getERC20ItemRequirement( + amount, + spenderAddress, + currency.address, + ); + + const transactionOrGasAmount = getIsGasless() + ? undefined + : getGasEstimate(); - const smartCheckoutResult = await checkout.smartCheckout({ - provider, - itemRequirements, - transactionOrGasAmount, - routingOptions: { bridge: false, onRamp: false, swap: true }, - fundingRouteFullAmount: true, - onComplete: () => { - onComplete?.(pushToFoundBalances([])); - }, - onFundingRoute: (route) => { - updateFundingBalances(getAlternativeFundingSteps([route], environment)); - }, - }); + const smartCheckoutResult = await checkout.smartCheckout({ + provider, + itemRequirements, + transactionOrGasAmount, + routingOptions: { bridge: false, onRamp: false, swap: true }, + fundingRouteFullAmount: true, + onComplete: () => { + onComplete?.(pushToFoundBalances([])); + }, + onFundingRoute: (route) => { + updateFundingBalances( + getAlternativeFundingSteps([route], environment), + ); + }, + }); - return { currency, smartCheckoutResult }; - }); + return { currency, smartCheckoutResult }; + }) + .filter(Boolean) as Promise[]; const results = await wrapPromisesWithOnResolve( balancePromises, diff --git a/packages/checkout/widgets-lib/src/widgets/sale/hooks/useFundingBalances.ts b/packages/checkout/widgets-lib/src/widgets/sale/hooks/useFundingBalances.ts index 1e1596285c..c61a1eb7f6 100644 --- a/packages/checkout/widgets-lib/src/widgets/sale/hooks/useFundingBalances.ts +++ b/packages/checkout/widgets-lib/src/widgets/sale/hooks/useFundingBalances.ts @@ -44,7 +44,7 @@ export const useFundingBalances = () => { routingOptions: { bridge: false, onRamp: false, swap: true }, baseCurrency: selectedCurrency, getAmountByCurrency: (currency) => clientConfig.currencyConversion?.[ - currency.name + currency.name.toUpperCase() ]?.amount?.toString(), getIsGasless: () => (provider.provider as any)?.isPassport || false, onFundingBalance: (foundBalances) => {