From 750fc6a09c1fd8ea8e0c9ce8bd2ca6f622cfb2f5 Mon Sep 17 00:00:00 2001 From: Jhonatan Gonzalez Date: Mon, 11 Mar 2024 12:24:41 +0900 Subject: [PATCH] fix insufficient coins trigger --- .../widgets/sale/hooks/useSmartCheckout.ts | 41 +++++++++---------- .../sale/views/FundWithSmartCheckout.tsx | 14 ++----- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/packages/checkout/widgets-lib/src/widgets/sale/hooks/useSmartCheckout.ts b/packages/checkout/widgets-lib/src/widgets/sale/hooks/useSmartCheckout.ts index ef8f0ff248..a35499acc2 100644 --- a/packages/checkout/widgets-lib/src/widgets/sale/hooks/useSmartCheckout.ts +++ b/packages/checkout/widgets-lib/src/widgets/sale/hooks/useSmartCheckout.ts @@ -9,7 +9,7 @@ import { SaleErrorTypes, SmartCheckoutError, SmartCheckoutErrorTypes, } from '../types'; import { - filterSmartCheckoutResult, getGasEstimate, getItemRequirements, isUserFractionalBalanceBlocked, + filterSmartCheckoutResult, getFractionalBalance, getGasEstimate, getItemRequirements, } from '../functions/smartCheckoutUtils'; type UseSmartCheckoutInput = { @@ -31,25 +31,13 @@ export const useSmartCheckout = ({ ); const smartCheckout = useCallback(async () => { + let finalSmartCheckoutResult: SmartCheckoutResult | undefined; try { const signer = provider?.getSigner(); const spenderAddress = await signer?.getAddress() || ''; - - const userFractionalBalanceBlocked = await isUserFractionalBalanceBlocked( - spenderAddress, - tokenAddress, - amount, - checkout, - provider, - ); - - if (userFractionalBalanceBlocked) { - throw new Error(SmartCheckoutErrorTypes.FRACTIONAL_BALANCE_BLOCKED); - } - const itemRequirements = getItemRequirements(amount, spenderAddress, tokenAddress); const gasEstimate = getGasEstimate(); - const res = await checkout?.smartCheckout( + const result = await checkout?.smartCheckout( { provider: provider!, itemRequirements, @@ -57,19 +45,30 @@ export const useSmartCheckout = ({ }, ); - if (!res) { + if (!result) { throw new Error(); } - const result = { ...res }; - const filteredSmartCheckoutResult = filterSmartCheckoutResult(result, provider); - setSmartCheckoutResult(filteredSmartCheckoutResult); + + // TODO: Smart funding UI will be added later, meanwhile all insufficient + // balances will be blocked and routed tru the add coins flow + // FIXME: filtering smart checkout result won't be necessary + // once smart checkout allows to skip gas checks and passing disabled funding routes + finalSmartCheckoutResult = filterSmartCheckoutResult({ ...result }, provider); + if (!finalSmartCheckoutResult.sufficient) { + throw new Error(SmartCheckoutErrorTypes.FRACTIONAL_BALANCE_BLOCKED); + } + + setSmartCheckoutResult(finalSmartCheckoutResult); + return result; - } catch (err: any) { + } catch (error: any) { + const fractionalBalance = getFractionalBalance(finalSmartCheckoutResult); setSmartCheckoutError({ type: SaleErrorTypes.SMART_CHECKOUT_ERROR, - data: { error: err }, + data: { error, fractionalBalance }, }); } + return undefined; }, [checkout, provider, items, amount, tokenAddress]); diff --git a/packages/checkout/widgets-lib/src/widgets/sale/views/FundWithSmartCheckout.tsx b/packages/checkout/widgets-lib/src/widgets/sale/views/FundWithSmartCheckout.tsx index 91c09b2697..fbcaea02a4 100644 --- a/packages/checkout/widgets-lib/src/widgets/sale/views/FundWithSmartCheckout.tsx +++ b/packages/checkout/widgets-lib/src/widgets/sale/views/FundWithSmartCheckout.tsx @@ -3,7 +3,7 @@ import { FundingRoute } from '@imtbl/checkout-sdk'; import { useContext, useEffect, - useMemo, useRef, useState, + useMemo, useState, } from 'react'; import { useTranslation } from 'react-i18next'; import { @@ -36,21 +36,13 @@ export function FundWithSmartCheckout({ subView }: FundWithSmartCheckoutProps) { } = useSaleContext(); const { cryptoFiatDispatch } = useContext(CryptoFiatContext); - const smartCheckoutLoading = useRef(false); - const onFundingRouteSelected = (fundingRoute: FundingRoute) => { setSelectedFundingRoute(fundingRoute); }; useEffect(() => { - if (subView === FundWithSmartCheckoutSubViews.INIT && !smartCheckoutLoading.current) { - smartCheckoutLoading.current = true; - try { - querySmartCheckout(); - } finally { - smartCheckoutLoading.current = false; - } - } + if (subView !== FundWithSmartCheckoutSubViews.INIT) return; + querySmartCheckout(); }, [subView]); useEffect(() => {