From 2e04c726d736f0fdc4fea7b96ecdc81ebb149b22 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Thu, 2 Dec 2021 15:34:06 -0300 Subject: [PATCH] Use new custom API to server-side check if address has code/is contract --- src/address/AddressTransactionResults.tsx | 4 ++-- src/block/BlockTransactionResults.tsx | 4 ++-- src/params.ts | 2 +- src/transaction/Details.tsx | 4 ++-- src/transaction/Logs.tsx | 4 ++-- src/useErigonHooks.ts | 21 +++++++++------------ 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/address/AddressTransactionResults.tsx b/src/address/AddressTransactionResults.tsx index 4c85c6c..e565427 100644 --- a/src/address/AddressTransactionResults.tsx +++ b/src/address/AddressTransactionResults.tsx @@ -12,7 +12,7 @@ import { useDedupedAddresses, useMultipleMetadata, } from "../sourcify/useSourcify"; -import { useAddressCodes } from "../useErigonHooks"; +import { useAddressesWithCode } from "../useErigonHooks"; import { useMultipleETHUSDOracle } from "../usePriceOracle"; import { RuntimeContext } from "../useRuntime"; import { pageCollector, useResolvedAddresses } from "../useResolvedAddresses"; @@ -125,7 +125,7 @@ const AddressTransactionResults: React.FC = ({ }, [address, page]); const { sourcifySource } = useAppConfigContext(); const deduped = useDedupedAddresses(addresses); - const checked = useAddressCodes(provider, deduped); + const checked = useAddressesWithCode(provider, deduped); const metadatas = useMultipleMetadata( undefined, checked, diff --git a/src/block/BlockTransactionResults.tsx b/src/block/BlockTransactionResults.tsx index 891637b..59d1ba6 100644 --- a/src/block/BlockTransactionResults.tsx +++ b/src/block/BlockTransactionResults.tsx @@ -17,7 +17,7 @@ import { useDedupedAddresses, useMultipleMetadata, } from "../sourcify/useSourcify"; -import { useAddressCodes } from "../useErigonHooks"; +import { useAddressesWithCode } from "../useErigonHooks"; type BlockTransactionResultsProps = { blockTag: BlockTag; @@ -49,7 +49,7 @@ const BlockTransactionResults: React.FC = ({ }, [page]); const { sourcifySource } = useAppConfigContext(); const deduped = useDedupedAddresses(addresses); - const checked = useAddressCodes(provider, deduped); + const checked = useAddressesWithCode(provider, deduped); const metadatas = useMultipleMetadata( undefined, checked, diff --git a/src/params.ts b/src/params.ts index 5c95cfa..7ac916a 100644 --- a/src/params.ts +++ b/src/params.ts @@ -1,3 +1,3 @@ -export const MIN_API_LEVEL = 3; +export const MIN_API_LEVEL = 4; export const PAGE_SIZE = 25; diff --git a/src/transaction/Details.tsx b/src/transaction/Details.tsx index 8799987..06e70d5 100644 --- a/src/transaction/Details.tsx +++ b/src/transaction/Details.tsx @@ -43,7 +43,7 @@ import { useMultipleMetadata, UserDoc, } from "../sourcify/useSourcify"; -import { useAddressCodes } from "../useErigonHooks"; +import { useAddressesWithCode } from "../useErigonHooks"; import { ResolvedAddresses } from "../api/address-resolver"; import { RuntimeContext } from "../useRuntime"; import { useAppConfigContext } from "../useAppConfig"; @@ -103,7 +103,7 @@ const Details: React.FC = ({ }, [txData]); const { sourcifySource } = useAppConfigContext(); const deduped = useDedupedAddresses(addresses); - const checked = useAddressCodes(provider, deduped); + const checked = useAddressesWithCode(provider, deduped); const metadatas = useMultipleMetadata( undefined, checked, diff --git a/src/transaction/Logs.tsx b/src/transaction/Logs.tsx index d907a37..acc2baa 100644 --- a/src/transaction/Logs.tsx +++ b/src/transaction/Logs.tsx @@ -9,7 +9,7 @@ import { useDedupedAddresses, useMultipleMetadata, } from "../sourcify/useSourcify"; -import { useAddressCodes } from "../useErigonHooks"; +import { useAddressesWithCode } from "../useErigonHooks"; import { ResolvedAddresses } from "../api/address-resolver"; import { RuntimeContext } from "../useRuntime"; @@ -37,7 +37,7 @@ const Logs: React.FC = ({ txData, metadata, resolvedAddresses }) => { const { provider } = useContext(RuntimeContext); const { sourcifySource } = useAppConfigContext(); const deduped = useDedupedAddresses(logAddresses); - const checked = useAddressCodes(provider, deduped); + const checked = useAddressesWithCode(provider, deduped); const metadatas = useMultipleMetadata( baseMetadatas, checked, diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index adbfce2..f722f03 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -442,41 +442,38 @@ export const useUniqueSignatures = (traces: TraceGroup[] | undefined) => { return uniqueSignatures; }; -const checkCode = async ( +const hasCode = async ( provider: JsonRpcProvider, address: ChecksummedAddress ): Promise => { - const code = await provider.getCode(address); - if (code !== "0x") { - console.log(`Has code: ${address}`); - } - return code === "0x"; + const result = await provider.send("ots_hasCode", [address, "latest"]); + return result as boolean; }; -export const useAddressCodes = ( +export const useAddressesWithCode = ( provider: JsonRpcProvider | undefined, addresses: ChecksummedAddress[] ): ChecksummedAddress[] | undefined => { - const [hasCode, setCode] = useState(); + const [results, setResults] = useState(); useEffect(() => { if (provider === undefined) { - setCode(undefined); + setResults(undefined); return; } const readCodes = async () => { const checkers: Promise[] = []; for (const a of addresses) { - checkers.push(checkCode(provider, a)); + checkers.push(hasCode(provider, a)); } const result = await Promise.all(checkers); const filtered = addresses.filter((_, i) => result[i]); - setCode(filtered); + setResults(filtered); }; readCodes(); }, [provider, addresses]); - return hasCode; + return results; };