-
Notifications
You must be signed in to change notification settings - Fork 7
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
Da-Colon
merged 5 commits into
issue/2565-network-config-updates
from
issue/2565-resolve-mainnet-ens
Dec 16, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c44efe8
Refactor useResolveAddressMultiChain to utilize mainnet client for EN…
Da-Colon 6cdc66e
Rename useResolveAddressMultiChain to useResolveENSName and refactor …
Da-Colon 8fc85e2
Refactor useSearchDao to utilize useResolveENSName for address resolu…
Da-Colon 98d5e0a
Refactor useSearchDao to use resolveENSName for address resolution an…
Da-Colon 30b14dc
remove try/catch
Da-Colon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,63 @@ | ||
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), | ||
}); | ||
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 }; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this try/catch is necessary any longer. I believe it was here previously to catch the error in
.getEnsAddress
when the call was made on a network that didn't ahve the ENS contracts deployed (all of them except for mainnet and sepolia IIRC).