Skip to content

Commit

Permalink
fix: wait for balances to load before showing warning
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Aug 3, 2022
1 parent 076a967 commit 58ae002
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/widget/src/hooks/useGasSufficiency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const useGasSufficiency = (route?: Route) => {
],
});
const fromAmount = useDebouncedWatch(SwapFormKey.FromAmount, 250);
const { tokens: fromChainTokenBalances } = useTokenBalances(fromChainId);
const { tokens: fromChainTokenBalances, isBalanceFetched } =
useTokenBalances(fromChainId);
const { tokens: toChainTokenBalances } = useTokenBalances(toChainId);
const { getChainById } = useChains();

Expand Down Expand Up @@ -117,14 +118,20 @@ export const useGasSufficiency = (route?: Route) => {
]);

const insufficientFunds = useMemo(() => {
if (!account.isActive || !fromToken || !fromAmount) {
if (!account.isActive || !fromToken || !fromAmount || !isBalanceFetched) {
return false;
}
const balance = Big(
fromChainTokenBalances?.find((t) => t.address === fromToken)?.amount ?? 0,
);
return Big(fromAmount).gt(balance);
}, [account.isActive, fromAmount, fromChainTokenBalances, fromToken]);
}, [
account.isActive,
fromAmount,
fromChainTokenBalances,
fromToken,
isBalanceFetched,
]);

return {
insufficientGas,
Expand Down
2 changes: 2 additions & 0 deletions packages/widget/src/hooks/useTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const useTokenBalances = (selectedChainId: number) => {
const {
data: tokensWithBalance,
isLoading: isBalanceLoading,
isFetched: isBalanceFetched,
refetch,
} = useQuery(
['token-balances', selectedChainId, account.address],
Expand Down Expand Up @@ -57,6 +58,7 @@ export const useTokenBalances = (selectedChainId: number) => {
tokens: tokensWithBalance ?? (tokens as TokenAmount[] | undefined),
isLoading: isLoading || isChainsLoading,
isBalanceLoading: isBalanceLoading && isBalanceLoadingEnabled,
isBalanceFetched,
updateBalances: refetch,
};
};

0 comments on commit 58ae002

Please sign in to comment.