|
1 | | -import { useCallback, useState } from 'react' |
2 | | -import { isAddress, isAddressEqual, type Address } from 'viem' |
| 1 | +import { useMemo } from 'react' |
| 2 | +import { type Address } from 'viem' |
3 | 3 | import { NG_ASSET_TYPE } from '@/dex/components/PageCreatePool/constants' |
4 | 4 | import { useIsErc4626 } from '@/dex/components/PageCreatePool/hooks/useIsErc4626' |
5 | | -import { TokenId, TokenState, TokensInPoolState } from '@/dex/components/PageCreatePool/types' |
6 | | -import { DEFAULT_ERC4626_STATUS } from '@/dex/store/createCreatePoolSlice' |
| 5 | +import { TokenId } from '@/dex/components/PageCreatePool/types' |
7 | 6 | import useStore from '@/dex/store/useStore' |
8 | 7 |
|
9 | | -type Candidate = { |
| 8 | +type UseAutoDetectErc4626Params = { |
10 | 9 | tokenId: TokenId |
11 | 10 | address: Address |
12 | 11 | } |
13 | 12 |
|
14 | | -type UseAutoDetectErc4626Params = { |
15 | | - tokensInPool: TokensInPoolState |
16 | | -} |
17 | | - |
18 | 13 | /** |
19 | 14 | * This hook is used to automatically detect if a token is an ERC4626 token and set the asset type to ERC4626 |
20 | | - * @param tokensInPool - The tokens in the pool |
21 | | - * @returns A function to schedule a check for an ERC4626 token |
22 | 15 | */ |
23 | | -export const useAutoDetectErc4626 = ({ tokensInPool }: UseAutoDetectErc4626Params) => { |
| 16 | +export const useAutoDetectErc4626 = ({ tokenId, address }: UseAutoDetectErc4626Params) => { |
24 | 17 | const updateNgAssetType = useStore((state) => state.createPool.updateNgAssetType) |
25 | 18 | const updateTokenErc4626Status = useStore((state) => state.createPool.updateTokenErc4626Status) |
26 | | - const [candidate, setCandidate] = useState<Candidate | null>(null) |
27 | | - const { isErc4626, isLoading, error, isSuccess } = useIsErc4626({ address: candidate?.address }) |
28 | | - |
29 | | - const setStatus = useCallback( |
30 | | - (tokenId: TokenId, status: TokenState['erc4626']) => { |
31 | | - updateTokenErc4626Status(tokenId, status) |
32 | | - }, |
33 | | - [updateTokenErc4626Status], |
34 | | - ) |
35 | | - |
36 | | - const scheduleCheck = useCallback( |
37 | | - (tokenId: TokenId, address: Address) => { |
38 | | - setCandidate({ tokenId, address }) |
39 | | - setStatus(tokenId, { |
40 | | - ...DEFAULT_ERC4626_STATUS, |
41 | | - isLoading: true, |
42 | | - }) |
43 | | - }, |
44 | | - [setStatus], |
45 | | - ) |
46 | | - |
47 | | - if (candidate) { |
48 | | - const currentToken = tokensInPool[candidate.tokenId] |
49 | | - if (!currentToken) { |
50 | | - setCandidate(null) |
51 | | - } else { |
52 | | - const nextStatus: TokenState['erc4626'] = { |
53 | | - isErc4626: Boolean(isErc4626 && isSuccess), |
54 | | - isLoading, |
55 | | - error, |
56 | | - isSuccess, |
57 | | - } |
58 | | - |
59 | | - const currentStatus = currentToken.erc4626 |
60 | | - const statusChanged = |
61 | | - currentStatus.isErc4626 !== nextStatus.isErc4626 || |
62 | | - currentStatus.isLoading !== nextStatus.isLoading || |
63 | | - currentStatus.error !== nextStatus.error || |
64 | | - currentStatus.isSuccess !== nextStatus.isSuccess |
65 | | - |
66 | | - if (statusChanged) { |
67 | | - setStatus(candidate.tokenId, nextStatus) |
68 | | - } |
69 | | - |
70 | | - if (!isLoading) { |
71 | | - const currentAddress = tokensInPool[candidate.tokenId].address |
72 | | - const addressesMatch = isAddress(currentAddress) && isAddressEqual(currentAddress, candidate.address) |
73 | | - |
74 | | - if (!addressesMatch) { |
75 | | - if (currentStatus.isErc4626 || currentStatus.isLoading || currentStatus.error || currentStatus.isSuccess) { |
76 | | - setStatus(candidate.tokenId, { ...DEFAULT_ERC4626_STATUS }) |
77 | | - } |
78 | | - setCandidate(null) |
79 | | - } else { |
80 | | - if (isErc4626) { |
81 | | - updateNgAssetType(candidate.tokenId, NG_ASSET_TYPE.ERC4626) |
82 | | - } |
83 | | - setCandidate(null) |
84 | | - } |
85 | | - } |
| 19 | + // check if the status is already set to avoid overriding the user's choice when switching the order of tokens |
| 20 | + const statusAlreadySet = useStore((state) => state.createPool.tokensInPool[tokenId].erc4626.isSuccess) |
| 21 | + const { isErc4626, isLoading, error, isSuccess } = useIsErc4626({ address }) |
| 22 | + |
| 23 | + useMemo(() => { |
| 24 | + if (isErc4626 && isSuccess && !statusAlreadySet) { |
| 25 | + updateNgAssetType(tokenId, NG_ASSET_TYPE.ERC4626) |
| 26 | + updateTokenErc4626Status(tokenId, { isErc4626, isLoading, error, isSuccess }) |
86 | 27 | } |
87 | | - } |
88 | | - |
89 | | - return { |
90 | | - scheduleCheck, |
91 | | - } |
| 28 | + }, [tokenId, isErc4626, isSuccess, isLoading, error, updateNgAssetType, updateTokenErc4626Status, statusAlreadySet]) |
92 | 29 | } |
0 commit comments