Skip to content

Commit 29dd2db

Browse files
committed
refactor: simplify useAutoDetectErc4626
1 parent dd14632 commit 29dd2db

File tree

3 files changed

+21
-85
lines changed

3 files changed

+21
-85
lines changed

apps/main/src/dex/components/PageCreatePool/TokensInPool/SelectToken.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Key } from 'react'
22
import { styled } from 'styled-components'
3+
import { type Address } from 'viem'
34
import {
45
STABLESWAP,
56
CRYPTOSWAP,
@@ -13,6 +14,7 @@ import {
1314
TOKEN_H,
1415
NG_ASSET_TYPE,
1516
} from '@/dex/components/PageCreatePool/constants'
17+
import { useAutoDetectErc4626 } from '@/dex/components/PageCreatePool/hooks/useAutoDetectErc4626'
1618
import {
1719
CreateToken,
1820
TokenState,
@@ -61,6 +63,10 @@ const SelectToken = ({
6163
const clearToken = useStore((state) => state.createPool.clearToken)
6264
const tokensInPool = useStore((state) => state.createPool.tokensInPool)
6365
const { data: network } = useNetworkByChain({ chainId })
66+
void useAutoDetectErc4626({
67+
tokenId,
68+
address: token.address as Address,
69+
})
6470

6571
const getTokenName = (tokenId: TokenId) => {
6672
if (tokenId === TOKEN_D) return t`Token D`

apps/main/src/dex/components/PageCreatePool/TokensInPool/index.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import lodash from 'lodash'
22
import { useMemo, useCallback } from 'react'
33
import { styled } from 'styled-components'
4-
import type { Address } from 'viem'
54
import SwitchTokensButton from '@/dex/components/PageCreatePool/components/SwitchTokensButton'
65
import WarningBox from '@/dex/components/PageCreatePool/components/WarningBox'
76
import {
@@ -18,7 +17,6 @@ import {
1817
FXSWAP,
1918
NG_ASSET_TYPE,
2019
} from '@/dex/components/PageCreatePool/constants'
21-
import { useAutoDetectErc4626 } from '@/dex/components/PageCreatePool/hooks/useAutoDetectErc4626'
2220
import SelectToken from '@/dex/components/PageCreatePool/TokensInPool/SelectToken'
2321
import SetOracle from '@/dex/components/PageCreatePool/TokensInPool/SetOracle'
2422
import { CreateToken, TokenId, TokensInPoolState } from '@/dex/components/PageCreatePool/types'
@@ -55,7 +53,6 @@ const TokensInPool = ({ curve, chainId, haveSigner }: Props) => {
5553
const userBalances = useStore((state) => state.userBalances.userBalancesMapper)
5654
const { tokensMapper } = useTokensMapper(chainId)
5755
const nativeToken = curve.getNetworkConstants().NATIVE_TOKEN
58-
const { scheduleCheck: scheduleErc4626Check } = useAutoDetectErc4626({ tokensInPool })
5956
const {
6057
data: { createDisabledTokens, stableswapFactory, tricryptoFactory, twocryptoFactory },
6158
} = useNetworkByChain({ chainId })
@@ -523,12 +520,8 @@ const TokensInPool = ({ curve, chainId, haveSigner }: Props) => {
523520
updatedFormValues[TOKEN_G],
524521
updatedFormValues[TOKEN_H],
525522
)
526-
527-
if (updatedFormValues[name].address.toLowerCase() === normalizedValue) {
528-
scheduleErc4626Check(name, value as Address)
529-
}
530523
},
531-
[tokensInPool, updateTokensInPool, curve, findSymbol, swapType, basePools, updateNgAssetType, scheduleErc4626Check],
524+
[tokensInPool, updateTokensInPool, curve, findSymbol, swapType, basePools, updateNgAssetType],
532525
)
533526

534527
const addToken = useCallback(() => {
Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,29 @@
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'
33
import { NG_ASSET_TYPE } from '@/dex/components/PageCreatePool/constants'
44
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'
76
import useStore from '@/dex/store/useStore'
87

9-
type Candidate = {
8+
type UseAutoDetectErc4626Params = {
109
tokenId: TokenId
1110
address: Address
1211
}
1312

14-
type UseAutoDetectErc4626Params = {
15-
tokensInPool: TokensInPoolState
16-
}
17-
1813
/**
1914
* 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
2215
*/
23-
export const useAutoDetectErc4626 = ({ tokensInPool }: UseAutoDetectErc4626Params) => {
16+
export const useAutoDetectErc4626 = ({ tokenId, address }: UseAutoDetectErc4626Params) => {
2417
const updateNgAssetType = useStore((state) => state.createPool.updateNgAssetType)
2518
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 })
8627
}
87-
}
88-
89-
return {
90-
scheduleCheck,
91-
}
28+
}, [tokenId, isErc4626, isSuccess, isLoading, error, updateNgAssetType, updateTokenErc4626Status, statusAlreadySet])
9229
}

0 commit comments

Comments
 (0)