Skip to content

Commit 23ab3b7

Browse files
committed
refactor: migrate hooks to useConfig()
Convert hooks from direct config import to useConfig() hook: - useAutoEnableGasDropoff, useAvailableWallets, useComputeDestinationTokens - useConfirmTransaction, useExternalSearch, useFetchQuotes - useFetchSupportedRoutes, useGasSlider, useGetTokenBalances - useGetTokens, useSortedRoutesWithQuotes, useTokenList - useTokenListWithSearch, useTrackTransfer, useTransactionHistory - useTransactionHistoryLiFi, useTransactionHistoryMayan, useTransactionHistoryWHScan
1 parent b6aac60 commit 23ab3b7

23 files changed

Lines changed: 489 additions & 321 deletions

src/hooks/useAutoEnableGasDropoff.test.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import { describe, it, expect, vi, beforeEach } from 'vitest';
33
import { renderHook } from '@testing-library/react';
44
import { Provider } from 'react-redux';
55
import { configureStore } from '@reduxjs/toolkit';
66
import { amount } from '@wormhole-foundation/sdk';
77
import { useAutoEnableGasDropOff } from './useAutoEnableGasDropoff';
88
import * as relayActions from 'store/relay';
9+
import { TestConfigContext } from 'utils/testHelpers';
910

10-
vi.mock('config', () => ({
11-
default: {
12-
tokens: {
13-
getGasToken: vi.fn(() => ({ key: 'ETH', symbol: 'ETH' })),
14-
},
11+
// Mock config object provided via TestConfigContext
12+
const mockConfig = {
13+
tokens: {
14+
getGasToken: vi.fn(() => ({ key: 'ETH', symbol: 'ETH' })),
15+
},
16+
};
17+
18+
// Mock useConfig to read from TestConfigContext
19+
vi.mock('contexts/ConfigContext', () => ({
20+
useConfig: () => {
21+
const context = React.useContext(TestConfigContext);
22+
if (!context) {
23+
throw new Error('useConfig must be used within a ConfigProvider');
24+
}
25+
return context;
1526
},
1627
}));
1728

@@ -34,7 +45,9 @@ describe('useAutoEnableGasDropoff', () => {
3445
});
3546

3647
const wrapper = ({ children }: { children: React.ReactNode }) => (
37-
<Provider store={mockStore}>{children}</Provider>
48+
<TestConfigContext.Provider value={mockConfig}>
49+
<Provider store={mockStore}>{children}</Provider>
50+
</TestConfigContext.Provider>
3851
);
3952

4053
it('enables gas dropoff when destination has no native balance and chain is allowed', () => {

src/hooks/useAutoEnableGasDropoff.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useDispatch } from 'react-redux';
33
import { amount as sdkAmount } from '@wormhole-foundation/sdk';
44
import type { Chain } from '@wormhole-foundation/sdk';
55
import type { Balances } from 'utils/wallet/types';
6-
import config from 'config';
6+
import { useConfig } from 'contexts/ConfigContext';
77
import { setToNativeToken } from 'store/relay';
88
import { isExecutorRoute } from 'utils';
99

@@ -34,6 +34,7 @@ export function useAutoEnableGasDropOff({
3434
isFetchingBalances,
3535
allowedChains,
3636
}: UseAutoEnableGasDropOffParams): void {
37+
const config = useConfig();
3738
const dispatch = useDispatch();
3839

3940
useEffect(() => {
@@ -81,6 +82,7 @@ export function useAutoEnableGasDropOff({
8182
dispatch(setToNativeToken(newGasValue));
8283
}
8384
}, [
85+
config,
8486
route,
8587
destChain,
8688
destinationBalances,

src/hooks/useAvailableWallets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';
33
import type { Chain, Platform } from '@wormhole-foundation/sdk';
44
import { chainToPlatform } from '@wormhole-foundation/sdk';
55

6-
import config from 'config';
6+
import { useConfig } from 'contexts/ConfigContext';
77
import type { WalletData } from 'utils/wallet';
88
import { getWalletOptions } from 'utils/wallet';
99

@@ -26,6 +26,7 @@ const FAILED_TO_LOAD_ERR =
2626
'Failed to load wallets. Please refresh and try again.';
2727

2828
export const useAvailableWallets = (props: Props): ReturnProps => {
29+
const config = useConfig();
2930
const { chain, supportedChains } = props;
3031

3132
const [walletOptionsResult, setWalletOptionsResult] = useState<WalletOptions>(
@@ -69,7 +70,7 @@ export const useAvailableWallets = (props: Props): ReturnProps => {
6970
return () => {
7071
cancelled = true;
7172
};
72-
}, [chain, supportedChains]);
73+
}, [chain, supportedChains, config.chains]);
7374

7475
return {
7576
walletOptionsResult,

src/hooks/useComputeDestinationTokens.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useCallback, useEffect, useState } from 'react';
22
import { useDispatch } from 'react-redux';
33

4-
import config from 'config';
4+
import { useConfig } from 'contexts/ConfigContext';
5+
import type { ConfigContextType } from 'contexts/ConfigContext';
56
import { setDestToken } from 'store/transferInput';
67

78
import type { Token } from 'config/tokens';
@@ -28,6 +29,7 @@ type ReturnProps = {
2829
* 3. If both chains selected AND a source token: fetch supported destination tokens from routes
2930
*/
3031
const computeDestTokensForChains = async (
32+
config: ConfigContextType,
3133
sourceChain: Chain | undefined,
3234
destChain: Chain | undefined,
3335
sourceToken: Token | undefined,
@@ -57,6 +59,7 @@ const computeDestTokensForChains = async (
5759
};
5860

5961
const useComputeDestinationTokens = (props: Props): ReturnProps => {
62+
const config = useConfig();
6063
const { sourceChain, destChain, sourceToken } = props;
6164

6265
const dispatch = useDispatch();
@@ -71,6 +74,7 @@ const useComputeDestinationTokens = (props: Props): ReturnProps => {
7174

7275
try {
7376
const supported = await computeDestTokensForChains(
77+
config,
7478
sourceChain,
7579
destChain,
7680
sourceToken,
@@ -86,7 +90,7 @@ const useComputeDestinationTokens = (props: Props): ReturnProps => {
8690
} finally {
8791
setIsFetching(false);
8892
}
89-
}, [sourceToken, sourceChain, destChain, dispatch, getOrFetchToken]);
93+
}, [config, sourceToken, sourceChain, destChain, dispatch, getOrFetchToken]);
9094

9195
useEffect(() => {
9296
computeDestTokens();

src/hooks/useConfirmTransaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useContext, useState, useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

4-
import config from 'config';
4+
import { useConfig } from 'contexts/ConfigContext';
55
import { RouteContext } from 'contexts/RouteContext';
66
import useWalletProvider from 'hooks/useWalletProvider';
77
import { useUSDamountGetter } from 'hooks/useUSDamountGetter';
@@ -42,6 +42,7 @@ type ReturnProps = {
4242
};
4343

4444
const useConfirmTransaction = (props: Props): ReturnProps => {
45+
const config = useConfig();
4546
const dispatch = useDispatch();
4647

4748
const [error, setError] = useState<string | undefined>(undefined);

src/hooks/useExternalSearch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Chain } from '@wormhole-foundation/sdk';
2-
import config from 'config';
2+
import { useConfig } from 'contexts/ConfigContext';
33
import { useEffect } from 'react';
44
import { useDispatch, useSelector } from 'react-redux';
55
import type { RootState } from 'store';
@@ -13,6 +13,7 @@ type ExternalSearch = {
1313
};
1414

1515
export function useExternalSearch(): ExternalSearch {
16+
const config = useConfig();
1617
const dispatch = useDispatch();
1718
const { txHash, chain } = useSelector((state: RootState) => state.search);
1819

@@ -32,7 +33,7 @@ export function useExternalSearch(): ExternalSearch {
3233
);
3334
}
3435
}
35-
}, [dispatch]);
36+
}, [dispatch, config.chainsArr, config.ui.searchTx]);
3637

3738
return {
3839
hasExternalSearch: !!(txHash && chain),

src/hooks/useFetchQuotes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Wormhole, circle, amount } from '@wormhole-foundation/sdk';
99
import type { QuoteParams, QuoteResult } from 'routes/operator';
1010
import { calculateUSDPriceRaw } from 'utils';
1111

12-
import config from 'config';
12+
import { useConfig } from 'contexts/ConfigContext';
1313
import type { Token } from 'config/tokens';
1414
import { useTokens } from 'contexts/TokensContext';
1515

@@ -35,6 +35,7 @@ const MAYAN_BETA_PROTOCOL_LIMITS = {
3535
};
3636

3737
export default (routes: string[], params: Params): HookReturn => {
38+
const config = useConfig();
3839
const [nonce, setNonce] = useState(new Date().valueOf());
3940
const refreshTimeout = useRef<undefined | ReturnType<typeof setTimeout>>(
4041
undefined,
@@ -120,7 +121,7 @@ export default (routes: string[], params: Params): HookReturn => {
120121
clearTimeout(refreshTimeout.current);
121122
}
122123
};
123-
}, [unfilteredQuotes, routes, params]);
124+
}, [unfilteredQuotes, routes, params, config]);
124125

125126
// IMPORTANT
126127
//
@@ -417,8 +418,7 @@ export default (routes: string[], params: Params): HookReturn => {
417418
}
418419

419420
return { quotes: filtered, failedQuotes };
420-
// eslint-disable-next-line react-hooks/exhaustive-deps
421-
}, [unfilteredQuotes]);
421+
}, [unfilteredQuotes, config, getTokenPrice, params]);
422422

423423
return {
424424
quotes,

src/hooks/useFetchSupportedRoutes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import config from 'config';
2+
import { useConfig } from 'contexts/ConfigContext';
33
import { getTokenDetails } from 'telemetry';
44
import { maybeLogSdkError } from 'utils/errors';
55
import { ReadOnlyWallet } from 'utils/wallet/ReadOnlyWallet';
@@ -29,6 +29,7 @@ const useFetchSupportedRoutes = ({
2929
toNativeToken,
3030
receivingWallet,
3131
}: UseFetchSupportedRoutesArgs): HookReturn => {
32+
const config = useConfig();
3233
const [routes, setRoutes] = useState<string[]>([]);
3334
const [isFetching, setIsFetching] = useState<boolean>(false);
3435

@@ -110,6 +111,7 @@ const useFetchSupportedRoutes = ({
110111
isActive = false;
111112
};
112113
}, [
114+
config,
113115
sourceToken,
114116
destToken,
115117
fromChain,

src/hooks/useGasSlider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import config from 'config';
1+
import { useConfig } from 'contexts/ConfigContext';
22
import type { Token } from 'config/tokens';
33

44
type Props = {
@@ -13,6 +13,7 @@ export const useGasSlider = (
1313
disabled: boolean;
1414
showGasSlider: boolean | undefined;
1515
} => {
16+
const config = useConfig();
1617
const { destToken, route, isTransactionInProgress } = props;
1718

1819
const disabled = isTransactionInProgress;

src/hooks/useGetTokenBalances.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState, useRef, useCallback, useMemo } from 'react';
22
import type { Balances } from 'utils/wallet/types';
3-
import config, { getWormholeContextV2 } from 'config';
3+
import { getWormholeContextV2 } from 'config';
4+
import { useConfig } from 'contexts/ConfigContext';
45
import type { Token } from 'config/tokens';
56
import { chainToPlatform } from '@wormhole-foundation/sdk-base';
67
import type { Chain } from '@wormhole-foundation/sdk';
@@ -51,6 +52,7 @@ const useGetTokenBalances = ({
5152
source,
5253
destination,
5354
}: UseGetTokenBalancesParams): UseGetTokenBalancesResult => {
55+
const config = useConfig();
5456
const [balances, setBalances] = useState<BalanceMap>({});
5557
const [fetchingKeys, setFetchingKeys] = useState<Set<string>>(new Set());
5658

@@ -145,7 +147,7 @@ const useGetTokenBalances = ({
145147
}
146148
}
147149
},
148-
[getOrFetchToken],
150+
[config, getOrFetchToken],
149151
);
150152

151153
// Fetch balances for a single chain
@@ -258,7 +260,7 @@ const useGetTokenBalances = ({
258260

259261
return updatedBalances;
260262
},
261-
[processIndexerResults],
263+
[config, processIndexerResults],
262264
);
263265

264266
// Effect to fetch balances

0 commit comments

Comments
 (0)