diff --git a/src/components/App.tsx b/src/components/App.tsx index d50104f..4834628 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1,6 +1,6 @@ // Components import React, { useState } from 'react'; -import { Artifact, Network } from 'cashscript'; +import { Artifact, ElectrumNetworkProvider, NetworkProvider } from 'cashscript'; import Header from './Header' import Main from './Main' import Footer from './Footer'; @@ -13,12 +13,12 @@ import Contracts from './Contracts'; import TransactionBuilder from './TransactionBuilder'; function App() { - const [network, setNetwork] = useState('chipnet') + const [provider, setProvider] = useState(new ElectrumNetworkProvider("chipnet")) const [wallets, setWallets] = useState([]) const [artifacts, setArtifacts] = useState(undefined); const [contracts, setContracts] = useState(undefined) const [code, setCode] = useState( -`pragma cashscript >= 0.8.0; +`pragma cashscript >= 0.10.0; contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { // Require recipient's signature to match @@ -79,16 +79,16 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
- + - + - + diff --git a/src/components/ContractCreation.tsx b/src/components/ContractCreation.tsx index 783611b..55624ff 100644 --- a/src/components/ContractCreation.tsx +++ b/src/components/ContractCreation.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react' -import { Artifact, Contract, ConstructorArgument, Network, ElectrumNetworkProvider } from 'cashscript' +import { Artifact, Contract, ConstructorArgument, NetworkProvider } from 'cashscript' import { InputGroup, Form, Button } from 'react-bootstrap' import { readAsConstructorType, ContractInfo, TinyContractObj } from './shared' @@ -7,11 +7,11 @@ interface Props { artifact?: Artifact contracts?: ContractInfo[] setContracts: (contracts?: ContractInfo[]) => void - network: Network + provider: NetworkProvider updateUtxosContract: (contractName: string) => void } -const ContractCreation: React.FC = ({ artifact, contracts, setContracts, network, updateUtxosContract}) => { +const ContractCreation: React.FC = ({ artifact, contracts, setContracts, provider, updateUtxosContract}) => { const [constructorArgs, setConstructorArgs] = useState([]) const [nameContract, setNameContract] = useState(""); const [createdContract, setCreatedContract] = useState(false); @@ -52,7 +52,6 @@ const ContractCreation: React.FC = ({ artifact, contracts, setContracts, function createContract() { if (!artifact) return try { - const provider = new ElectrumNetworkProvider(network) const newContract = new Contract(artifact, constructorArgs, { provider }) newContract.name = nameContract const contractInfo = {contract: newContract, utxos: undefined, args: constructorArgs} diff --git a/src/components/ContractFunction.tsx b/src/components/ContractFunction.tsx index b1aaae2..36c5054 100644 --- a/src/components/ContractFunction.tsx +++ b/src/components/ContractFunction.tsx @@ -1,17 +1,17 @@ import React, { useState, useEffect } from 'react' -import { AbiFunction, FunctionArgument, Network, Recipient, SignatureTemplate, Utxo } from 'cashscript' +import { AbiFunction, NetworkProvider, FunctionArgument, Recipient, SignatureTemplate } from 'cashscript' import { Form, InputGroup, Button, Card } from 'react-bootstrap' import { readAsType, ExplorerString, Wallet, NamedUtxo, ContractInfo } from './shared' interface Props { contractInfo: ContractInfo abi?: AbiFunction - network: Network + provider: NetworkProvider wallets: Wallet[] updateUtxosContract: (contractName: string) => void } -const ContractFunction: React.FC = ({ contractInfo, abi, network, wallets, updateUtxosContract }) => { +const ContractFunction: React.FC = ({ contractInfo, abi, provider, wallets, updateUtxosContract }) => { const [functionArgs, setFunctionArgs] = useState([]) const [outputs, setOutputs] = useState([{ to: '', amount: 0n }]) // transaction inputs, not the same as abi.inputs @@ -291,8 +291,8 @@ const ContractFunction: React.FC = ({ contractInfo, abi, network, wallets transaction.to(outputs) const { txid } = await transaction.send() - alert(`Transaction successfully sent: ${ExplorerString[network]}/tx/${txid}`) - console.log(`Transaction successfully sent: ${ExplorerString[network]}/tx/${txid}`) + alert(`Transaction successfully sent: ${ExplorerString[provider.network]}/tx/${txid}`) + console.log(`Transaction successfully sent: ${ExplorerString[provider.network]}/tx/${txid}`) updateUtxosContract(contract.name) } catch (e: any) { alert(e.message) diff --git a/src/components/ContractFunctions.tsx b/src/components/ContractFunctions.tsx index ae92fae..3c0e199 100644 --- a/src/components/ContractFunctions.tsx +++ b/src/components/ContractFunctions.tsx @@ -1,18 +1,18 @@ import React from 'react' -import { Network } from 'cashscript' +import { NetworkProvider } from 'cashscript' import ContractFunction from './ContractFunction' import { Wallet, ContractInfo } from './shared' interface Props { contractInfo: ContractInfo - network: Network + provider: NetworkProvider wallets: Wallet[] updateUtxosContract: (contractName: string) => void } -const ContractFunctions: React.FC = ({ contractInfo, network, wallets, updateUtxosContract }) => { +const ContractFunctions: React.FC = ({ contractInfo, provider, wallets, updateUtxosContract }) => { const functions = contractInfo.contract.artifact?.abi.map(func => ( - + )) return ( diff --git a/src/components/NewContract.tsx b/src/components/NewContract.tsx index 83b77f3..0a37b6d 100644 --- a/src/components/NewContract.tsx +++ b/src/components/NewContract.tsx @@ -1,19 +1,19 @@ import React, { useState } from 'react' -import { Artifact, Network } from 'cashscript' +import { Artifact, NetworkProvider, Network, MockNetworkProvider, ElectrumNetworkProvider } from 'cashscript' import { Form } from 'react-bootstrap' import ContractCreation from './ContractCreation'; import { ContractInfo } from './shared' interface Props { artifacts?: Artifact[] - network: Network - setNetwork: (network: Network) => void + provider: NetworkProvider + setProvider: (provider: NetworkProvider) => void updateUtxosContract: (contractName: string) => void contracts: ContractInfo[] | undefined setContracts: (contract: ContractInfo[] | undefined) => void } -const NewContract: React.FC = ({ artifacts, network, setNetwork, contracts, setContracts, updateUtxosContract }) => { +const NewContract: React.FC = ({ artifacts, provider, setProvider, contracts, setContracts, updateUtxosContract }) => { const [selectedArifact, setSelectedArtifact] = useState(undefined); @@ -36,18 +36,23 @@ const NewContract: React.FC = ({ artifacts, network, setNetwork, contract ) + function changeNetwork(newNetwork: Network){ + const newprovider = new ElectrumNetworkProvider(newNetwork) + setProvider(newprovider) + } + const networkSelector = ( { - setNetwork(event.target.value as Network) + changeNetwork(event.target.value as Network) }} > - + - + ) @@ -67,15 +72,15 @@ const NewContract: React.FC = ({ artifacts, network, setNetwork, contract

New Contract

{artifacts?.length ? <> -

Choose the contract Arifact and network to use:

- Select Artifact: {artifactSelector} + Select target Network: {networkSelector}
+

Choose the contract Arifact to use:

- Select target Network: {networkSelector} + Select Artifact: {artifactSelector}
{selectedArifact !== undefined ? - + : null } diff --git a/src/components/TransactionBuilder.tsx b/src/components/TransactionBuilder.tsx index 17457b4..5721e3f 100644 --- a/src/components/TransactionBuilder.tsx +++ b/src/components/TransactionBuilder.tsx @@ -1,17 +1,17 @@ import React, {useState} from 'react' -import { Network } from 'cashscript' +import { NetworkProvider } from 'cashscript' import ContractFunctions from './ContractFunctions'; import { Wallet, ContractInfo } from './shared' import { Form } from 'react-bootstrap' interface Props { - network: Network + provider: NetworkProvider wallets: Wallet[] updateUtxosContract: (contractName: string) => void contracts: ContractInfo[] | undefined } -const TransactionBuilder: React.FC = ({ network, wallets, contracts, updateUtxosContract }) => { +const TransactionBuilder: React.FC = ({ provider, wallets, contracts, updateUtxosContract }) => { const [selectedContract, setSelectedContract] = useState(undefined); @@ -55,7 +55,7 @@ const TransactionBuilder: React.FC = ({ network, wallets, contracts, upda } {selectedContract !== undefined ? - + : null } diff --git a/src/components/Wallets.tsx b/src/components/Wallets.tsx index 4e90139..f647ddd 100644 --- a/src/components/Wallets.tsx +++ b/src/components/Wallets.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from 'react' -import { Network, ElectrumNetworkProvider } from 'cashscript' +import { NetworkProvider } from 'cashscript' import { ColumnFlex, Wallet } from './shared' import { Button, Card } from 'react-bootstrap' import { @@ -16,12 +16,12 @@ import CopyText from './shared/CopyText' import InfoUtxos from './InfoUtxos' interface Props { - network: Network + provider: NetworkProvider wallets: Wallet[] setWallets:(wallets: Wallet[]) => void } -const WalletInfo: React.FC = ({network, wallets, setWallets}) => { +const WalletInfo: React.FC = ({provider, wallets, setWallets}) => { useEffect(() => { const localStorageData = localStorage.getItem("wallets"); // If the local storage is null @@ -91,9 +91,8 @@ const WalletInfo: React.FC = ({network, wallets, setWallets}) => { } setWallets(newWallets); // fetch UTXOs - const networkProvider = new ElectrumNetworkProvider(network) for (const wallet of newWallets){ - const walletUtxos = await networkProvider.getUtxos(wallet.address); + const walletUtxos = await provider.getUtxos(wallet.address); wallet.utxos = walletUtxos } setWallets(newWallets); @@ -118,7 +117,7 @@ const WalletInfo: React.FC = ({network, wallets, setWallets}) => { }, [wallets]); async function updateUtxosWallet (wallet: Wallet, index: number) { - const walletUtxos = await new ElectrumNetworkProvider(network).getUtxos(wallet.address); + const walletUtxos = await provider.getUtxos(wallet.address); const walletsCopy = [...wallets] walletsCopy[index].utxos = walletUtxos setWallets(walletsCopy) @@ -142,10 +141,10 @@ const WalletInfo: React.FC = ({network, wallets, setWallets}) => { {wallet.pubKeyHex} Pubkeyhash hex: {wallet.pubKeyHashHex} - {network==="mainnet"? "Address:" : "Testnet Address:"} - {network==="mainnet"? wallet.address : wallet.testnetAddress} - {network==="mainnet"? "Token address:" : "Testnet Token Address:"} - {network==="mainnet"? hash160ToCash(wallet.pubKeyHashHex, false, true) : hash160ToCash(wallet.pubKeyHashHex, true, true)} + {provider.network==="mainnet"? "Address:" : "Testnet Address:"} + {provider.network==="mainnet"? wallet.address : wallet.testnetAddress} + {provider.network==="mainnet"? "Token address:" : "Testnet Token Address:"} + {provider.network==="mainnet"? hash160ToCash(wallet.pubKeyHashHex, false, true) : hash160ToCash(wallet.pubKeyHashHex, true, true)} Wallet utxos
{wallet.utxos?.length} {wallet.utxos?.length == 1 ? "utxo" : "utxos"} @@ -159,7 +158,7 @@ const WalletInfo: React.FC = ({network, wallets, setWallets}) => {
Show Private Key WIF: - {encodePrivateKeyWif(wallet.privKey, network === "mainnet" ? "mainnet" : "testnet")} + {encodePrivateKeyWif(wallet.privKey, provider.network === "mainnet" ? "mainnet" : "testnet")} Hex: {wallet.privKeyHex}