-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2637 from decentdao/issue/2565-resolve-mainnet-ens
Refactor `useResolveAddressMultichain` to `useResolveENSName`
- Loading branch information
Showing
3 changed files
with
71 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |