-
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
[Issue #2565]
Cross-chain Safe search
#2630
Changes from 7 commits
113d28f
d2319e5
2bb83c1
8dab85b
ef7a3de
2118a5d
1e8292a
a25fd55
992c141
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New hook to handle multi-chain address resolving. similar to our similarly takes a string as an input, but instead of resolving a single chain, loops through all supported networks and resolves the input if its a valid ENS name. If input is an Address returns valid on all networks |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useState, useCallback } from 'react'; | ||
import { Address, createPublicClient, http, isAddress, getAddress } from 'viem'; | ||
import { normalize } from 'viem/ens'; | ||
import { supportedNetworks } from '../../providers/NetworkConfig/useNetworkConfigStore'; | ||
|
||
type ResolveAddressReturnType = { | ||
resolved: { | ||
address: Address; | ||
chainId: number; | ||
}[]; | ||
isValid: boolean; | ||
}; | ||
export const useResolveAddressMultiChain = () => { | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
const resolveAddressMultiChain = useCallback( | ||
async (input: string): Promise<ResolveAddressReturnType> => { | ||
setIsLoading(true); | ||
|
||
const returnedResult: ResolveAddressReturnType = { | ||
resolved: [], | ||
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.resolved = supportedNetworks.map(network => ({ | ||
address: getAddress(input), | ||
chainId: network.chain.id, | ||
})); | ||
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; | ||
} | ||
for (const network of supportedNetworks) { | ||
const client = createPublicClient({ | ||
chain: network.chain, | ||
transport: http(network.rpcEndpoint), | ||
}); | ||
try { | ||
const resolvedAddress = await client.getEnsAddress({ name: normalizedName }); | ||
if (resolvedAddress) { | ||
returnedResult.resolved.push({ | ||
address: resolvedAddress, | ||
chainId: network.chain.id, | ||
}); | ||
returnedResult.isValid = true; | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
setIsLoading(false); | ||
return returnedResult; | ||
}, | ||
[], | ||
); | ||
return { resolveAddressMultiChain, isLoading }; | ||
}; |
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 reverted changes I made to
useIsSafe
and instead of continue updating that hook for use here just pulled in what was needed and removed relying on conditionaluseEffect
and more intentionally call to resolve searchInput.