Skip to content

Commit

Permalink
fix: only check route data for gas sufficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Sep 14, 2022
1 parent ad0a83f commit d2b08be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
6 changes: 5 additions & 1 deletion packages/widget/src/components/TokenList/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export const TokenList: FC<TokenListProps> = ({
)
: filteredTokens;

const tokenSearchEnabled = !filteredTokens.length && !isTokensLoading;
const tokenSearchEnabled =
!isTokensLoading &&
!!filteredTokens.length &&
!!tokenSearchFilter &&
!!selectedChainId;

const { token: searchedToken, isLoading: isSearchedTokenLoading } =
useTokenSearch(tokenSearchFilter, selectedChainId, tokenSearchEnabled);
Expand Down
67 changes: 22 additions & 45 deletions packages/widget/src/hooks/useGasSufficiency.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type { EVMChain, Route, Token } from '@lifi/sdk';
import Big from 'big.js';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useWatch } from 'react-hook-form';
import { useChains, useDebouncedWatch } from '.';
import {
SwapFormKey,
SwapFormKeyHelper,
useLiFi,
useWallet,
} from '../providers';
import { useChains } from '.';
import { useLiFi, useWallet } from '../providers';
import { useTokenBalances } from './useTokenBalances';

interface GasSufficiency {
Expand All @@ -23,27 +17,18 @@ interface GasSufficiency {
export const useGasSufficiency = (route?: Route) => {
const lifi = useLiFi();
const { account } = useWallet();
const [fromChainId, toChainId, fromToken]: [number, number, string] =
useWatch({
name: [
SwapFormKeyHelper.getChainKey('from'),
SwapFormKeyHelper.getChainKey('to'),
SwapFormKey.FromToken,
],
});

const fromAmount = useDebouncedWatch(SwapFormKey.FromAmount, 250);
const { getChainById } = useChains();
const { tokensWithBalance: fromChainTokenBalances } =
useTokenBalances(fromChainId);
const { tokensWithBalance: fromChainTokenBalances } = useTokenBalances(
route?.fromChainId,
);
const [insufficientGas, setInsufficientGas] = useState<GasSufficiency[]>();

const checkInsufficientGas = useCallback(async () => {
if (!account.isActive || !route || !fromAmount) {
if (!account.isActive || !route) {
setInsufficientGas(undefined);
return;
}

const gasCosts = route.steps.reduce((groupedGasCosts, step) => {
if (step.estimate.gasCosts) {
const { token } = step.estimate.gasCosts[0];
Expand All @@ -69,11 +54,13 @@ export const useGasSufficiency = (route?: Route) => {
}, {} as Record<number, GasSufficiency>);

if (
gasCosts[fromChainId] &&
route.fromToken.address === gasCosts[fromChainId].token.address
gasCosts[route.fromChainId] &&
route.fromToken.address === gasCosts[route.fromChainId].token.address
) {
gasCosts[fromChainId].tokenAmount = gasCosts[fromChainId]?.gasAmount.plus(
Big(fromAmount),
gasCosts[route.fromChainId].tokenAmount = gasCosts[
route.fromChainId
]?.gasAmount.plus(
Big(route.fromAmount).div(10 ** route.fromToken.decimals),
);
}

Expand All @@ -87,7 +74,7 @@ export const useGasSufficiency = (route?: Route) => {
return;
}

[fromChainId, toChainId].forEach((chainId) => {
[route.fromChainId, route.toChainId].forEach((chainId) => {
if (gasCosts[chainId]) {
const gasTokenBalance = Big(
tokenBalances?.find(
Expand Down Expand Up @@ -120,31 +107,21 @@ export const useGasSufficiency = (route?: Route) => {
);

setInsufficientGas(gasCostResult);
}, [
account.address,
account.isActive,
fromAmount,
fromChainId,
getChainById,
lifi,
route,
toChainId,
]);
}, [account.address, account.isActive, getChainById, lifi, route]);

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

useEffect(() => {
checkInsufficientGas();
Expand Down
6 changes: 4 additions & 2 deletions packages/widget/src/hooks/useTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useTokens } from './useTokens';
const defaultRefetchInterval = 60_000;
const minRefetchInterval = 1000;

export const useTokenBalances = (selectedChainId: number) => {
export const useTokenBalances = (selectedChainId?: number) => {
const lifi = useLiFi();
const { account } = useWallet();
const featuredTokens = useFeaturedTokens(selectedChainId);
Expand All @@ -20,7 +20,9 @@ export const useTokenBalances = (selectedChainId: number) => {
);

const isBalanceLoadingEnabled =
Boolean(account.address) && Boolean(tokens?.length);
Boolean(account.address) &&
Boolean(tokens?.length) &&
Boolean(selectedChainId);

const {
data: tokensWithBalance,
Expand Down

0 comments on commit d2b08be

Please sign in to comment.