-
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 #2609 from decentdao/issue/2565-network-config-ext…
…ended `[Issue #2565]` network config extended
- Loading branch information
Showing
11 changed files
with
188 additions
and
47 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 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 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 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 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 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 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,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 }; | ||
}; |
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 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 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 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