Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
load artifact code
  • Loading branch information
mr-zwets committed Jun 2, 2024
commit 701525065d94a0ecddfa995d3f4909961c84efb7
18 changes: 17 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ function App() {
const [contract, setContract] = useState<Contract | undefined>(undefined)
const [utxos, setUtxos] = useState<Utxo[] | undefined>(undefined)
const [balance, setBalance] = useState<bigint | undefined>(undefined)
const [code, setCode] = useState<string>(
`pragma cashscript >= 0.8.0;

contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
// Require recipient's signature to match
function transfer(sig recipientSig) {
require(checkSig(recipientSig, recipient));
}

// Require timeout time to be reached and sender's signature to match
function timeout(sig senderSig) {
require(checkSig(senderSig, sender));
require(tx.time >= timeout);
}
}
`);

async function updateUtxosContract () {
if (!contract) return
Expand All @@ -37,7 +53,7 @@ function App() {
style={{ display: "inline-flex", marginLeft: "calc(100vw - 1100px)" }}
>
<Tab eventKey="editor" title="Editor">
<Main artifacts={artifacts} setArtifacts={setArtifacts} />
<Main code={code} setCode={setCode} artifacts={artifacts} setArtifacts={setArtifacts} />
</Tab>
<Tab eventKey="newcontract" title="New Contract">
<NewContract artifacts={artifacts} network={network} setNetwork={setNetwork} utxos={utxos} balance={balance} contract={contract} setContract={setContract} updateUtxosContract={updateUtxosContract} />
Expand Down
13 changes: 12 additions & 1 deletion src/components/ArtifactsInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import { Artifact } from 'cashscript'
import { Button } from 'react-bootstrap'

interface Props {
setCode: (code: string) => void
artifacts?: Artifact[]
setArtifacts: (artifacts: Artifact[] | undefined) => void
}

const ContractInfo: React.FC<Props> = ({ artifacts, setArtifacts }) => {
const ContractInfo: React.FC<Props> = ({ setCode, artifacts, setArtifacts }) => {

const downloadArtifact = (artifact: Artifact) => {
const element = document.createElement('a');
Expand All @@ -24,6 +26,11 @@ const ContractInfo: React.FC<Props> = ({ artifacts, setArtifacts }) => {
setArtifacts(newArtifacts)
}

const loadArtifact = (artifact: Artifact) => {
console.log(artifact.source)
setCode(artifact.source)
}

return (
<div style={{
flex: 2,
Expand Down Expand Up @@ -71,6 +78,10 @@ const ContractInfo: React.FC<Props> = ({ artifacts, setArtifacts }) => {
download JSON file
<img src='./downloadIcon.svg' style={{marginLeft:"5px", verticalAlign: "text-bottom", cursor:"pointer"}}/>
</p>
<strong>Load Contract to Editor</strong>
<Button variant="secondary" size="sm" style={{display:"block"}} onClick={() => loadArtifact(artifact)}>
Load Artifact
</Button>
</div>
</details>
))}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Editor: React.FC<Props> = ({ code, setCode, compile, needRecompile }) => {
language="sol"
value={code}
theme="light"
onChange={(ev: any, code?: string) => code && setCode(code)}
onChange={(ev: any, code?: string) => setCode(code?? "") }
editorDidMount={handleEditorDidMount}
/>
<Button
Expand Down
30 changes: 10 additions & 20 deletions src/components/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import React, { useState, useEffect } from 'react';
import { Artifact, Utxo } from 'cashscript';
import { Artifact } from 'cashscript';
import { compileString } from 'cashc';
import { RowFlex } from './shared';
import Editor from './Editor';
import ArtifactsInfo from './ArtifactsInfo';

interface Props {
code: string
setCode: (code: string) => void
artifacts: Artifact[] | undefined
setArtifacts: (artifacts: Artifact[] | undefined) => void
}

const Main: React.FC<Props> = ({artifacts, setArtifacts}) => {
const [code, setCode] = useState<string>(
`pragma cashscript >= 0.8.0;

contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
// Require recipient's signature to match
function transfer(sig recipientSig) {
require(checkSig(recipientSig, recipient));
}

// Require timeout time to be reached and sender's signature to match
function timeout(sig senderSig) {
require(checkSig(senderSig, sender));
require(tx.time >= timeout);
}
}
`);
const Main: React.FC<Props> = ({code, setCode, artifacts, setArtifacts}) => {

const [needRecompile, setNeedRecompile] = useState<boolean>(true);
/*
Expand Down Expand Up @@ -58,12 +44,16 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
localStorage.setItem("code", code);
const newArtifact = compileString(code);
const nameNewArtifact = newArtifact.contractName
const sameArifactExists = artifacts?.find(artifact => nameNewArtifact === artifact.contractName)
if(sameArifactExists){
const confirmOverwrite = confirm("About to overwite existing artifact with same name")
if(!confirmOverwrite) return
}
const newArtifacts = artifacts?.filter(artifact => artifact.contractName !== nameNewArtifact)
setArtifacts([newArtifact, ...newArtifacts ?? []]);
} catch (e: any) {
alert(e.message);
console.error(e.message);
setArtifacts(undefined);
}
}

Expand All @@ -73,7 +63,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
height: 'calc(100vh - 140px)'
}}>
<Editor code={code} setCode={setCode} compile={compile} needRecompile={needRecompile}/>
<ArtifactsInfo artifacts={artifacts} setArtifacts={setArtifacts}/>
<ArtifactsInfo setCode={setCode} artifacts={artifacts} setArtifacts={setArtifacts}/>
</RowFlex>
)
}
Expand Down