diff --git a/src/types.ts b/src/types.ts index 12763bb..66ccebe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -67,6 +67,9 @@ export type ConfirmedTransactionData = { // The VOID... export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; +// TODO: replace all occurrences of plain string +export type ChecksummedAddress = string; + export enum AddressContext { FROM, TO, diff --git a/src/url.ts b/src/url.ts index 647fc42..471c9d8 100644 --- a/src/url.ts +++ b/src/url.ts @@ -1,4 +1,5 @@ import { BlockTag } from "@ethersproject/abstract-provider"; +import { ChecksummedAddress } from "./types"; export const fourBytesURL = ( assetsURLPrefix: string, @@ -42,23 +43,23 @@ const resolveSourcifySource = (source: SourcifySource) => { }; export const sourcifyMetadata = ( - checksummedAddress: string, - networkId: number, + address: ChecksummedAddress, + chainId: number, source: SourcifySource ) => `${resolveSourcifySource( source - )}/contracts/full_match/${networkId}/${checksummedAddress}/metadata.json`; + )}/contracts/full_match/${chainId}/${address}/metadata.json`; export const sourcifySourceFile = ( - checksummedAddress: string, - networkId: number, + address: ChecksummedAddress, + chainId: number, filepath: string, source: SourcifySource ) => `${resolveSourcifySource( source - )}/contracts/full_match/${networkId}/${checksummedAddress}/sources/${filepath}`; + )}/contracts/full_match/${chainId}/${address}/sources/${filepath}`; export const openInRemixURL = (checksummedAddress: string, networkId: number) => `https://remix.ethereum.org/#call=source-verification//fetchAndSave//${checksummedAddress}//${networkId}`; diff --git a/src/useSourcify.ts b/src/useSourcify.ts index 0d6ea59..2b2f9c4 100644 --- a/src/useSourcify.ts +++ b/src/useSourcify.ts @@ -1,6 +1,6 @@ import { useState, useEffect, useMemo } from "react"; import { Interface } from "@ethersproject/abi"; -import { TransactionData } from "./types"; +import { ChecksummedAddress, TransactionData } from "./types"; import { sourcifyMetadata, SourcifySource, sourcifySourceFile } from "./url"; export type UserMethod = { @@ -65,24 +65,19 @@ export type Metadata = { }; }; -export const fetchSourcifyMetadata = async ( - checksummedAddress: string, +const fetchSourcifyMetadata = async ( + address: ChecksummedAddress, chainId: number, source: SourcifySource, abortController: AbortController ): Promise => { try { - const contractMetadataURL = sourcifyMetadata( - checksummedAddress, - chainId, - source - ); - const result = await fetch(contractMetadataURL, { + const metadataURL = sourcifyMetadata(address, chainId, source); + const result = await fetch(metadataURL, { signal: abortController.signal, }); if (result.ok) { - const _metadata = await result.json(); - return _metadata; + return await result.json(); } return null; @@ -93,14 +88,14 @@ export const fetchSourcifyMetadata = async ( }; export const useSourcify = ( - checksummedAddress: string | undefined, + address: ChecksummedAddress | undefined, chainId: number | undefined, source: SourcifySource ): Metadata | null | undefined => { const [rawMetadata, setRawMetadata] = useState(); useEffect(() => { - if (!checksummedAddress || chainId === undefined) { + if (!address || chainId === undefined) { return; } setRawMetadata(undefined); @@ -108,7 +103,7 @@ export const useSourcify = ( const abortController = new AbortController(); const fetchMetadata = async () => { const _metadata = await fetchSourcifyMetadata( - checksummedAddress, + address, chainId, source, abortController @@ -120,47 +115,48 @@ export const useSourcify = ( return () => { abortController.abort(); }; - }, [checksummedAddress, chainId, source]); + }, [address, chainId, source]); return rawMetadata; }; export const useMultipleMetadata = ( baseMetadatas: Record, - checksummedAddress: (string | undefined)[], + addresses: (ChecksummedAddress | undefined)[], chainId: number | undefined, source: SourcifySource -): Record => { +): Record => { const [rawMetadata, setRawMetadata] = useState< Record >({}); useEffect(() => { - if (!checksummedAddress || chainId === undefined) { + if (!addresses || chainId === undefined) { return; } setRawMetadata({}); const abortController = new AbortController(); - const fetchMetadata = async (addresses: string[]) => { + const fetchMetadata = async (dedupedAddresses: string[]) => { const promises: Promise[] = []; - for (const addr of addresses) { + for (const address of dedupedAddresses) { promises.push( - fetchSourcifyMetadata(addr, chainId, source, abortController) + fetchSourcifyMetadata(address, chainId, source, abortController) ); } const results = await Promise.all(promises); const metadatas: Record = { ...baseMetadatas }; for (let i = 0; i < results.length; i++) { - metadatas[addresses[i]] = results[i]; + metadatas[dedupedAddresses[i]] = results[i]; } setRawMetadata(metadatas); }; const deduped = new Set( - checksummedAddress.filter( - (a): a is string => a !== undefined && baseMetadatas[a] === undefined + addresses.filter( + (a): a is ChecksummedAddress => + a !== undefined && baseMetadatas[a] === undefined ) ); fetchMetadata(Array.from(deduped)); @@ -168,7 +164,7 @@ export const useMultipleMetadata = ( return () => { abortController.abort(); }; - }, [baseMetadatas, checksummedAddress, chainId, source]); + }, [baseMetadatas, addresses, chainId, source]); return rawMetadata; };