forked from Markeljan/web3gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-contract-button.tsx
222 lines (205 loc) · 7.81 KB
/
deploy-contract-button.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use client"
import Link from "next/link"
import { useCallback, useEffect, useMemo, useState } from "react"
import { useAccount, useChains } from "wagmi"
import { useGlobalStore } from "@/app/state/global-store"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger
} from "@/components/ui/dialog"
import { IconExternalLink, IconSpinner } from "@/components/ui/icons"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { useWalletDeploy } from "@/lib/hooks/use-wallet-deploy"
type DeployContractButtonProps = {
getSourceCode: () => string
}
// function to get the contract name from the source code
const getContractName = (sourceCode: string) => {
const contractNameRegex = /contract\s+(\w+)\s*(?:is|{)/
const contractNameMatch = contractNameRegex.exec(sourceCode)
return contractNameMatch ? contractNameMatch[1] : ""
}
export const DeployContractButton = ({ getSourceCode }: DeployContractButtonProps) => {
const { deploy: deployWithWallet } = useWalletDeploy()
const [explorerUrl, setExplorerUrl] = useState<string>("")
const [ipfsUrl, setIpfsUrl] = useState<string>("")
const [constructorArgValues, setConstructorArgValues] = useState<string[]>([])
const [constructorArgNames, setConstructorArgNames] = useState<string[]>([])
const [isErrorDeploying, setIsErrorDeploying] = useState<boolean>(false)
const [sourceCode, setSourceCode] = useState<string>("")
const { isDeploying, setIsDeploying } = useGlobalStore()
const supportedChains = useChains()
const { chain } = useAccount()
const [isDialogOpen, setIsDialogOpen] = useState(false);
const isSupportedChain = useMemo(
() => !!chain && supportedChains.find((c) => c.id === chain.id),
[chain, supportedChains]
)
const generateConstructorArgs = useCallback(() => {
const constructorArgsRegex = /constructor\(([^)]+)\)/
const constructorArgsMatch = constructorArgsRegex.exec(sourceCode)
if (!constructorArgsMatch) return []
const constructorArgs = constructorArgsMatch[1]
const constructorArgsArray = constructorArgs.split(",")
return constructorArgsArray.map((arg) => arg.trim())
}, [sourceCode])
useEffect(() => {
const args = generateConstructorArgs()
setConstructorArgNames(args.map((arg) => arg.split(" ").pop() || ""))
setConstructorArgValues(args.map(() => ""))
}, [generateConstructorArgs])
const handleInputChange = useCallback((index: number, value: string) => {
setConstructorArgValues((prev) => {
const newValues = [...prev]
newValues[index] = value
return newValues
})
}, [])
const generatedConstructorFields = useMemo(() => {
return constructorArgNames.map((arg, index) => (
<div key={`${arg}`} className="flex flex-col gap-2">
<Label className="text-sm font-medium">{arg}</Label>
<Input
type="text"
placeholder={arg}
value={constructorArgValues[index]}
onChange={(e) => {
e.preventDefault()
handleInputChange(index, e.target.value)
}}
className="rounded-md border border-gray-300 p-2"
/>
</div>
))
}, [constructorArgNames, constructorArgValues, handleInputChange])
const contractName = useMemo(() => getContractName(sourceCode), [sourceCode])
const handleClickDeploy = async () => {
setIsDeploying(true)
setIsErrorDeploying(false)
try {
const deploymentData = await deployWithWallet({
contractName,
sourceCode,
constructorArgs: constructorArgValues
})
if (!deploymentData) {
setIsErrorDeploying(true)
setIsDeploying(false)
return
}
const { explorerUrl, ipfsUrl } = deploymentData
if (!explorerUrl || !ipfsUrl) {
setIsErrorDeploying(true)
setIsDeploying(false)
return
}
explorerUrl && setExplorerUrl(explorerUrl)
setIpfsUrl(ipfsUrl)
setIsDeploying(false)
} catch (e) {
console.error(e)
setIsErrorDeploying(true)
setIsDeploying(false)
}
}
return (
<div className="ml-4 flex w-full justify-end">
<Dialog
open={isDialogOpen}
onOpenChange={(isOpen) => {
setIsDialogOpen(isOpen);
if (!isOpen && !isDeploying) {
setIsErrorDeploying(false)
}
}}
>
<DialogTrigger asChild>
<Button
onClick={() => {
setSourceCode(getSourceCode())
setIsDialogOpen(true);
}}
className="mr-2 text-primary-foreground"
variant="default"
disabled={!isSupportedChain}
size="sm"
>
<p className="hidden sm:flex">Deploy Contract</p>
<p className="flex sm:hidden">Deploy</p>
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Manually Deploy Contract</DialogTitle>
<DialogDescription>
Deploy the contract using your wallet. Must be connected to a supported testnet.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-4 py-4">
<div className="flex flex-col gap-2">
<div className="flex">
<p className="text-sm font-medium">Agent Deploy</p>
<Badge variant="default" className="ml-2 rounded">
RECOMMENDED
</Badge>
</div>
<p className="text-sm text-gray-500">
{`This method does not require wallet connection. Just use the keyword "deploy" in the chat.`}
</p>
</div>
<div className="flex flex-col gap-2">
<div className="flex">
<p className="text-sm font-medium">Deploy with Wallet</p>
<Badge variant="destructive" className="ml-2 rounded">
ADVANCED
</Badge>
</div>
<p className="text-sm text-gray-500">
Sign and deploy the contract using your own wallet. Be cautious of risks and network fees.
</p>
</div>
{constructorArgNames.length > 0 ? (
<div className="flex max-h-48 flex-col gap-4 overflow-y-auto rounded border-2 p-4">
<DialogTitle className="text-md">Constructor Arguments</DialogTitle>
{generatedConstructorFields}
</div>
) : null}
</div>
<div className="flex flex-col items-center gap-4">
{isErrorDeploying && <p className="text-sm text-destructive">Error deploying contract.</p>}
{isDeploying && <IconSpinner className="size-8 animate-spin text-gray-500" />}
{explorerUrl && (
<Link href={explorerUrl} target="_blank" rel="noopener noreferrer" className="text-sm text-green-500">
<div className="flex items-center">
View on Explorer
<IconExternalLink className="ml-1" />
</div>
</Link>
)}
{ipfsUrl && (
<Link href={ipfsUrl} target="_blank" rel="noopener noreferrer" className="text-sm text-blue-500">
<div className="flex items-center">
View on IPFS
<IconExternalLink className="ml-1" />
</div>
</Link>
)}
</div>
<DialogFooter>
<Button className="mb-4 p-6 sm:p-4" disabled={isDeploying} onClick={handleClickDeploy} variant="secondary">
Deploy with Wallet
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}