Use useHasCode hook instead

This commit is contained in:
Willian Mitsuda 2022-08-15 20:44:48 -03:00
parent 003d0ae5b8
commit d316f30f50
No known key found for this signature in database
2 changed files with 6 additions and 57 deletions

View File

@ -1,4 +1,4 @@
import React, { useEffect, useContext, useCallback, useMemo } from "react"; import React, { useEffect, useContext, useCallback } from "react";
import { import {
useParams, useParams,
useNavigate, useNavigate,
@ -25,7 +25,7 @@ import { useAppConfigContext } from "./useAppConfig";
import { useAddressOrENS } from "./useResolvedAddresses"; import { useAddressOrENS } from "./useResolvedAddresses";
import { useSourcifyMetadata } from "./sourcify/useSourcify"; import { useSourcifyMetadata } from "./sourcify/useSourcify";
import { ChecksummedAddress } from "./types"; import { ChecksummedAddress } from "./types";
import { useAddressesWithCode } from "./useErigonHooks"; import { useHasCode } from "./useErigonHooks";
import { useChainInfo } from "./useChainInfo"; import { useChainInfo } from "./useChainInfo";
const AddressTransactionByNonce = React.lazy( const AddressTransactionByNonce = React.lazy(
@ -65,18 +65,10 @@ const Address: React.FC = () => {
} }
}, [addressOrName, checksummedAddress, isENS]); }, [addressOrName, checksummedAddress, isENS]);
const checksummedAddressAsArray = useMemo( const hasCode = useHasCode(provider, checksummedAddress, "latest");
() => (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 { sourcifySource } = useAppConfigContext(); const { sourcifySource } = useAppConfigContext();
const addressMetadata = useSourcifyMetadata( const addressMetadata = useSourcifyMetadata(
checksummedAddress, hasCode ? checksummedAddress : undefined,
provider?.network.chainId, provider?.network.chainId,
sourcifySource sourcifySource
); );
@ -130,7 +122,7 @@ const Address: React.FC = () => {
<Tab.Group> <Tab.Group>
<Tab.List className="flex space-x-2 border-l border-r border-t rounded-t-lg bg-white"> <Tab.List className="flex space-x-2 border-l border-r border-t rounded-t-lg bg-white">
<NavTab href={`/address/${addressOrName}`}>Overview</NavTab> <NavTab href={`/address/${addressOrName}`}>Overview</NavTab>
{(contractAddresses?.length ?? 0) > 0 && ( {hasCode && (
<NavTab href={`/address/${addressOrName}/contract`}> <NavTab href={`/address/${addressOrName}/contract`}>
<span <span
className={`flex items-baseline space-x-2 ${ className={`flex items-baseline space-x-2 ${
@ -177,12 +169,7 @@ const Address: React.FC = () => {
element={ element={
<Contracts <Contracts
checksummedAddress={checksummedAddress} checksummedAddress={checksummedAddress}
rawMetadata={ rawMetadata={addressMetadata}
contractAddresses !== undefined &&
contractAddresses.length === 0
? null
: addressMetadata
}
/> />
} }
/> />

View File

@ -427,44 +427,6 @@ export const useTraceTransaction = (
return traceGroups; return traceGroups;
}; };
const hasCode = async (
provider: JsonRpcProvider,
address: ChecksummedAddress
): Promise<boolean> => {
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<ChecksummedAddress[] | undefined>();
useEffect(() => {
// Reset
setResults(undefined);
if (provider === undefined) {
return;
}
const readCodes = async () => {
const checkers: Promise<boolean>[] = [];
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) // Error(string)
const ERROR_MESSAGE_SELECTOR = "0x08c379a0"; const ERROR_MESSAGE_SELECTOR = "0x08c379a0";