Skip to content

Commit

Permalink
fix insufficient coins trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
jhesgodi committed Mar 11, 2024
1 parent 7382e8a commit 750fc6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SaleErrorTypes, SmartCheckoutError, SmartCheckoutErrorTypes,
} from '../types';
import {
filterSmartCheckoutResult, getGasEstimate, getItemRequirements, isUserFractionalBalanceBlocked,
filterSmartCheckoutResult, getFractionalBalance, getGasEstimate, getItemRequirements,
} from '../functions/smartCheckoutUtils';

type UseSmartCheckoutInput = {
Expand All @@ -31,45 +31,44 @@ 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,
transactionOrGasAmount: gasEstimate,
},
);

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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit 750fc6a

Please sign in to comment.