From 99bcaaae0e7f1fdc67db6aa8338b1d450c95f725 Mon Sep 17 00:00:00 2001 From: Mathieu Geukens Date: Sun, 2 Jun 2024 10:07:19 +0200 Subject: [PATCH] mutliple contract artifacts --- src/components/App.tsx | 6 +-- src/components/ArtifactsInfo.tsx | 70 +++++++++++++++++++++----------- src/components/Main.tsx | 19 +++++---- 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 5bd423e..7237889 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -14,7 +14,7 @@ import ContractFunctions from './ContractFunctions' function App() { const [network, setNetwork] = useState('chipnet') const [wallets, setWallets] = useState([]) - const [artifact, setArtifact] = useState(undefined); + const [artifacts, setArtifacts] = useState(undefined); const [contract, setContract] = useState(undefined) const [utxos, setUtxos] = useState(undefined) const [balance, setBalance] = useState(undefined) @@ -36,16 +36,14 @@ function App() { style={{ display: "inline-flex", marginLeft: "calc(100vw - 1000px)" }} > -
+
- - diff --git a/src/components/ArtifactsInfo.tsx b/src/components/ArtifactsInfo.tsx index 2ddb331..87c60af 100644 --- a/src/components/ArtifactsInfo.tsx +++ b/src/components/ArtifactsInfo.tsx @@ -2,13 +2,13 @@ import React from 'react' import { Artifact } from 'cashscript' interface Props { - artifact?: Artifact + artifacts?: Artifact[] + setArtifacts: (artifacts: Artifact[] | undefined) => void } -const ContractInfo: React.FC = ({ artifact }) => { +const ContractInfo: React.FC = ({ artifacts, setArtifacts }) => { - const downloadArtifact = () => { - if(!artifact) return + const downloadArtifact = (artifact: Artifact) => { const element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(artifact, null, 2))); element.setAttribute('download', `${artifact.contractName}.json`); @@ -18,6 +18,12 @@ const ContractInfo: React.FC = ({ artifact }) => { document.body.removeChild(element); } + const removeArtifact = (artifactToRemove: Artifact) => { + const artifactToRemoveName = artifactToRemove.contractName; + const newArtifacts = artifacts?.filter(artifact => artifact.contractName !== artifactToRemoveName) + setArtifacts(newArtifacts) + } + return (
= ({ artifact }) => { background: '#fffffe', padding: '8px 16px', color: '#000' - }}>{ artifact? + }}>{ artifacts?.length? (
-

Artifact {artifact.contractName}

- Last Updated -

{artifact.updatedAt}

- Artifact Bytecode -
- - show full Bytecode - - {artifact.bytecode} -
- Compiler Version -

{artifact.compiler.version}

- Download Artifact -

- download JSON file - -

-
) : +

Contract Artifacts

+ {artifacts.map(artifact => ( +
+ + {artifact.contractName} +
+ removeArtifact(artifact)} + style={{padding: "0px 6px", width: "fit-content", cursor:"pointer"}} + alt='trashIcon' + /> +
+
+ +
+ Last Updated +

{artifact.updatedAt}

+ Artifact Bytecode +
+ + show full Bytecode + + {artifact.bytecode} +
+ Compiler Version +

{artifact.compiler.version}

+ Download Artifact +

downloadArtifact(artifact)}> + download JSON file + +

+
+
+ ))} +
) :
Compile a CashScript contract to get started!
} ) diff --git a/src/components/Main.tsx b/src/components/Main.tsx index ce1a7c7..e824132 100644 --- a/src/components/Main.tsx +++ b/src/components/Main.tsx @@ -6,11 +6,11 @@ import Editor from './Editor'; import ArtifactsInfo from './ArtifactsInfo'; interface Props { - artifact: Artifact | undefined - setArtifact: (artifact: Artifact | undefined) => void + artifacts: Artifact[] | undefined + setArtifacts: (artifacts: Artifact[] | undefined) => void } -const Main: React.FC = ({artifact, setArtifact}) => { +const Main: React.FC = ({artifacts, setArtifacts}) => { const [code, setCode] = useState( `pragma cashscript >= 0.8.0; @@ -29,7 +29,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { `); const [needRecompile, setNeedRecompile] = useState(true); - + /* useEffect(() => { const codeLocalStorage = localStorage.getItem("code"); // If code exits in local storage, set it as new code @@ -51,16 +51,19 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { const changedCashScriptCode = previousCode!= code; setNeedRecompile(changedCashScriptCode); },[code, needRecompile, artifact]) + */ function compile() { try { localStorage.setItem("code", code); - const artifact = compileString(code); - setArtifact(artifact); + const newArtifact = compileString(code); + const nameNewArtifact = newArtifact.contractName + const newArtifacts = artifacts?.filter(artifact => artifact.contractName !== nameNewArtifact) + setArtifacts([newArtifact, ...newArtifacts ?? []]); } catch (e: any) { alert(e.message); console.error(e.message); - setArtifact(undefined); + setArtifacts(undefined); } } @@ -70,7 +73,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { height: 'calc(100vh - 140px)' }}> - + ) }