otterscan/src/hooks.ts

34 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-12-04 19:33:07 +00:00
import { useMemo } from "react";
2021-12-02 19:19:03 +00:00
import { JsonRpcProvider } from "@ethersproject/providers";
import { ChecksummedAddress } from "./types";
2021-12-04 19:33:07 +00:00
import { Metadata, useMultipleMetadata } from "./sourcify/useSourcify";
2021-12-02 19:19:03 +00:00
import { useAppConfigContext } from "./useAppConfig";
import { useAddressesWithCode } from "./useErigonHooks";
2021-12-04 19:33:07 +00:00
export const useDedupedAddresses = (
addresses: ChecksummedAddress[]
): ChecksummedAddress[] => {
return useMemo(() => {
const deduped = new Set(addresses);
return [...deduped];
}, [addresses]);
};
2021-12-02 19:19:03 +00:00
export const useContractsMetadata = (
addresses: ChecksummedAddress[],
provider: JsonRpcProvider | undefined,
baseMetadatas?: Record<string, Metadata | null>
) => {
const deduped = useDedupedAddresses(addresses);
const contracts = useAddressesWithCode(provider, deduped);
const { sourcifySource } = useAppConfigContext();
const metadatas = useMultipleMetadata(
baseMetadatas,
contracts,
provider?.network.chainId,
sourcifySource
);
return metadatas;
};