Skip to content

Commit

Permalink
fix: check for ENS name while looking for balances
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Mar 16, 2023
1 parent 6b62f64 commit ace7070
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/widget/src/hooks/useGasSufficiency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export interface GasSufficiency {
const refetchInterval = 30_000;

export const useGasSufficiency = (route?: Route) => {
const { account } = useWallet();
const { account, provider } = useWallet();
const { getChainById } = useChains();
const getTokenBalancesWithRetry = useGetTokenBalancesWithRetry();
const getTokenBalancesWithRetry = useGetTokenBalancesWithRetry(provider);

const { data: insufficientGas, isInitialLoading } = useQuery(
['gas-sufficiency-check', account.address, route?.id],
Expand Down
12 changes: 9 additions & 3 deletions packages/widget/src/hooks/useGetTokenBalancesWithRetry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { isAddress } from '@ethersproject/address';
import type { Provider } from '@ethersproject/providers';
import type { Token, TokenAmount } from '@lifi/sdk';
import { useCallback } from 'react';
import { useLiFi } from '../providers';

export const useGetTokenBalancesWithRetry = () => {
export const useGetTokenBalancesWithRetry = (provider?: Provider) => {
const lifi = useLiFi();

const getTokenBalancesWithRetry = useCallback(
Expand All @@ -12,8 +14,12 @@ export const useGetTokenBalancesWithRetry = () => {
depth = 0,
): Promise<TokenAmount[] | undefined> => {
try {
const walletAddress = isAddress(accountAddress)
? accountAddress
: await provider?.resolveName(accountAddress);

const tokenBalances = await lifi.getTokenBalances(
accountAddress as string,
walletAddress as string,
tokens,
);
if (!tokenBalances.every((token) => token.blockNumber)) {
Expand All @@ -31,7 +37,7 @@ export const useGetTokenBalancesWithRetry = () => {
//
}
},
[lifi],
[lifi, provider],
);

return getTokenBalancesWithRetry;
Expand Down
4 changes: 2 additions & 2 deletions packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { useGetTokenBalancesWithRetry } from './useGetTokenBalancesWithRetry';
const defaultRefetchInterval = 30_000;

export const useTokenBalance = (token?: Token, accountAddress?: string) => {
const { account } = useWallet();
const { account, provider } = useWallet();
const queryClient = useQueryClient();
const walletAddress = accountAddress || account.address;

const getTokenBalancesWithRetry = useGetTokenBalancesWithRetry();
const getTokenBalancesWithRetry = useGetTokenBalancesWithRetry(provider);

const tokenBalanceQueryKey = useMemo(
() => ['token-balance', walletAddress, token?.chainId, token?.address],
Expand Down

0 comments on commit ace7070

Please sign in to comment.