Use new custom API to server-side check if address has code/is contract

This commit is contained in:
Willian Mitsuda 2021-12-02 15:34:06 -03:00
parent c3dfcfeee3
commit 2e04c726d7
6 changed files with 18 additions and 21 deletions

View File

@ -12,7 +12,7 @@ import {
useDedupedAddresses, useDedupedAddresses,
useMultipleMetadata, useMultipleMetadata,
} from "../sourcify/useSourcify"; } from "../sourcify/useSourcify";
import { useAddressCodes } from "../useErigonHooks"; import { useAddressesWithCode } from "../useErigonHooks";
import { useMultipleETHUSDOracle } from "../usePriceOracle"; import { useMultipleETHUSDOracle } from "../usePriceOracle";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import { pageCollector, useResolvedAddresses } from "../useResolvedAddresses"; import { pageCollector, useResolvedAddresses } from "../useResolvedAddresses";
@ -125,7 +125,7 @@ const AddressTransactionResults: React.FC<AddressTransactionResultsProps> = ({
}, [address, page]); }, [address, page]);
const { sourcifySource } = useAppConfigContext(); const { sourcifySource } = useAppConfigContext();
const deduped = useDedupedAddresses(addresses); const deduped = useDedupedAddresses(addresses);
const checked = useAddressCodes(provider, deduped); const checked = useAddressesWithCode(provider, deduped);
const metadatas = useMultipleMetadata( const metadatas = useMultipleMetadata(
undefined, undefined,
checked, checked,

View File

@ -17,7 +17,7 @@ import {
useDedupedAddresses, useDedupedAddresses,
useMultipleMetadata, useMultipleMetadata,
} from "../sourcify/useSourcify"; } from "../sourcify/useSourcify";
import { useAddressCodes } from "../useErigonHooks"; import { useAddressesWithCode } from "../useErigonHooks";
type BlockTransactionResultsProps = { type BlockTransactionResultsProps = {
blockTag: BlockTag; blockTag: BlockTag;
@ -49,7 +49,7 @@ const BlockTransactionResults: React.FC<BlockTransactionResultsProps> = ({
}, [page]); }, [page]);
const { sourcifySource } = useAppConfigContext(); const { sourcifySource } = useAppConfigContext();
const deduped = useDedupedAddresses(addresses); const deduped = useDedupedAddresses(addresses);
const checked = useAddressCodes(provider, deduped); const checked = useAddressesWithCode(provider, deduped);
const metadatas = useMultipleMetadata( const metadatas = useMultipleMetadata(
undefined, undefined,
checked, checked,

View File

@ -1,3 +1,3 @@
export const MIN_API_LEVEL = 3; export const MIN_API_LEVEL = 4;
export const PAGE_SIZE = 25; export const PAGE_SIZE = 25;

View File

@ -43,7 +43,7 @@ import {
useMultipleMetadata, useMultipleMetadata,
UserDoc, UserDoc,
} from "../sourcify/useSourcify"; } from "../sourcify/useSourcify";
import { useAddressCodes } from "../useErigonHooks"; import { useAddressesWithCode } from "../useErigonHooks";
import { ResolvedAddresses } from "../api/address-resolver"; import { ResolvedAddresses } from "../api/address-resolver";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import { useAppConfigContext } from "../useAppConfig"; import { useAppConfigContext } from "../useAppConfig";
@ -103,7 +103,7 @@ const Details: React.FC<DetailsProps> = ({
}, [txData]); }, [txData]);
const { sourcifySource } = useAppConfigContext(); const { sourcifySource } = useAppConfigContext();
const deduped = useDedupedAddresses(addresses); const deduped = useDedupedAddresses(addresses);
const checked = useAddressCodes(provider, deduped); const checked = useAddressesWithCode(provider, deduped);
const metadatas = useMultipleMetadata( const metadatas = useMultipleMetadata(
undefined, undefined,
checked, checked,

View File

@ -9,7 +9,7 @@ import {
useDedupedAddresses, useDedupedAddresses,
useMultipleMetadata, useMultipleMetadata,
} from "../sourcify/useSourcify"; } from "../sourcify/useSourcify";
import { useAddressCodes } from "../useErigonHooks"; import { useAddressesWithCode } from "../useErigonHooks";
import { ResolvedAddresses } from "../api/address-resolver"; import { ResolvedAddresses } from "../api/address-resolver";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
@ -37,7 +37,7 @@ const Logs: React.FC<LogsProps> = ({ txData, metadata, resolvedAddresses }) => {
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const { sourcifySource } = useAppConfigContext(); const { sourcifySource } = useAppConfigContext();
const deduped = useDedupedAddresses(logAddresses); const deduped = useDedupedAddresses(logAddresses);
const checked = useAddressCodes(provider, deduped); const checked = useAddressesWithCode(provider, deduped);
const metadatas = useMultipleMetadata( const metadatas = useMultipleMetadata(
baseMetadatas, baseMetadatas,
checked, checked,

View File

@ -442,41 +442,38 @@ export const useUniqueSignatures = (traces: TraceGroup[] | undefined) => {
return uniqueSignatures; return uniqueSignatures;
}; };
const checkCode = async ( const hasCode = async (
provider: JsonRpcProvider, provider: JsonRpcProvider,
address: ChecksummedAddress address: ChecksummedAddress
): Promise<boolean> => { ): Promise<boolean> => {
const code = await provider.getCode(address); const result = await provider.send("ots_hasCode", [address, "latest"]);
if (code !== "0x") { return result as boolean;
console.log(`Has code: ${address}`);
}
return code === "0x";
}; };
export const useAddressCodes = ( export const useAddressesWithCode = (
provider: JsonRpcProvider | undefined, provider: JsonRpcProvider | undefined,
addresses: ChecksummedAddress[] addresses: ChecksummedAddress[]
): ChecksummedAddress[] | undefined => { ): ChecksummedAddress[] | undefined => {
const [hasCode, setCode] = useState<ChecksummedAddress[] | undefined>(); const [results, setResults] = useState<ChecksummedAddress[] | undefined>();
useEffect(() => { useEffect(() => {
if (provider === undefined) { if (provider === undefined) {
setCode(undefined); setResults(undefined);
return; return;
} }
const readCodes = async () => { const readCodes = async () => {
const checkers: Promise<boolean>[] = []; const checkers: Promise<boolean>[] = [];
for (const a of addresses) { for (const a of addresses) {
checkers.push(checkCode(provider, a)); checkers.push(hasCode(provider, a));
} }
const result = await Promise.all(checkers); const result = await Promise.all(checkers);
const filtered = addresses.filter((_, i) => result[i]); const filtered = addresses.filter((_, i) => result[i]);
setCode(filtered); setResults(filtered);
}; };
readCodes(); readCodes();
}, [provider, addresses]); }, [provider, addresses]);
return hasCode; return results;
}; };