Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor useResolveAddressMultichain to useResolveENSName #2637

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
};
Loading