Add more compilation info

This commit is contained in:
Willian Mitsuda 2021-07-23 15:36:31 -03:00
parent 38f76e9e2b
commit 851a791375
1 changed files with 34 additions and 12 deletions

View File

@ -1,6 +1,8 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { ethers } from "ethers";
import ContentFrame from "../ContentFrame"; import ContentFrame from "../ContentFrame";
import Highlight from "react-highlight"; import Highlight from "react-highlight";
import InfoRow from "../components/InfoRow";
import "highlight.js/styles/stackoverflow-light.css"; import "highlight.js/styles/stackoverflow-light.css";
import hljs from "highlight.js"; import hljs from "highlight.js";
@ -13,9 +15,7 @@ type ContractProps = {
}; };
const Contract: React.FC<ContractProps> = ({ checksummedAddress }) => { const Contract: React.FC<ContractProps> = ({ checksummedAddress }) => {
const [sources, setSources] = useState< const [rawMetadata, setRawMetadata] = useState<any>();
{ [fileName: string]: any } | undefined | null
>(undefined);
useEffect(() => { useEffect(() => {
if (!checksummedAddress) { if (!checksummedAddress) {
return; return;
@ -29,14 +29,14 @@ const Contract: React.FC<ContractProps> = ({ checksummedAddress }) => {
if (result.ok) { if (result.ok) {
const json = await result.json(); const json = await result.json();
console.log(json); console.log(json);
setSources(json.sources); setRawMetadata(json);
setSelected(Object.keys(json.sources)[0]); setSelected(Object.keys(json.sources)[0]);
} else { } else {
setSources(null); setRawMetadata(null);
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
setSources(null); setRawMetadata(null);
} }
}; };
fetchMetadata(); fetchMetadata();
@ -44,15 +44,37 @@ const Contract: React.FC<ContractProps> = ({ checksummedAddress }) => {
const [selected, setSelected] = useState<string>(); const [selected, setSelected] = useState<string>();
const optimizer = rawMetadata?.settings?.optimizer;
return ( return (
<ContentFrame tabs> <ContentFrame tabs>
{rawMetadata && (
<>
<InfoRow title="Compiler">
<span>{rawMetadata.compiler?.version}</span>
</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">
{ethers.utils.commify(optimizer?.runs)}
</span>{" "}
runs
</span>
) : (
<span className="font-bold text-red-600">No</span>
)}
</InfoRow>
</>
)}
<div className="py-5"> <div className="py-5">
{sources === null && ( {rawMetadata === null && (
<span>Couldn't find contract metadata in Sourcify repository.</span> <span>Couldn't find contract metadata in Sourcify repository.</span>
)} )}
{sources !== undefined && sources !== null && ( {rawMetadata !== undefined && rawMetadata !== null && (
<> <div>
{Object.entries(sources).map(([k]) => ( {Object.entries(rawMetadata.sources).map(([k]) => (
<button <button
className={`border-b-2 border-transparent rounded-t text-sm px-2 py-1 bg-gray-200 text-gray-500 ${ className={`border-b-2 border-transparent rounded-t text-sm px-2 py-1 bg-gray-200 text-gray-500 ${
selected === k ? "border-orange-300 font-bold" : "" selected === k ? "border-orange-300 font-bold" : ""
@ -63,10 +85,10 @@ const Contract: React.FC<ContractProps> = ({ checksummedAddress }) => {
))} ))}
{selected && ( {selected && (
<Highlight className="w-full h-full border focus:outline-none font-code text-base"> <Highlight className="w-full h-full border focus:outline-none font-code text-base">
{sources[selected].content} {rawMetadata.sources[selected].content}
</Highlight> </Highlight>
)} )}
</> </div>
)} )}
</div> </div>
</ContentFrame> </ContentFrame>