From f15c75bb8e76eb22e760aa6e8da99de505ca9d15 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Sat, 24 Jul 2021 20:33:45 -0300 Subject: [PATCH] Add support for external files --- src/AddressTransactions.tsx | 4 +- src/address/Contract.tsx | 107 ++++++++++-------------------------- src/address/Contracts.tsx | 98 +++++++++++++++++++++++++++++++++ src/url.ts | 7 +++ 4 files changed, 137 insertions(+), 79 deletions(-) create mode 100644 src/address/Contracts.tsx diff --git a/src/AddressTransactions.tsx b/src/AddressTransactions.tsx index ae60c21..1d124de 100644 --- a/src/AddressTransactions.tsx +++ b/src/AddressTransactions.tsx @@ -16,7 +16,7 @@ import Copy from "./components/Copy"; import ContentFrame from "./ContentFrame"; import TabGroup from "./components/TabGroup"; import Tab from "./components/Tab"; -import Contract from "./address/Contract"; +import Contracts from "./address/Contracts"; import UndefinedPageControl from "./search/UndefinedPageControl"; import ResultHeader from "./search/ResultHeader"; import PendingResults from "./search/PendingResults"; @@ -273,7 +273,7 @@ const AddressTransactions: React.FC = () => { - + diff --git a/src/address/Contract.tsx b/src/address/Contract.tsx index 357e606..317348d 100644 --- a/src/address/Contract.tsx +++ b/src/address/Contract.tsx @@ -1,100 +1,53 @@ -import React, { useState, useEffect, useContext } from "react"; -import { ethers } from "ethers"; -import ContentFrame from "../ContentFrame"; +import React, { useState } from "react"; import Highlight from "react-highlight"; -import InfoRow from "../components/InfoRow"; -import { sourcifyMetadata } from "../url"; -import { RuntimeContext } from "../useRuntime"; import "highlight.js/styles/stackoverflow-light.css"; import hljs from "highlight.js"; import hljsDefineSolidity from "highlightjs-solidity"; +import { useEffect } from "react"; +import { sourcifySourceFile } from "../url"; hljsDefineSolidity(hljs); hljs.initHighlightingOnLoad(); type ContractProps = { checksummedAddress: string; + networkId: number; + filename: string; + source: any; }; -const Contract: React.FC = ({ checksummedAddress }) => { - const { provider } = useContext(RuntimeContext); - const [rawMetadata, setRawMetadata] = useState(); +const Contract: React.FC = ({ + checksummedAddress, + networkId, + filename, + source, +}) => { + const [content, setContent] = useState(source.content); useEffect(() => { - if (!checksummedAddress) { + if (source.content) { return; } - const fetchMetadata = async () => { - try { - const result = await fetch( - sourcifyMetadata(checksummedAddress, provider!.network.chainId) - ); - if (result.ok) { - const json = await result.json(); - console.log(json); - setRawMetadata(json); - setSelected(Object.keys(json.sources)[0]); - } else { - setRawMetadata(null); - } - } catch (err) { - console.error(err); - setRawMetadata(null); + 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); } }; - fetchMetadata(); - }, [provider, checksummedAddress]); - - const [selected, setSelected] = useState(); - - const optimizer = rawMetadata?.settings?.optimizer; + readContent(); + }, [checksummedAddress, networkId, filename, source.content]); return ( - - {rawMetadata && ( - <> - - {rawMetadata.compiler?.version} - - - {optimizer?.enabled ? ( - - Yes with{" "} - - {ethers.utils.commify(optimizer?.runs)} - {" "} - runs - - ) : ( - No - )} - - - )} -
- {rawMetadata === null && ( - Couldn't find contract metadata in Sourcify repository. - )} - {rawMetadata !== undefined && rawMetadata !== null && ( -
- {Object.entries(rawMetadata.sources).map(([k]) => ( - - ))} - {selected && ( - - {rawMetadata.sources[selected].content} - - )} -
- )} -
-
+ + {content} + ); }; diff --git a/src/address/Contracts.tsx b/src/address/Contracts.tsx new file mode 100644 index 0000000..1fcc81c --- /dev/null +++ b/src/address/Contracts.tsx @@ -0,0 +1,98 @@ +import React, { useState, useEffect, useContext } from "react"; +import { ethers } from "ethers"; +import ContentFrame from "../ContentFrame"; +import InfoRow from "../components/InfoRow"; +import Contract from "./Contract"; +import { sourcifyMetadata } from "../url"; +import { RuntimeContext } from "../useRuntime"; + +type ContractsProps = { + checksummedAddress: string; +}; + +const Contracts: React.FC = ({ checksummedAddress }) => { + const { provider } = useContext(RuntimeContext); + const [rawMetadata, setRawMetadata] = useState(); + useEffect(() => { + if (!checksummedAddress) { + return; + } + + const fetchMetadata = async () => { + try { + const result = await fetch( + sourcifyMetadata(checksummedAddress, provider!.network.chainId) + ); + if (result.ok) { + const json = await result.json(); + console.log(json); + setRawMetadata(json); + setSelected(Object.keys(json.sources)[0]); + } else { + setRawMetadata(null); + } + } catch (err) { + console.error(err); + setRawMetadata(null); + } + }; + fetchMetadata(); + }, [provider, checksummedAddress]); + + const [selected, setSelected] = useState(); + + const optimizer = rawMetadata?.settings?.optimizer; + + return ( + + {rawMetadata && ( + <> + + {rawMetadata.compiler?.version} + + + {optimizer?.enabled ? ( + + Yes with{" "} + + {ethers.utils.commify(optimizer?.runs)} + {" "} + runs + + ) : ( + No + )} + + + )} +
+ {rawMetadata === null && ( + Couldn't find contract metadata in Sourcify repository. + )} + {rawMetadata !== undefined && rawMetadata !== null && ( +
+ {Object.entries(rawMetadata.sources).map(([k]) => ( + + ))} + {selected && ( + + )} +
+ )} +
+
+ ); +}; + +export default React.memo(Contracts); diff --git a/src/url.ts b/src/url.ts index 0d3c9e6..1b4da9c 100644 --- a/src/url.ts +++ b/src/url.ts @@ -19,3 +19,10 @@ export const sourcifyMetadata = ( networkId: number ) => `http://localhost:7000/sourcify/contracts/full_match/${networkId}/${checksummedAddress}/metadata.json`; + +export const sourcifySourceFile = ( + checksummedAddress: string, + networkId: number, + filepath: string +) => + `http://localhost:7000/sourcify/contracts/full_match/${networkId}/${checksummedAddress}/sources/${filepath}`;