Skip to content

Commit

Permalink
fix: gas sufficiency message appears after reloading while executing …
Browse files Browse the repository at this point in the history
…the route
  • Loading branch information
chybisov committed Aug 6, 2022
1 parent beaf96f commit e281787
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
10 changes: 8 additions & 2 deletions packages/widget/src/components/TokenList/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ export const TokenList: FC<TokenListProps> = ({
250,
);

const { tokens, isLoading, isBalanceLoading } =
useTokenBalances(selectedChainId);
const {
tokens: tokensWithoutBalance,
tokensWithBalance,
isLoading,
isBalanceLoading,
} = useTokenBalances(selectedChainId);

const tokens = tokensWithBalance ?? tokensWithoutBalance;

const chainTokens = useMemo(() => {
let chainTokens = tokens ?? [];
Expand Down
31 changes: 19 additions & 12 deletions packages/widget/src/hooks/useGasSufficiency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ export const useGasSufficiency = (route?: Route) => {
SwapFormKey.FromToken,
],
});

const fromAmount = useDebouncedWatch(SwapFormKey.FromAmount, 250);
const { tokens: fromChainTokenBalances, isBalanceFetched } =
useTokenBalances(fromChainId);
const { tokens: toChainTokenBalances } = useTokenBalances(toChainId);
const { getChainById } = useChains();
const { tokensWithBalance: fromChainTokenBalances } =
useTokenBalances(fromChainId);
const { tokensWithBalance: toChainTokenBalances } =
useTokenBalances(toChainId);

const insufficientGas = useMemo(() => {
if (!account.isActive || !route || !fromAmount) {
if (
!account.isActive ||
!route ||
!fromAmount ||
!fromChainTokenBalances ||
!toChainTokenBalances
) {
return [];
}

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

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

return {
insufficientGas,
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useTokenBalance = (chainId: number, tokenAddress: string) => {
isFetching,
refetch,
} = useQuery(
['token', account.address, chainId, tokenAddress],
['token-balance', account.address, chainId, tokenAddress],
async ({ queryKey: [, address] }) => {
if (!address || !token) {
return null;
Expand Down
42 changes: 30 additions & 12 deletions packages/widget/src/hooks/useTokenBalances.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
/* eslint-disable consistent-return */
import { TokenAmount } from '@lifi/sdk';
import { useQuery } from '@tanstack/react-query';
import { useState } from 'react';
import { LiFi } from '../config/lifi';
import { useWallet } from '../providers/WalletProvider';
import { formatTokenAmount } from '../utils';
import { useChains } from './useChains';
import { useTokens } from './useTokens';

const defaultRefetchInterval = 60_000;
const minRefetchInterval = 1000;

export const useTokenBalances = (selectedChainId: number) => {
const { account } = useWallet();
const { chains, isLoading: isChainsLoading } = useChains();
const { tokens, isLoading } = useTokens(selectedChainId);
const [refetchInterval, setRefetchInterval] = useState(
defaultRefetchInterval,
);

const isBalanceLoadingEnabled =
Boolean(account.address) && Boolean(tokens) && Boolean(chains);
const isBalanceLoadingEnabled = Boolean(account.address) && Boolean(tokens);

const {
data: tokensWithBalance,
Expand All @@ -21,14 +26,26 @@ export const useTokenBalances = (selectedChainId: number) => {
refetch,
} = useQuery(
['token-balances', selectedChainId, account.address],
async ({ queryKey: [_, chainId, account] }) => {
if (!account || !tokens) {
return [];
async ({ queryKey: [, , accountAddress] }) => {
if (!accountAddress || !tokens) {
return;
}
const tokenBalances = await LiFi.getTokenBalances(
account as string,
accountAddress as string,
tokens,
);

if (!tokenBalances?.length) {
// Sometimes RPCs (e.g. Arbitrum) don't return balances on first call
// TODO: fix and remove backplane
setRefetchInterval((interval) =>
interval === defaultRefetchInterval
? minRefetchInterval
: interval * 2,
);
return;
}

const formatedTokens = (
tokenBalances.length === 0 ? (tokens as TokenAmount[]) : tokenBalances
).map((token) => {
Expand All @@ -49,14 +66,15 @@ export const useTokenBalances = (selectedChainId: number) => {
{
enabled: isBalanceLoadingEnabled,
refetchIntervalInBackground: true,
refetchInterval: 60_000,
staleTime: 60_000,
refetchInterval,
staleTime: refetchInterval,
},
);

return {
tokens: tokensWithBalance ?? (tokens as TokenAmount[] | undefined),
isLoading: isLoading || isChainsLoading,
tokens,
tokensWithBalance,
isLoading,
isBalanceLoading: isBalanceLoading && isBalanceLoadingEnabled,
isBalanceFetched,
updateBalances: refetch,
Expand Down

0 comments on commit e281787

Please sign in to comment.