2022-01-13 19:54:08 +03:00
|
|
|
import { useSingleCallResult } from 'lib/hooks/multicall'
|
2021-12-02 21:35:39 +03:00
|
|
|
import { useMemo } from 'react'
|
2021-12-04 00:45:33 +03:00
|
|
|
import { safeNamehash } from 'utils/safeNamehash'
|
2021-12-02 21:35:39 +03:00
|
|
|
|
|
|
|
import isZero from '../utils/isZero'
|
|
|
|
import { useENSRegistrarContract, useENSResolverContract } from './useContract'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does a lookup for an ENS name to find its contenthash.
|
|
|
|
*/
|
|
|
|
export default function useENSContentHash(ensName?: string | null): { loading: boolean; contenthash: string | null } {
|
2021-12-04 00:45:33 +03:00
|
|
|
const ensNodeArgument = useMemo(() => [ensName === null ? undefined : safeNamehash(ensName)], [ensName])
|
2021-12-02 21:35:39 +03:00
|
|
|
const registrarContract = useENSRegistrarContract(false)
|
|
|
|
const resolverAddressResult = useSingleCallResult(registrarContract, 'resolver', ensNodeArgument)
|
|
|
|
const resolverAddress = resolverAddressResult.result?.[0]
|
|
|
|
const resolverContract = useENSResolverContract(
|
|
|
|
resolverAddress && isZero(resolverAddress) ? undefined : resolverAddress,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
const contenthash = useSingleCallResult(resolverContract, 'contenthash', ensNodeArgument)
|
|
|
|
|
2021-12-10 00:06:55 +03:00
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
contenthash: contenthash.result?.[0] ?? null,
|
|
|
|
loading: resolverAddressResult.loading || contenthash.loading,
|
|
|
|
}),
|
|
|
|
[contenthash.loading, contenthash.result, resolverAddressResult.loading]
|
|
|
|
)
|
2021-12-02 21:35:39 +03:00
|
|
|
}
|