Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display Scilla contract source #9

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 97 additions & 77 deletions src/execution/address/Contracts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useContext } from "react";
import React, { useState, useEffect, useContext, useMemo } from "react";
import { commify } from "@ethersproject/units";
import { Menu } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand All @@ -14,6 +14,8 @@ import { openInRemixURL } from "../../url";
import ContractABI from "./contract/ContractABI";
import { useGetCode } from "../../useErigonHooks";
import StandardTextarea from "../../components/StandardTextarea";
import { toUtf8String } from "ethers/lib/utils";
import ScillaContract from "./ScillaContract";

type ContractsProps = {
checksummedAddress: string;
Expand All @@ -23,6 +25,19 @@ type ContractsProps = {
const Contracts: React.FC<ContractsProps> = ({ checksummedAddress, match }) => {
const { provider } = useContext(RuntimeContext);
const code = useGetCode(provider, checksummedAddress);
const scillaCode = useMemo(() => {
try {
if (code) {
let s = toUtf8String(code);
if (s.startsWith("scilla_version")) {
return s;
}
}
} catch (err) {
// Silently ignore on purpose
return undefined;
}
}, [code]);

const [selected, setSelected] = useState<string>();
useEffect(() => {
Expand Down Expand Up @@ -60,85 +75,90 @@ const Contracts: React.FC<ContractsProps> = ({ checksummedAddress, match }) => {
</InfoRow>
</>
)}
<div className="py-5">
JamesHinshelwood marked this conversation as resolved.
Show resolved Hide resolved
{match === undefined && (
<span>Getting data from Sourcify repository...</span>
)}
{match === null && (
<span>
Address is not a contract or couldn't find contract metadata in
Sourcify repository.
</span>
)}
{match !== undefined && match !== null && (
<>
{match.metadata.output.abi && (
<ContractABI abi={match.metadata.output.abi} />
)}
<div>
<Menu>
<div className="flex items-baseline justify-between space-x-2">
<Menu.Button className="flex space-x-2 rounded-t border-l border-r border-t px-2 py-1 text-sm">
<span>{selected}</span>
<span className="self-center">
<FontAwesomeIcon icon={faChevronDown} size="xs" />
</span>
</Menu.Button>
{provider && (
<div className="text-sm">
<ExternalLink
href={openInRemixURL(
checksummedAddress,
provider.network.chainId
)}
>
Open in Remix
</ExternalLink>
</div>
)}
</div>
<div className="relative">
<Menu.Items className="absolute flex flex-col rounded-b border bg-white p-1">
{Object.entries(match.metadata.sources).map(([k]) => (
<Menu.Item key={k}>
<button
className={`flex px-2 py-1 text-sm ${
selected === k
? "bg-gray-200 font-bold text-gray-500"
: "text-gray-400 transition-colors duration-75 hover:text-gray-500"
}`}
onClick={() => setSelected(k)}
>
{k}
</button>
</Menu.Item>
))}
</Menu.Items>
</div>
</Menu>
{selected && (
<>
{match.metadata.sources[selected].content ? (
<Contract
content={match.metadata.sources[selected].content}
/>
) : (
<ContractFromRepo
checksummedAddress={checksummedAddress}
networkId={provider!.network.chainId}
filename={selected}
type={match.type}
/>
)}
</>
{!scillaCode && (
<div className="py-5">
{match === undefined && (
<span>Getting data from Sourcify repository...</span>
)}
{match === null && (
<span>
Address is not a contract or couldn't find contract metadata in
Sourcify repository.
</span>
)}
{match !== undefined && match !== null && (
<>
{match.metadata.output.abi && (
<ContractABI abi={match.metadata.output.abi} />
)}
</div>
</>
)}
</div>
<div>
<Menu>
<div className="flex items-baseline justify-between space-x-2">
<Menu.Button className="flex space-x-2 rounded-t border-l border-r border-t px-2 py-1 text-sm">
<span>{selected}</span>
<span className="self-center">
<FontAwesomeIcon icon={faChevronDown} size="xs" />
</span>
</Menu.Button>
{provider && (
<div className="text-sm">
<ExternalLink
href={openInRemixURL(
checksummedAddress,
provider.network.chainId
)}
>
Open in Remix
</ExternalLink>
</div>
)}
</div>
<div className="relative">
<Menu.Items className="absolute flex flex-col rounded-b border bg-white p-1">
{Object.entries(match.metadata.sources).map(([k]) => (
<Menu.Item key={k}>
<button
className={`flex px-2 py-1 text-sm ${
selected === k
? "bg-gray-200 font-bold text-gray-500"
: "text-gray-400 transition-colors duration-75 hover:text-gray-500"
}`}
onClick={() => setSelected(k)}
>
{k}
</button>
</Menu.Item>
))}
</Menu.Items>
</div>
</Menu>
{selected && (
<>
{match.metadata.sources[selected].content ? (
<Contract
content={match.metadata.sources[selected].content}
/>
) : (
<ContractFromRepo
checksummedAddress={checksummedAddress}
networkId={provider!.network.chainId}
filename={selected}
type={match.type}
/>
)}
</>
)}
</div>
</>
)}
</div>
)}
<div className="py-5">
{code === undefined && <span>Getting contract bytecode...</span>}
{code && (
{scillaCode && (
<ScillaContract content={scillaCode} />
)}
{!scillaCode && code && (
lucac-zilliqa marked this conversation as resolved.
Show resolved Hide resolved
<>
<div className="pb-2">Contract Bytecode</div>
<StandardTextarea value={code} />
Expand Down
19 changes: 19 additions & 0 deletions src/execution/address/ScillaContract.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { SyntaxHighlighter, docco } from "../../highlight-init";

type ContractProps = {
content: any;
};

const ScillaContract: React.FC<ContractProps> = ({ content }) => (
<SyntaxHighlighter
className="h-full w-full border font-code text-base"
language="scilla"
style={docco}
showLineNumbers
>
{content ?? ""}
</SyntaxHighlighter>
);

export default React.memo(ScillaContract);