Create a new type for checksummed addresses; small renames

This commit is contained in:
Willian Mitsuda 2021-11-10 04:46:43 -03:00
parent cab432aa13
commit 8d259dacc8
3 changed files with 31 additions and 31 deletions

View File

@ -67,6 +67,9 @@ export type ConfirmedTransactionData = {
// The VOID... // The VOID...
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
// TODO: replace all occurrences of plain string
export type ChecksummedAddress = string;
export enum AddressContext { export enum AddressContext {
FROM, FROM,
TO, TO,

View File

@ -1,4 +1,5 @@
import { BlockTag } from "@ethersproject/abstract-provider"; import { BlockTag } from "@ethersproject/abstract-provider";
import { ChecksummedAddress } from "./types";
export const fourBytesURL = ( export const fourBytesURL = (
assetsURLPrefix: string, assetsURLPrefix: string,
@ -42,23 +43,23 @@ const resolveSourcifySource = (source: SourcifySource) => {
}; };
export const sourcifyMetadata = ( export const sourcifyMetadata = (
checksummedAddress: string, address: ChecksummedAddress,
networkId: number, chainId: number,
source: SourcifySource source: SourcifySource
) => ) =>
`${resolveSourcifySource( `${resolveSourcifySource(
source source
)}/contracts/full_match/${networkId}/${checksummedAddress}/metadata.json`; )}/contracts/full_match/${chainId}/${address}/metadata.json`;
export const sourcifySourceFile = ( export const sourcifySourceFile = (
checksummedAddress: string, address: ChecksummedAddress,
networkId: number, chainId: number,
filepath: string, filepath: string,
source: SourcifySource source: SourcifySource
) => ) =>
`${resolveSourcifySource( `${resolveSourcifySource(
source source
)}/contracts/full_match/${networkId}/${checksummedAddress}/sources/${filepath}`; )}/contracts/full_match/${chainId}/${address}/sources/${filepath}`;
export const openInRemixURL = (checksummedAddress: string, networkId: number) => export const openInRemixURL = (checksummedAddress: string, networkId: number) =>
`https://remix.ethereum.org/#call=source-verification//fetchAndSave//${checksummedAddress}//${networkId}`; `https://remix.ethereum.org/#call=source-verification//fetchAndSave//${checksummedAddress}//${networkId}`;

View File

@ -1,6 +1,6 @@
import { useState, useEffect, useMemo } from "react"; import { useState, useEffect, useMemo } from "react";
import { Interface } from "@ethersproject/abi"; import { Interface } from "@ethersproject/abi";
import { TransactionData } from "./types"; import { ChecksummedAddress, TransactionData } from "./types";
import { sourcifyMetadata, SourcifySource, sourcifySourceFile } from "./url"; import { sourcifyMetadata, SourcifySource, sourcifySourceFile } from "./url";
export type UserMethod = { export type UserMethod = {
@ -65,24 +65,19 @@ export type Metadata = {
}; };
}; };
export const fetchSourcifyMetadata = async ( const fetchSourcifyMetadata = async (
checksummedAddress: string, address: ChecksummedAddress,
chainId: number, chainId: number,
source: SourcifySource, source: SourcifySource,
abortController: AbortController abortController: AbortController
): Promise<Metadata | null> => { ): Promise<Metadata | null> => {
try { try {
const contractMetadataURL = sourcifyMetadata( const metadataURL = sourcifyMetadata(address, chainId, source);
checksummedAddress, const result = await fetch(metadataURL, {
chainId,
source
);
const result = await fetch(contractMetadataURL, {
signal: abortController.signal, signal: abortController.signal,
}); });
if (result.ok) { if (result.ok) {
const _metadata = await result.json(); return await result.json();
return _metadata;
} }
return null; return null;
@ -93,14 +88,14 @@ export const fetchSourcifyMetadata = async (
}; };
export const useSourcify = ( export const useSourcify = (
checksummedAddress: string | undefined, address: ChecksummedAddress | undefined,
chainId: number | undefined, chainId: number | undefined,
source: SourcifySource source: SourcifySource
): Metadata | null | undefined => { ): Metadata | null | undefined => {
const [rawMetadata, setRawMetadata] = useState<Metadata | null | undefined>(); const [rawMetadata, setRawMetadata] = useState<Metadata | null | undefined>();
useEffect(() => { useEffect(() => {
if (!checksummedAddress || chainId === undefined) { if (!address || chainId === undefined) {
return; return;
} }
setRawMetadata(undefined); setRawMetadata(undefined);
@ -108,7 +103,7 @@ export const useSourcify = (
const abortController = new AbortController(); const abortController = new AbortController();
const fetchMetadata = async () => { const fetchMetadata = async () => {
const _metadata = await fetchSourcifyMetadata( const _metadata = await fetchSourcifyMetadata(
checksummedAddress, address,
chainId, chainId,
source, source,
abortController abortController
@ -120,47 +115,48 @@ export const useSourcify = (
return () => { return () => {
abortController.abort(); abortController.abort();
}; };
}, [checksummedAddress, chainId, source]); }, [address, chainId, source]);
return rawMetadata; return rawMetadata;
}; };
export const useMultipleMetadata = ( export const useMultipleMetadata = (
baseMetadatas: Record<string, Metadata | null>, baseMetadatas: Record<string, Metadata | null>,
checksummedAddress: (string | undefined)[], addresses: (ChecksummedAddress | undefined)[],
chainId: number | undefined, chainId: number | undefined,
source: SourcifySource source: SourcifySource
): Record<string, Metadata | null | undefined> => { ): Record<ChecksummedAddress, Metadata | null | undefined> => {
const [rawMetadata, setRawMetadata] = useState< const [rawMetadata, setRawMetadata] = useState<
Record<string, Metadata | null | undefined> Record<string, Metadata | null | undefined>
>({}); >({});
useEffect(() => { useEffect(() => {
if (!checksummedAddress || chainId === undefined) { if (!addresses || chainId === undefined) {
return; return;
} }
setRawMetadata({}); setRawMetadata({});
const abortController = new AbortController(); const abortController = new AbortController();
const fetchMetadata = async (addresses: string[]) => { const fetchMetadata = async (dedupedAddresses: string[]) => {
const promises: Promise<Metadata | null>[] = []; const promises: Promise<Metadata | null>[] = [];
for (const addr of addresses) { for (const address of dedupedAddresses) {
promises.push( promises.push(
fetchSourcifyMetadata(addr, chainId, source, abortController) fetchSourcifyMetadata(address, chainId, source, abortController)
); );
} }
const results = await Promise.all(promises); const results = await Promise.all(promises);
const metadatas: Record<string, Metadata | null> = { ...baseMetadatas }; const metadatas: Record<string, Metadata | null> = { ...baseMetadatas };
for (let i = 0; i < results.length; i++) { for (let i = 0; i < results.length; i++) {
metadatas[addresses[i]] = results[i]; metadatas[dedupedAddresses[i]] = results[i];
} }
setRawMetadata(metadatas); setRawMetadata(metadatas);
}; };
const deduped = new Set( const deduped = new Set(
checksummedAddress.filter( addresses.filter(
(a): a is string => a !== undefined && baseMetadatas[a] === undefined (a): a is ChecksummedAddress =>
a !== undefined && baseMetadatas[a] === undefined
) )
); );
fetchMetadata(Array.from(deduped)); fetchMetadata(Array.from(deduped));
@ -168,7 +164,7 @@ export const useMultipleMetadata = (
return () => { return () => {
abortController.abort(); abortController.abort();
}; };
}, [baseMetadatas, checksummedAddress, chainId, source]); }, [baseMetadatas, addresses, chainId, source]);
return rawMetadata; return rawMetadata;
}; };