-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContracts.tsx
More file actions
132 lines (126 loc) · 5.91 KB
/
Contracts.tsx
File metadata and controls
132 lines (126 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React from 'react'
import CopyText from './shared/CopyText'
import { Card, Button } from 'react-bootstrap'
import { ContractInfo } from './shared'
import InfoUtxos from './shared/InfoUtxos'
import { MockNetworkProvider, NetworkProvider, randomUtxo } from 'cashscript'
import CreateUtxo from './shared/CreateUtxo'
interface Props {
provider: NetworkProvider
contracts: ContractInfo[] | undefined
setContracts: (contract: ContractInfo[] | undefined) => void
updateUtxosContract: (contractName: string) => void
}
const Contracts: React.FC<Props> = ({ provider, contracts, setContracts, updateUtxosContract }) => {
const removeContract = (contractInfo: ContractInfo) => {
const contractToRemove = contractInfo.contract
const newContracts = contracts?.filter(ci => ci.contract.name !== contractToRemove.name)
setContracts(newContracts)
}
const addRandomUtxo = (contractInfo:ContractInfo) => {
if(!(provider instanceof MockNetworkProvider)) return
const contract = contractInfo.contract as any
const address = contract.contractType === 'p2s' ? contract.lockingBytecode : contract.address
provider.addUtxo(address, randomUtxo())
updateUtxosContract(contractInfo.contract.name)
}
return (
<div style={{
height: 'calc(100vh - 170px)',
border: '2px solid black',
borderBottom: '1px solid black',
fontSize: '100%',
lineHeight: 'inherit',
overflow: 'auto',
background: '#fffffe',
padding: '8px 16px',
color: '#000',
margin: "16px"
}}>
<h2>Contracts</h2>
{contracts == undefined ? <p>
No Contracts created yet...
</p>:null}
{contracts?.map((contractInfo) => {
const contract = contractInfo.contract as any
const isP2s = contract.contractType === 'p2s'
return (
<Card style={{ marginBottom: '10px' }} key={contractInfo.contract.name + (isP2s ? contract.lockingBytecode : contract.address)}>
<Card.Header style={{ display:"flex", justifyContent:"space-between"}}>
<div>{contractInfo.contract.name}</div>
<img src='./trash.svg' onClick={() => removeContract(contractInfo)} style={{padding: "0px 6px", width: "28px", cursor:"pointer"}}/>
</Card.Header>
<Card.Body>
<div style={{ margin: '5px', width: '100%' }}>
<strong>Contract type: </strong>
<span>{contract.contractType}</span><br/>
{!isP2s && <>
<strong>Contract address</strong>
<CopyText>{contract.address}</CopyText>
<strong>Contract token address</strong>
<CopyText>{contract.tokenAddress}</CopyText>
</>}
<strong>Contract locking bytecode</strong>
<CopyText>{contract.lockingBytecode}</CopyText>
<strong>Contract artifact</strong>
<p>{contractInfo.contract.artifact.contractName}</p>
<strong>Contract arguments</strong>
<p>{contractInfo.args.length} {contractInfo.args.length === 1 ? "argument" : "arguments"}</p>
{contractInfo.args.length > 0 && (
<details>
<summary>Details</summary>
<div>
{contractInfo.args.map((arg, index) => (<div key={`${contractInfo.contract.name}-arg-${index}`}>
{contractInfo.contract.artifact.constructorInputs[index]?.type} {contractInfo.contract.artifact.constructorInputs[index]?.name + ": "}
{typeof arg == "bigint" ? arg.toString() : null}
{typeof arg == "string" || typeof arg == "number" ? arg : null}
</div>))}
</div>
</details>
)}
<strong>Contract utxos</strong>
{contractInfo?.utxos == undefined?
<p>loading ...</p>:
(<div>
{contractInfo?.utxos.length} {contractInfo?.utxos.length == 1 ? "utxo" : "utxos"}
<details style={{width: "fit-content"}}>
<summary>Show utxos</summary>
<div>
<InfoUtxos utxos={contractInfo?.utxos}/>
</div>
</details>
{ undefined === (provider as MockNetworkProvider)?.addUtxo ? (
<div style={{cursor:"pointer", marginLeft:"10px"}}>
<Button size='sm' onClick={() => updateUtxosContract(contractInfo.contract.name)} variant='secondary' style={{padding:" 0px 2px"}}>refresh ⭯</Button>
</div>)
: null}
</div>)
}
{ undefined !== (provider as MockNetworkProvider)?.addUtxo ? (
<div>
<strong>Create new contract utxo</strong>
<div onClick={() => addRandomUtxo(contractInfo)} style={{cursor:"pointer"}}>
<Button size='sm' variant='secondary' style={{padding:"0px 2px"}}>add random utxo</Button>
</div>
<details style={{maxWidth: "50%"}}>
<summary>Create custom utxo</summary>
<CreateUtxo provider={provider} address={isP2s ? contract.lockingBytecode : contract.address}
updateUtxos={() => updateUtxosContract(contractInfo.contract.name)}/>
</details>
</div>) : null}
<strong>Total contract balance</strong>
{contractInfo.utxos == undefined?
<p>loading ...</p>:
<p>{contractInfo.utxos?.reduce((acc, utxo) => acc + utxo.satoshis, 0n).toString()} satoshis</p>
}
<strong>Contract size</strong>
<p>{contractInfo.contract.bytesize} bytes (max {isP2s ? '201' : '10,000'} bytes)</p>
</div>
</Card.Body>
</Card>
)
})}
</div>
)
}
export default Contracts