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...
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
// TODO: replace all occurrences of plain string
export type ChecksummedAddress = string;
export enum AddressContext {
FROM,
TO,

View File

@ -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}`;

View File

@ -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<Metadata | null> => {
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<Metadata | null | undefined>();
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<string, Metadata | null>,
checksummedAddress: (string | undefined)[],
addresses: (ChecksummedAddress | undefined)[],
chainId: number | undefined,
source: SourcifySource
): Record<string, Metadata | null | undefined> => {
): Record<ChecksummedAddress, Metadata | null | undefined> => {
const [rawMetadata, setRawMetadata] = useState<
Record<string, Metadata | null | undefined>
>({});
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<Metadata | null>[] = [];
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<string, Metadata | null> = { ...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;
};