Use SWR for Sourcify metadata fetching
This commit is contained in:
parent
50ac6f7a3e
commit
0b210fd446
|
@ -11,7 +11,10 @@ import { SelectionContext, useSelection } from "./useSelection";
|
||||||
import { SelectedTransactionContext } from "./useSelectedTransaction";
|
import { SelectedTransactionContext } from "./useSelectedTransaction";
|
||||||
import { BlockNumberContext } from "./useBlockTagContext";
|
import { BlockNumberContext } from "./useBlockTagContext";
|
||||||
import { useAppConfigContext } from "./useAppConfig";
|
import { useAppConfigContext } from "./useAppConfig";
|
||||||
import { useSourcify, useTransactionDescription } from "./sourcify/useSourcify";
|
import {
|
||||||
|
useSourcifyMetadata,
|
||||||
|
useTransactionDescription,
|
||||||
|
} from "./sourcify/useSourcify";
|
||||||
|
|
||||||
const Details = React.lazy(() => import("./transaction/Details"));
|
const Details = React.lazy(() => import("./transaction/Details"));
|
||||||
const Logs = React.lazy(() => import("./transaction/Logs"));
|
const Logs = React.lazy(() => import("./transaction/Logs"));
|
||||||
|
@ -44,7 +47,7 @@ const TransactionPageContent: React.FC<TransactionPageContentProps> = ({
|
||||||
const selectionCtx = useSelection();
|
const selectionCtx = useSelection();
|
||||||
|
|
||||||
const { sourcifySource } = useAppConfigContext();
|
const { sourcifySource } = useAppConfigContext();
|
||||||
const metadata = useSourcify(
|
const metadata = useSourcifyMetadata(
|
||||||
txData?.to,
|
txData?.to,
|
||||||
provider?.network.chainId,
|
provider?.network.chainId,
|
||||||
sourcifySource
|
sourcifySource
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { useState, useEffect, useMemo } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import { Interface } from "@ethersproject/abi";
|
import { Interface } from "@ethersproject/abi";
|
||||||
import { ErrorDescription } from "@ethersproject/abi/lib/interface";
|
import { ErrorDescription } from "@ethersproject/abi/lib/interface";
|
||||||
|
import useSWRImmutable from "swr/immutable";
|
||||||
import { ChecksummedAddress, TransactionData } from "../types";
|
import { ChecksummedAddress, TransactionData } from "../types";
|
||||||
import { sourcifyMetadata, SourcifySource, sourcifySourceFile } from "../url";
|
import { sourcifyMetadata, SourcifySource, sourcifySourceFile } from "../url";
|
||||||
|
|
||||||
|
@ -102,38 +103,22 @@ const fetchSourcifyMetadata = async (
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: replace every occurrence with the multiple version one
|
export const useSourcifyMetadata = (
|
||||||
export const useSourcify = (
|
|
||||||
address: ChecksummedAddress | 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 metadataURL = () =>
|
||||||
|
address === undefined || chainId === undefined
|
||||||
useEffect(() => {
|
? null
|
||||||
if (!address || chainId === undefined) {
|
: sourcifyMetadata(address, chainId, source);
|
||||||
return;
|
const { data, error } = useSWRImmutable<Metadata>(metadataURL, (url) =>
|
||||||
}
|
fetch(url).then((res) => res.json())
|
||||||
setRawMetadata(undefined);
|
);
|
||||||
|
if (error) {
|
||||||
const abortController = new AbortController();
|
return null;
|
||||||
const fetchMetadata = async () => {
|
}
|
||||||
const _metadata = await fetchSourcifyMetadata(
|
return data;
|
||||||
address,
|
|
||||||
chainId,
|
|
||||||
source,
|
|
||||||
abortController
|
|
||||||
);
|
|
||||||
setRawMetadata(_metadata);
|
|
||||||
};
|
|
||||||
fetchMetadata();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
abortController.abort();
|
|
||||||
};
|
|
||||||
}, [address, chainId, source]);
|
|
||||||
|
|
||||||
return rawMetadata;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useMultipleMetadata = (
|
export const useMultipleMetadata = (
|
||||||
|
|
|
@ -53,6 +53,10 @@ const resolveSourcifySource = (source: SourcifySource) => {
|
||||||
throw new Error(`Unknown Sourcify integration source code: ${source}`);
|
throw new Error(`Unknown Sourcify integration source code: ${source}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a complete Sourcify metadata.json URL given the contract address
|
||||||
|
* and chain.
|
||||||
|
*/
|
||||||
export const sourcifyMetadata = (
|
export const sourcifyMetadata = (
|
||||||
address: ChecksummedAddress,
|
address: ChecksummedAddress,
|
||||||
chainId: number,
|
chainId: number,
|
||||||
|
|
Loading…
Reference in New Issue