2021-09-06 06:43:20 +00:00
|
|
|
import React, { useState, useEffect, useContext, Fragment } from "react";
|
|
|
|
|
import { commify } from "@ethersproject/units";
|
2021-09-06 21:32:11 +00:00
|
|
|
import { Switch, Tab } from "@headlessui/react";
|
2021-07-24 23:33:45 +00:00
|
|
|
import ContentFrame from "../ContentFrame";
|
|
|
|
|
import InfoRow from "../components/InfoRow";
|
|
|
|
|
import Contract from "./Contract";
|
|
|
|
|
import { RuntimeContext } from "../useRuntime";
|
2021-09-06 00:13:01 +00:00
|
|
|
import { Metadata } from "../useSourcify";
|
2021-09-06 07:14:49 +00:00
|
|
|
import ExternalLink from "../components/ExternalLink";
|
|
|
|
|
import { openInRemixURL } from "../url";
|
2021-07-25 08:09:30 +00:00
|
|
|
|
2021-07-24 23:33:45 +00:00
|
|
|
type ContractsProps = {
|
|
|
|
|
checksummedAddress: string;
|
2021-09-06 00:13:01 +00:00
|
|
|
rawMetadata: Metadata | null | undefined;
|
2021-09-06 21:32:11 +00:00
|
|
|
useIPFS: boolean;
|
|
|
|
|
setUseIPFS: (useIPFS: boolean) => void;
|
2021-07-24 23:33:45 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-06 00:13:01 +00:00
|
|
|
const Contracts: React.FC<ContractsProps> = ({
|
|
|
|
|
checksummedAddress,
|
|
|
|
|
rawMetadata,
|
2021-09-06 21:32:11 +00:00
|
|
|
useIPFS,
|
|
|
|
|
setUseIPFS,
|
2021-09-06 00:13:01 +00:00
|
|
|
}) => {
|
2021-07-24 23:33:45 +00:00
|
|
|
const { provider } = useContext(RuntimeContext);
|
|
|
|
|
|
|
|
|
|
const [selected, setSelected] = useState<string>();
|
2021-09-06 00:08:06 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (rawMetadata) {
|
|
|
|
|
setSelected(Object.keys(rawMetadata.sources)[0]);
|
|
|
|
|
}
|
|
|
|
|
}, [rawMetadata]);
|
2021-07-24 23:33:45 +00:00
|
|
|
const optimizer = rawMetadata?.settings?.optimizer;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ContentFrame tabs>
|
2021-09-06 21:32:11 +00:00
|
|
|
<InfoRow title="Use IPFS">
|
|
|
|
|
<Switch
|
|
|
|
|
className={`flex items-center h-7 w-12 px-1 rounded-full transition duration-200 ${
|
|
|
|
|
useIPFS ? "bg-blue-500" : "bg-blue-900"
|
|
|
|
|
}`}
|
|
|
|
|
checked={useIPFS}
|
|
|
|
|
onChange={setUseIPFS}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className={`inline-block border rounded-full w-5 h-5 bg-white transform duration-200 ${
|
|
|
|
|
useIPFS ? "" : "translate-x-5"
|
|
|
|
|
}`}
|
|
|
|
|
></span>
|
|
|
|
|
</Switch>
|
|
|
|
|
</InfoRow>
|
2021-07-24 23:33:45 +00:00
|
|
|
{rawMetadata && (
|
|
|
|
|
<>
|
2021-07-25 08:09:30 +00:00
|
|
|
<InfoRow title="Language">
|
|
|
|
|
<span>{rawMetadata.language}</span>
|
|
|
|
|
</InfoRow>
|
2021-07-24 23:33:45 +00:00
|
|
|
<InfoRow title="Compiler">
|
2021-07-25 08:09:30 +00:00
|
|
|
<span>{rawMetadata.compiler.version}</span>
|
2021-07-24 23:33:45 +00:00
|
|
|
</InfoRow>
|
|
|
|
|
<InfoRow title="Optimizer Enabled">
|
|
|
|
|
{optimizer?.enabled ? (
|
|
|
|
|
<span>
|
|
|
|
|
<span className="font-bold text-green-600">Yes</span> with{" "}
|
|
|
|
|
<span className="font-bold text-green-600">
|
2021-09-06 06:43:20 +00:00
|
|
|
{commify(optimizer?.runs)}
|
2021-07-24 23:33:45 +00:00
|
|
|
</span>{" "}
|
|
|
|
|
runs
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="font-bold text-red-600">No</span>
|
|
|
|
|
)}
|
|
|
|
|
</InfoRow>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
<div className="py-5">
|
|
|
|
|
{rawMetadata === null && (
|
|
|
|
|
<span>Couldn't find contract metadata in Sourcify repository.</span>
|
|
|
|
|
)}
|
|
|
|
|
{rawMetadata !== undefined && rawMetadata !== null && (
|
|
|
|
|
<div>
|
2021-09-06 07:14:49 +00:00
|
|
|
{provider && (
|
|
|
|
|
<div className="text-sm mb-3">
|
|
|
|
|
<ExternalLink
|
|
|
|
|
href={openInRemixURL(
|
|
|
|
|
checksummedAddress,
|
|
|
|
|
provider.network.chainId
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
Open in Remix
|
|
|
|
|
</ExternalLink>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2021-09-06 06:43:20 +00:00
|
|
|
<Tab.Group>
|
|
|
|
|
<Tab.List className="flex truncate">
|
|
|
|
|
{Object.entries(rawMetadata.sources).map(([k]) => (
|
|
|
|
|
<Tab key={k} as={Fragment}>
|
|
|
|
|
<button
|
|
|
|
|
className={`border-b-2 border-transparent rounded-t text-sm px-2 py-1 ${
|
|
|
|
|
selected === k
|
|
|
|
|
? "border-orange-300 font-bold bg-gray-200 text-gray-500"
|
|
|
|
|
: "hover:border-orange-200 bg-gray-100 hover:text-gray-500 text-gray-400 transition-transform transition-colors duration-75 transform origin-bottom scale-95 hover:scale-100"
|
|
|
|
|
}`}
|
|
|
|
|
onClick={() => setSelected(k)}
|
|
|
|
|
>
|
|
|
|
|
{k}
|
|
|
|
|
</button>
|
|
|
|
|
</Tab>
|
|
|
|
|
))}
|
|
|
|
|
</Tab.List>
|
|
|
|
|
</Tab.Group>
|
2021-07-24 23:33:45 +00:00
|
|
|
{selected && (
|
|
|
|
|
<Contract
|
|
|
|
|
checksummedAddress={checksummedAddress}
|
|
|
|
|
networkId={provider!.network.chainId}
|
|
|
|
|
filename={selected}
|
|
|
|
|
source={rawMetadata.sources[selected]}
|
2021-09-06 21:32:11 +00:00
|
|
|
useIPFS={useIPFS}
|
2021-07-24 23:33:45 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</ContentFrame>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default React.memo(Contracts);
|