diff --git a/src/hooks/DAO/useSearchDao.ts b/src/hooks/DAO/useSearchDao.ts index 5b46fda4c..69c233119 100644 --- a/src/hooks/DAO/useSearchDao.ts +++ b/src/hooks/DAO/useSearchDao.ts @@ -2,7 +2,8 @@ import SafeApiKit from '@safe-global/api-kit'; import { useState, useEffect, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { Address } from 'viem'; -import { useResolveAddressMultiChain } from '../utils/useResolveAddressMultiChain'; +import { supportedNetworks } from '../../providers/NetworkConfig/useNetworkConfigStore'; +import { useResolveENSName } from '../utils/useResolveENSName'; type ResolvedAddressWithPrefix = { address: Address; @@ -10,7 +11,7 @@ type ResolvedAddressWithPrefix = { }; export const useSearchDao = () => { const { t } = useTranslation('dashboard'); - const { resolveAddressMultiChain, isLoading: isAddressLoading } = useResolveAddressMultiChain(); + const { resolveENSName, isLoading: isAddressLoading } = useResolveENSName(); const [searchString, setSearchString] = useState(''); const [errorMessage, setErrorMessage] = useState(); @@ -41,14 +42,19 @@ export const useSearchDao = () => { const resolveInput = useCallback( async (input: string) => { - const { resolved, isValid } = await resolveAddressMultiChain(input); + const { resolvedAddress, isValid } = await resolveENSName(input); if (isValid) { - await findSafes(resolved); + await findSafes( + supportedNetworks.map(network => ({ + address: resolvedAddress, + chainId: network.chain.id, + })), + ); } else { setErrorMessage('Invalid search'); } }, - [findSafes, resolveAddressMultiChain], + [findSafes, resolveENSName], ); useEffect(() => { diff --git a/src/hooks/utils/useResolveAddressMultiChain.ts b/src/hooks/utils/useResolveAddressMultiChain.ts deleted file mode 100644 index f3926b1e5..000000000 --- a/src/hooks/utils/useResolveAddressMultiChain.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { useState, useCallback } from 'react'; -import { Address, createPublicClient, http, isAddress, getAddress } from 'viem'; -import { normalize } from 'viem/ens'; -import { supportedNetworks } from '../../providers/NetworkConfig/useNetworkConfigStore'; - -type ResolveAddressReturnType = { - resolved: { - address: Address; - chainId: number; - }[]; - isValid: boolean; -}; -export const useResolveAddressMultiChain = () => { - const [isLoading, setIsLoading] = useState(false); - - const resolveAddressMultiChain = useCallback( - async (input: string): Promise => { - setIsLoading(true); - - const returnedResult: ResolveAddressReturnType = { - resolved: [], - isValid: false, - }; - - if (input === '') { - throw new Error('ENS name is empty'); - } - - if (isAddress(input)) { - // @dev if its a valid address, its valid on all networks - returnedResult.isValid = true; - returnedResult.resolved = supportedNetworks.map(network => ({ - address: getAddress(input), - chainId: network.chain.id, - })); - setIsLoading(false); - return returnedResult; - } - - // @dev if its not an address, try to resolve as possible ENS name on all networks - let normalizedName: string; - try { - normalizedName = normalize(input); - } catch { - setIsLoading(false); - return returnedResult; - } - for (const network of supportedNetworks) { - const client = createPublicClient({ - chain: network.chain, - transport: http(network.rpcEndpoint), - }); - try { - const resolvedAddress = await client.getEnsAddress({ name: normalizedName }); - if (resolvedAddress) { - returnedResult.resolved.push({ - address: resolvedAddress, - chainId: network.chain.id, - }); - returnedResult.isValid = true; - } - } catch { - // do nothing - } - } - setIsLoading(false); - return returnedResult; - }, - [], - ); - return { resolveAddressMultiChain, isLoading }; -}; diff --git a/src/hooks/utils/useResolveENSName.ts b/src/hooks/utils/useResolveENSName.ts new file mode 100644 index 000000000..7ba84734b --- /dev/null +++ b/src/hooks/utils/useResolveENSName.ts @@ -0,0 +1,60 @@ +import { useState, useCallback } from 'react'; +import { Address, createPublicClient, http, isAddress, getAddress, zeroAddress } from 'viem'; +import { normalize } from 'viem/ens'; +import { supportedNetworks } from '../../providers/NetworkConfig/useNetworkConfigStore'; + +type ResolveENSNameReturnType = { + resolvedAddress: Address; + isValid: boolean; +}; +export const useResolveENSName = () => { + const [isLoading, setIsLoading] = useState(false); + + const resolveENSName = useCallback(async (input: string): Promise => { + setIsLoading(true); + + const returnedResult: ResolveENSNameReturnType = { + resolvedAddress: zeroAddress, + isValid: false, + }; + + if (input === '') { + throw new Error('ENS name is empty'); + } + + if (isAddress(input)) { + // @dev if its a valid address, its valid on all networks + returnedResult.isValid = true; + returnedResult.resolvedAddress = getAddress(input); + setIsLoading(false); + return returnedResult; + } + + // @dev if its not an address, try to resolve as possible ENS name on all networks + let normalizedName: string; + try { + normalizedName = normalize(input); + } catch { + setIsLoading(false); + return returnedResult; + } + const mainnet = supportedNetworks.find(network => network.chain.id === 1); + if (!mainnet) { + throw new Error('Mainnet not found'); + } + + const mainnetPublicClient = createPublicClient({ + chain: mainnet.chain, + transport: http(mainnet.rpcEndpoint), + }); + const resolvedAddress = await mainnetPublicClient.getEnsAddress({ name: normalizedName }); + if (resolvedAddress) { + returnedResult.resolvedAddress = resolvedAddress; + returnedResult.isValid = true; + } + + setIsLoading(false); + return returnedResult; + }, []); + return { resolveENSName, isLoading }; +};