From d316f30f501b8e5c833bea00907645796b5a6019 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 15 Aug 2022 20:44:48 -0300 Subject: [PATCH] Use useHasCode hook instead --- src/Address.tsx | 25 ++++++------------------- src/useErigonHooks.ts | 38 -------------------------------------- 2 files changed, 6 insertions(+), 57 deletions(-) diff --git a/src/Address.tsx b/src/Address.tsx index b8767c8..583966f 100644 --- a/src/Address.tsx +++ b/src/Address.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useContext, useCallback, useMemo } from "react"; +import React, { useEffect, useContext, useCallback } from "react"; import { useParams, useNavigate, @@ -25,7 +25,7 @@ import { useAppConfigContext } from "./useAppConfig"; import { useAddressOrENS } from "./useResolvedAddresses"; import { useSourcifyMetadata } from "./sourcify/useSourcify"; import { ChecksummedAddress } from "./types"; -import { useAddressesWithCode } from "./useErigonHooks"; +import { useHasCode } from "./useErigonHooks"; import { useChainInfo } from "./useChainInfo"; const AddressTransactionByNonce = React.lazy( @@ -65,18 +65,10 @@ const Address: React.FC = () => { } }, [addressOrName, checksummedAddress, isENS]); - const checksummedAddressAsArray = useMemo( - () => (checksummedAddress !== undefined ? [checksummedAddress] : []), - [checksummedAddress] - ); - const contractAddresses = useAddressesWithCode( - provider, - checksummedAddressAsArray - ); - // TODO: restore filter to only check Sourcify metadata if the address is a contract (hasCode) + const hasCode = useHasCode(provider, checksummedAddress, "latest"); const { sourcifySource } = useAppConfigContext(); const addressMetadata = useSourcifyMetadata( - checksummedAddress, + hasCode ? checksummedAddress : undefined, provider?.network.chainId, sourcifySource ); @@ -130,7 +122,7 @@ const Address: React.FC = () => { Overview - {(contractAddresses?.length ?? 0) > 0 && ( + {hasCode && ( { element={ } /> diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index 79745f4..6382877 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -427,44 +427,6 @@ export const useTraceTransaction = ( return traceGroups; }; -const hasCode = async ( - provider: JsonRpcProvider, - address: ChecksummedAddress -): Promise => { - const result = await provider.send("ots_hasCode", [address, "latest"]); - return result as boolean; -}; - -export const useAddressesWithCode = ( - provider: JsonRpcProvider | undefined, - addresses: ChecksummedAddress[] -): ChecksummedAddress[] | undefined => { - const [results, setResults] = useState(); - - useEffect(() => { - // Reset - setResults(undefined); - - if (provider === undefined) { - return; - } - - const readCodes = async () => { - const checkers: Promise[] = []; - for (const a of addresses) { - checkers.push(hasCode(provider, a)); - } - - const result = await Promise.all(checkers); - const filtered = addresses.filter((_, i) => result[i]); - setResults(filtered); - }; - readCodes(); - }, [provider, addresses]); - - return results; -}; - // Error(string) const ERROR_MESSAGE_SELECTOR = "0x08c379a0";