diff --git a/src/components/App.tsx b/src/components/App.tsx index db6fc57..61c38dc 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -16,7 +16,7 @@ function App() { const [network, setNetwork] = useState('chipnet') const [wallets, setWallets] = useState([]) const [artifacts, setArtifacts] = useState(undefined); - const [contract, setContract] = useState(undefined) + const [contracts, setContracts] = useState(undefined) const [utxos, setUtxos] = useState(undefined) const [balance, setBalance] = useState(undefined) const [code, setCode] = useState( @@ -35,7 +35,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { } } `); - + const contract = contracts?.[0] // TODO: delete this async function updateUtxosContract () { if (!contract) return setBalance(await contract.getBalance()) @@ -56,10 +56,10 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
- + - + diff --git a/src/components/ContractCreation.tsx b/src/components/ContractCreation.tsx index e292e6a..cbf9852 100644 --- a/src/components/ContractCreation.tsx +++ b/src/components/ContractCreation.tsx @@ -1,25 +1,24 @@ import React, { useState, useEffect } from 'react' import { Artifact, Contract, Argument, Network, ElectrumNetworkProvider, Utxo } from 'cashscript' import { InputGroup, Form, Button } from 'react-bootstrap' -// import { QRFunc } from 'react-qrbtf' import { readAsType } from './shared' -import CopyText from './shared/CopyText' -import InfoUtxos from './InfoUtxos' interface Props { artifact?: Artifact - contract?: Contract - setContract: (contract?: Contract) => void + contracts?: Contract[] + setContracts: (contract?: Contract[]) => void network: Network utxos: Utxo[] | undefined balance: bigint | undefined updateUtxosContract: () => void } -const ContractCreation: React.FC = ({ artifact, contract, setContract, network, balance, utxos, updateUtxosContract}) => { +const ContractCreation: React.FC = ({ artifact, contracts, setContracts, network, balance, utxos, updateUtxosContract}) => { const [args, setArgs] = useState([]) - useEffect(() => { + const contract = contracts?.[0] // TODO: delete this + + const resetInputFields = () => { // This code is suuper ugly but I haven't found any other way to clear the value // of the input fields. artifact?.constructorInputs.forEach((input, i) => { @@ -31,7 +30,7 @@ const ContractCreation: React.FC = ({ artifact, contract, setContract, ne const newArgs = artifact?.constructorInputs.map(() => '') || [] setArgs(newArgs) - }, [artifact]) + } useEffect(() => { updateUtxosContract() @@ -62,7 +61,9 @@ const ContractCreation: React.FC = ({ artifact, contract, setContract, ne try { const provider = new ElectrumNetworkProvider(network) const newContract = new Contract(artifact, args, { provider }) - setContract(newContract) + setContracts([newContract, ...contracts ?? []]) + alert("created contract!") + resetInputFields() } catch (e: any) { alert(e.message) console.error(e.message) @@ -76,40 +77,6 @@ const ContractCreation: React.FC = ({ artifact, contract, setContract, ne
{artifact?.contractName}

Initialise contract by providing contract arguments:

{constructorForm} - {contract !== undefined && -
-
- Contract address (p2sh32) - {contract.address} - Contract token address (p2sh32) - {contract.tokenAddress} - Contract utxos - {utxos == undefined? -

loading ...

: - (<> -

{utxos.length} {utxos.length == 1 ? "utxo" : "utxos"}

- {utxos.length ? -
- Show utxos -
- -
-
: null} - ) - } - Total contract balance - {balance == undefined? -

loading ...

: -

{balance.toString()} satoshis

- } - Contract size -

{contract.bytesize} bytes (max 520), {contract.opcount} opcodes (max 201)

-
- {/*
- -
*/} -
- } ) } diff --git a/src/components/ContractInfo.tsx b/src/components/ContractInfo.tsx index 5e65f6d..fad5a66 100644 --- a/src/components/ContractInfo.tsx +++ b/src/components/ContractInfo.tsx @@ -10,14 +10,16 @@ interface Props { balance: bigint | undefined utxos: Utxo[] | undefined updateUtxosContract: () => void - contract: Contract | undefined - setContract: (contract: Contract | undefined) => void + contracts: Contract[] | undefined + setContracts: (contract: Contract[] | undefined) => void } -const ContractInfo: React.FC = ({ artifact, network, contract, setContract, utxos, balance, updateUtxosContract }) => { +const ContractInfo: React.FC = ({ artifact, network, contracts, setContracts, utxos, balance, updateUtxosContract }) => { const [electrumClient, setElectrumClient] = useState(undefined) - useEffect(() => setContract(undefined), [artifact]) + const contract = contracts?.[0] // TODO: delete this + + useEffect(() => setContracts(undefined), [artifact]) async function initElectrumSubscription(){ if(electrumClient) electrumClient?.disconnect() @@ -42,11 +44,11 @@ const ContractInfo: React.FC = ({ artifact, network, contract, setContrac useEffect(() => { initElectrumSubscription() - }, [contract]) + }, [contracts]) return ( - + ) } diff --git a/src/components/Contracts.tsx b/src/components/Contracts.tsx index 2c118d3..9350ed7 100644 --- a/src/components/Contracts.tsx +++ b/src/components/Contracts.tsx @@ -1,18 +1,23 @@ import React from 'react' import { Artifact, Contract, Network, Utxo } from 'cashscript' +import CopyText from './shared/CopyText' +import { Card } from 'react-bootstrap' interface Props { - artifacts?: Artifact[] - network: Network - setNetwork: (network: Network) => void + contracts: Contract[] | undefined + setContracts: (contract: Contract[] | undefined) => void balance: bigint | undefined utxos: Utxo[] | undefined updateUtxosContract: () => void - contract: Contract | undefined - setContract: (contract: Contract | undefined) => void } -const Contracts: React.FC = ({ artifacts, network, setNetwork, contract, setContract, utxos, balance, updateUtxosContract }) => { +const Contracts: React.FC = ({ contracts, setContracts, utxos, balance, updateUtxosContract }) => { + + const removeContract = (contractToRemove: Contract) => { + const contractToRemoveAddress = contractToRemove.address; + const newContracts = contracts?.filter(contract => contract.address !== contractToRemoveAddress) + setContracts(newContracts) + } return (
= ({ artifacts, network, setNetwork, contract, margin: "16px" }}>

Contracts

- hello world + {contracts == undefined ?

+ No Contracts created yet... +

:null} + {contracts?.map(contract => ( + + +
{contract.name}
+ removeContract(contract)} style={{padding: "0px 6px", width: "fit-content", cursor:"pointer"}}/> +
+ +
+ Contract address (p2sh32) + {contract.address} + Contract token address (p2sh32) + {contract.tokenAddress} + Contract utxos + {utxos == undefined? +

loading ...

: + (<> +

{utxos.length} {utxos.length == 1 ? "utxo" : "utxos"}

+ {utxos.length ? +
+ Show utxos +
+
+
: null} + ) + } + Total contract balance + {balance == undefined? +

loading ...

: +

{balance.toString()} satoshis

+ } + Contract size +

{contract.bytesize} bytes (max 520), {contract.opcount} opcodes (max 201)

+
+
+
+ ))}
) } diff --git a/src/components/NewContract.tsx b/src/components/NewContract.tsx index b9e7479..ef4601f 100644 --- a/src/components/NewContract.tsx +++ b/src/components/NewContract.tsx @@ -10,11 +10,11 @@ interface Props { balance: bigint | undefined utxos: Utxo[] | undefined updateUtxosContract: () => void - contract: Contract | undefined - setContract: (contract: Contract | undefined) => void + contracts: Contract[] | undefined + setContracts: (contract: Contract[] | undefined) => void } -const NewContract: React.FC = ({ artifacts, network, setNetwork, contract, setContract, utxos, balance, updateUtxosContract }) => { +const NewContract: React.FC = ({ artifacts, network, setNetwork, contracts, setContracts, utxos, balance, updateUtxosContract }) => { const [selectedArifact, setSelectedArtifact] = useState(undefined); @@ -76,11 +76,11 @@ const NewContract: React.FC = ({ artifacts, network, setNetwork, contract Select target Network: {networkSelector} {selectedArifact !== undefined ? - + : null } - :

Create a Artifact first.

+ :

Create an Artifact first.

} )