From 573da29d9ee45cbd728d9fc2431e228726b44787 Mon Sep 17 00:00:00 2001 From: Mathieu Geukens Date: Tue, 17 Sep 2024 11:34:07 +0200 Subject: [PATCH] add mocknet --- src/components/App.tsx | 10 +++++----- src/components/ContractFunction.tsx | 15 +++++++++++---- src/components/Contracts.tsx | 25 +++++++++++++++++++++++-- src/components/NewContract.tsx | 9 ++++++--- src/components/Wallets.tsx | 27 +++++++++++++++++++++++---- 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 4834628..b5234bd 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1,6 +1,6 @@ // Components import React, { useState } from 'react'; -import { Artifact, ElectrumNetworkProvider, NetworkProvider } from 'cashscript'; +import { Artifact, MockNetworkProvider, NetworkProvider } from 'cashscript'; import Header from './Header' import Main from './Main' import Footer from './Footer'; @@ -13,7 +13,7 @@ import Contracts from './Contracts'; import TransactionBuilder from './TransactionBuilder'; function App() { - const [provider, setProvider] = useState(new ElectrumNetworkProvider("chipnet")) + const [provider, setProvider] = useState(new MockNetworkProvider()) const [wallets, setWallets] = useState([]) const [artifacts, setArtifacts] = useState(undefined); const [contracts, setContracts] = useState(undefined) @@ -41,7 +41,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { if (!currentContract) return // create a separate lists for utxos and mutate entry const utxosList = contracts.map(contract => contract.utxos ?? []) - const contractUtxos = await currentContract.getUtxos(); + const contractUtxos = await provider.getUtxos(currentContract.address); utxosList[contractIndex] = contractUtxos // map is the best way to deep clone array of complex objects const newContracts: ContractInfo[] = contracts.map((contractInfo,index) => ( @@ -54,7 +54,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { if(!contracts) return const utxosPromises = contracts.map(contractInfo => { - const contractUtxos = contractInfo.contract.getUtxos(); + const contractUtxos = provider.getUtxos(contractInfo.contract.address); return contractUtxos ?? [] }) const utxosContracts = await Promise.all(utxosPromises) @@ -82,7 +82,7 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) { - + diff --git a/src/components/ContractFunction.tsx b/src/components/ContractFunction.tsx index 36c5054..47e8ac9 100644 --- a/src/components/ContractFunction.tsx +++ b/src/components/ContractFunction.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react' -import { AbiFunction, NetworkProvider, FunctionArgument, Recipient, SignatureTemplate } from 'cashscript' +import { AbiFunction, NetworkProvider, FunctionArgument, Recipient, SignatureTemplate, MockNetworkProvider } from 'cashscript' import { Form, InputGroup, Button, Card } from 'react-bootstrap' import { readAsType, ExplorerString, Wallet, NamedUtxo, ContractInfo } from './shared' @@ -289,10 +289,17 @@ const ContractFunction: React.FC = ({ contractInfo, abi, provider, wallet // if noAutomaticChange is enabled, add this to the transaction in construction if (noAutomaticChange) transaction.withoutChange().withoutTokenChange() transaction.to(outputs) - const { txid } = await transaction.send() - alert(`Transaction successfully sent: ${ExplorerString[provider.network]}/tx/${txid}`) - console.log(`Transaction successfully sent: ${ExplorerString[provider.network]}/tx/${txid}`) + // check for mocknet + if(provider instanceof MockNetworkProvider){ + await transaction.debug() + alert(`Transaction evalution passed! Bitauth IDE link: ${await transaction.bitauthUri()}`) + console.log(`Transaction evalution passed! Bitauth IDE link: ${await transaction.bitauthUri()}`) + } else { + const { txid } = await transaction.send() + 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/Contracts.tsx b/src/components/Contracts.tsx index fd20c85..a31f5de 100644 --- a/src/components/Contracts.tsx +++ b/src/components/Contracts.tsx @@ -3,14 +3,16 @@ import CopyText from './shared/CopyText' import { Card, Button } from 'react-bootstrap' import { ContractInfo } from './shared' import InfoUtxos from './InfoUtxos' +import { MockNetworkProvider, NetworkProvider, randomUtxo } from 'cashscript' interface Props { + provider: NetworkProvider contracts: ContractInfo[] | undefined setContracts: (contract: ContractInfo[] | undefined) => void updateUtxosContract: (contractName: string) => void } -const Contracts: React.FC = ({ contracts, setContracts, updateUtxosContract }) => { +const Contracts: React.FC = ({ provider, contracts, setContracts, updateUtxosContract }) => { const removeContract = (contractInfo: ContractInfo) => { const contractToRemove = contractInfo.contract @@ -19,6 +21,12 @@ const Contracts: React.FC = ({ contracts, setContracts, updateUtxosContra setContracts(newContracts) } + const addRandomUtxo = (contractInfo:ContractInfo) => { + if(!(provider instanceof MockNetworkProvider)) return + provider.addUtxo(contractInfo.contract.address, randomUtxo()) + updateUtxosContract(contractInfo.contract.name) + } + return (
= ({ contracts, setContracts, updateUtxosContra

loading ...

: (
{contractInfo?.utxos.length} {contractInfo?.utxos.length == 1 ? "utxo" : "utxos"} - {}} style={{cursor:"pointer", marginLeft:"10px"}}> + { undefined === (provider as MockNetworkProvider)?.addUtxo ? (<> + {}} style={{cursor:"pointer", marginLeft:"10px"}}> + ) : null} {contractInfo.utxos.length ?
Show utxos @@ -78,6 +88,17 @@ const Contracts: React.FC = ({ contracts, setContracts, updateUtxosContra
: null}
) } + { undefined !== (provider as MockNetworkProvider)?.addUtxo ? ( +
+ Create new contract utxo +
addRandomUtxo(contractInfo)} style={{cursor:"pointer"}}> + +
+
+ Create custom utxo +

coming soon...

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

loading ...

: diff --git a/src/components/NewContract.tsx b/src/components/NewContract.tsx index 0a37b6d..4e76ac9 100644 --- a/src/components/NewContract.tsx +++ b/src/components/NewContract.tsx @@ -36,19 +36,22 @@ const NewContract: React.FC = ({ artifacts, provider, setProvider, contra ) - function changeNetwork(newNetwork: Network){ - const newprovider = new ElectrumNetworkProvider(newNetwork) + function changeNetwork(newNetwork: "mocknet" | Network){ + const newprovider = newNetwork == "mocknet" ? + new MockNetworkProvider() : new ElectrumNetworkProvider(newNetwork) setProvider(newprovider) } const networkSelector = ( { changeNetwork(event.target.value as Network) }} > + diff --git a/src/components/Wallets.tsx b/src/components/Wallets.tsx index f647ddd..dc6f11f 100644 --- a/src/components/Wallets.tsx +++ b/src/components/Wallets.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from 'react' -import { NetworkProvider } from 'cashscript' +import { MockNetworkProvider, NetworkProvider, randomUtxo } from 'cashscript' import { ColumnFlex, Wallet } from './shared' import { Button, Card } from 'react-bootstrap' import { @@ -123,6 +123,12 @@ const WalletInfo: React.FC = ({provider, wallets, setWallets}) => { setWallets(walletsCopy) } + const addRandomUtxo = (wallet: Wallet, index: number) => { + if(!(provider instanceof MockNetworkProvider)) return + provider.addUtxo(wallet.address, randomUtxo()) + updateUtxosWallet(wallet, index) + } + const walletList = wallets.map((wallet, index) => ( @@ -148,10 +154,23 @@ const WalletInfo: React.FC = ({provider, wallets, setWallets}) => { Wallet utxos
{wallet.utxos?.length} {wallet.utxos?.length == 1 ? "utxo" : "utxos"} - updateUtxosWallet(wallet,index)} style={{cursor:"pointer", marginLeft:"10px"}}> - - + { undefined === (provider as MockNetworkProvider)?.addUtxo ? (<> + updateUtxosWallet(wallet,index)} style={{cursor:"pointer", marginLeft:"10px"}}> + + + ) : null}
+ { undefined !== (provider as MockNetworkProvider)?.addUtxo ? ( +
+ Create new wallet utxo +
addRandomUtxo(wallet, index)} style={{cursor:"pointer"}}> + +
+
+ Create custom utxo +

coming soon...

+
+
) : null} Wallet Balance
{wallet.utxos?.reduce((acc, utxo) => acc + utxo.satoshis, 0n).toString()} satoshis
Private Key