Skip to content

Commit

Permalink
Merge pull request #2637 from decentdao/issue/2565-resolve-mainnet-ens
Browse files Browse the repository at this point in the history
Refactor `useResolveAddressMultichain` to `useResolveENSName`
  • Loading branch information
Da-Colon authored Dec 16, 2024
2 parents cba45d2 + 30b14dc commit 5d4c6fd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 77 deletions.
16 changes: 11 additions & 5 deletions src/hooks/DAO/useSearchDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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;
chainId: number;
};
export const useSearchDao = () => {
const { t } = useTranslation('dashboard');
const { resolveAddressMultiChain, isLoading: isAddressLoading } = useResolveAddressMultiChain();
const { resolveENSName, isLoading: isAddressLoading } = useResolveENSName();
const [searchString, setSearchString] = useState<string>('');
const [errorMessage, setErrorMessage] = useState<string>();

Expand Down Expand Up @@ -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(() => {
Expand Down
72 changes: 0 additions & 72 deletions src/hooks/utils/useResolveAddressMultiChain.ts

This file was deleted.

60 changes: 60 additions & 0 deletions src/hooks/utils/useResolveENSName.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>(false);

const resolveENSName = useCallback(async (input: string): Promise<ResolveENSNameReturnType> => {
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 };
};

0 comments on commit 5d4c6fd

Please sign in to comment.