Skip to content

Commit

Permalink
Refactor useSearchDao to use resolveENSName for address resolution an…
Browse files Browse the repository at this point in the history
…d update related logic
  • Loading branch information
Da-Colon committed Dec 16, 2024
1 parent 8fc85e2 commit 98d5e0a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/hooks/DAO/useSearchDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ResolvedAddressWithPrefix = {
};
export const useSearchDao = () => {
const { t } = useTranslation('dashboard');
const { resolveAddressMultiChain, isLoading: isAddressLoading } = useResolveENSName();
const { resolveENSName, isLoading: isAddressLoading } = useResolveENSName();
const [searchString, setSearchString] = useState<string>('');
const [errorMessage, setErrorMessage] = useState<string>();

Expand Down Expand Up @@ -42,7 +42,7 @@ export const useSearchDao = () => {

const resolveInput = useCallback(
async (input: string) => {
const { resolvedAddress, isValid } = await resolveAddressMultiChain(input);
const { resolvedAddress, isValid } = await resolveENSName(input);
if (isValid) {
await findSafes(
supportedNetworks.map(network => ({
Expand All @@ -54,7 +54,7 @@ export const useSearchDao = () => {
setErrorMessage('Invalid search');
}
},
[findSafes, resolveAddressMultiChain],
[findSafes, resolveENSName],
);

useEffect(() => {
Expand Down
91 changes: 44 additions & 47 deletions src/hooks/utils/useResolveENSName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,54 @@ type ResolveENSNameReturnType = {
export const useResolveENSName = () => {
const [isLoading, setIsLoading] = useState<boolean>(false);

const resolveAddressMultiChain = useCallback(
async (input: string): Promise<ResolveENSNameReturnType> => {
setIsLoading(true);
const resolveENSName = useCallback(async (input: string): Promise<ResolveENSNameReturnType> => {
setIsLoading(true);

const returnedResult: ResolveENSNameReturnType = {
resolvedAddress: zeroAddress,
isValid: false,
};
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;
}
if (input === '') {
throw new Error('ENS name is empty');
}

// @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');
}
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;
}

const mainnetPublicClient = createPublicClient({
chain: mainnet.chain,
transport: http(mainnet.rpcEndpoint),
});
try {
const resolvedAddress = await mainnetPublicClient.getEnsAddress({ name: normalizedName });
if (resolvedAddress) {
returnedResult.resolvedAddress = resolvedAddress;
returnedResult.isValid = true;
}
} catch {
// do nothing
}
// @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;
},
[],
);
return { resolveAddressMultiChain, isLoading };
}
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),
});
try {
const resolvedAddress = await mainnetPublicClient.getEnsAddress({ name: normalizedName });
if (resolvedAddress) {
returnedResult.resolvedAddress = resolvedAddress;
returnedResult.isValid = true;
}
} catch {
// do nothing
}
setIsLoading(false);
return returnedResult;
}, []);
return { resolveENSName, isLoading };
};

0 comments on commit 98d5e0a

Please sign in to comment.