|
1 | | -import { useCallback } from 'react' |
| 1 | +import { useCallback, useMemo } from 'react' |
2 | 2 | import curvejsApi from '@/dex/lib/curvejs' |
3 | 3 | import useStore from '@/dex/store/useStore' |
4 | | -import type { NetworkDef } from '@ui/utils' |
5 | 4 | import { type CurveApi, useConnection } from '@ui-kit/features/connect-wallet' |
6 | 5 | import usePageVisibleInterval from '@ui-kit/hooks/usePageVisibleInterval' |
7 | 6 | import { REFRESH_INTERVAL } from '@ui-kit/lib/model' |
8 | 7 | import { useGasInfoAndUpdateLib } from '@ui-kit/lib/model/entities/gas-info' |
9 | | -import { useNetworkByChain, useNetworks } from '../entities/networks' |
| 8 | +import { useNetworks } from '../entities/networks' |
10 | 9 |
|
11 | | -export const useAutoRefresh = (networkDef: NetworkDef) => { |
| 10 | +export const useAutoRefresh = (chainId: number | undefined) => { |
12 | 11 | const { curveApi } = useConnection() |
13 | | - |
| 12 | + const { data: networks } = useNetworks() |
14 | 13 | const fetchPools = useStore((state) => state.pools.fetchPools) |
15 | | - const poolDataMapper = useStore((state) => state.pools.poolsMapper[networkDef.chainId]) |
| 14 | + const poolDataMapper = useStore((state) => chainId && state.pools.poolsMapper[chainId]) |
16 | 15 | const fetchPoolsVolume = useStore((state) => state.pools.fetchPoolsVolume) |
17 | 16 | const fetchPoolsTvl = useStore((state) => state.pools.fetchPoolsTvl) |
18 | 17 | const setTokensMapper = useStore((state) => state.tokens.setTokensMapper) |
19 | 18 | const fetchAllStoredBalances = useStore((state) => state.userBalances.fetchAllStoredBalances) |
20 | 19 |
|
21 | | - const { data: networks } = useNetworks() |
22 | | - const { data: network } = useNetworkByChain({ chainId: networkDef.chainId }) |
| 20 | + const network = useMemo(() => chainId && networks[chainId], [chainId, networks]) |
23 | 21 |
|
24 | | - useGasInfoAndUpdateLib({ chainId: networkDef.chainId, networks }) |
| 22 | + useGasInfoAndUpdateLib({ chainId, networks }) |
25 | 23 |
|
26 | 24 | const fetchPoolsVolumeTvl = useCallback( |
27 | 25 | async (curve: CurveApi) => { |
28 | | - const { chainId } = curve |
| 26 | + if (!chainId || !poolDataMapper) return |
29 | 27 | const poolsData = Object.values(poolDataMapper) |
30 | 28 | await Promise.all([fetchPoolsVolume(chainId, poolsData), fetchPoolsTvl(curve, poolsData)]) |
31 | 29 | void setTokensMapper(curve, poolsData) |
32 | 30 | }, |
33 | | - [fetchPoolsTvl, fetchPoolsVolume, poolDataMapper, setTokensMapper], |
| 31 | + [chainId, fetchPoolsTvl, fetchPoolsVolume, poolDataMapper, setTokensMapper], |
34 | 32 | ) |
35 | 33 |
|
36 | 34 | usePageVisibleInterval(() => { |
|
0 commit comments