otterscan/src/useSourcify.ts

105 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-09-06 00:08:06 +00:00
import { useState, useEffect } from "react";
2021-09-06 05:09:12 +00:00
import { sourcifyMetadata, sourcifySourceFile } from "./url";
2021-09-06 00:08:06 +00:00
export type Metadata = {
version: string;
language: string;
compiler: {
version: string;
keccak256?: string | undefined;
};
sources: {
[filename: string]: {
keccak256: string;
content?: string | undefined;
urls?: string[];
license?: string;
};
};
settings: {
remappings: string[];
optimizer?: {
enabled: boolean;
runs: number;
};
compilationTarget: {
[filename: string]: string;
};
libraries: {
[filename: string]: string;
};
};
output: {
abi: any[];
userdocs: any[];
devdoc: any[];
};
};
export const useSourcify = (
checksummedAddress: string | undefined,
chainId: number | undefined
) => {
const [rawMetadata, setRawMetadata] = useState<Metadata | null | undefined>();
useEffect(() => {
if (!checksummedAddress || chainId === undefined) {
return;
}
const fetchMetadata = async () => {
try {
const contractMetadataURL = sourcifyMetadata(
checksummedAddress,
chainId
);
const result = await fetch(contractMetadataURL);
if (result.ok) {
const _metadata = await result.json();
setRawMetadata(_metadata);
} else {
setRawMetadata(null);
}
} catch (err) {
console.error(err);
setRawMetadata(null);
}
};
fetchMetadata();
}, [checksummedAddress, chainId]);
return rawMetadata;
};
2021-09-06 05:09:12 +00:00
export const useContract = (
checksummedAddress: string,
networkId: number,
filename: string,
source: any
) => {
const [content, setContent] = useState<string>(source.content);
useEffect(() => {
if (source.content) {
return;
}
const readContent = async () => {
const normalizedFilename = filename.replaceAll("@", "_");
const url = sourcifySourceFile(
checksummedAddress,
networkId,
normalizedFilename
);
const res = await fetch(url);
if (res.ok) {
const _content = await res.text();
setContent(_content);
}
};
readContent();
}, [checksummedAddress, networkId, filename, source.content]);
return content;
};