Skip to content

Commit

Permalink
fix: correctly return balances on first call
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Apr 27, 2023
1 parent 9abaeeb commit 0b9b30e
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions packages/widget/src/hooks/useTokenBalances.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import { useState } from 'react';
import { useLiFi, useWallet } from '../providers';
import type { Token } from '../types';
import { formatTokenAmount } from '../utils';
import { useFeaturedTokens } from './useFeaturedTokens';
import { useTokens } from './useTokens';

const defaultRefetchInterval = 30_000;
const minRefetchInterval = 1000;
const defaultRefetchInterval = 32_000;

export const useTokenBalances = (selectedChainId?: number) => {
const lifi = useLiFi();
const { account } = useWallet();
const featuredTokens = useFeaturedTokens(selectedChainId);
const { tokens, isLoading } = useTokens(selectedChainId);
const [refetchInterval, setRefetchInterval] = useState(
defaultRefetchInterval,
);

const isBalanceLoadingEnabled =
Boolean(account.address) &&
Expand All @@ -35,17 +30,6 @@ export const useTokenBalances = (selectedChainId?: number) => {
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,
);
throw Error('Could not get tokens balance.');
}

const featuredTokenAddresses = new Set(
featuredTokens?.map((token) => token.address),
);
Expand Down Expand Up @@ -87,8 +71,8 @@ export const useTokenBalances = (selectedChainId?: number) => {
},
{
enabled: isBalanceLoadingEnabled,
refetchInterval,
staleTime: refetchInterval,
refetchInterval: defaultRefetchInterval,
staleTime: defaultRefetchInterval,
},
);

Expand Down

0 comments on commit 0b9b30e

Please sign in to comment.