-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDeclareContract.tsx
163 lines (150 loc) · 5.28 KB
/
DeclareContract.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
// import { useAccount, useDeclareContract } from "@starknet-react/core"
import { useState } from "react"
import { SectionLayout } from "../SectionLayout"
import { Button } from "@/components/ui/Button"
import { useAccount, useDeclareContract } from "@starknet-react/core"
import { hash } from "starknet"
import { isMainnet, toHexChainid } from "@/helpers/chainId"
const DeclareContract = () => {
const { account, address, chainId } = useAccount()
const { declareAsync } = useDeclareContract({})
const [contractJson, setContractJson] = useState<string | null>(null)
const [compiledClassHashJson, setCompiledClassHashJson] = useState<
string | null
>(null)
const [declaredClassHash, setDeclaredClassHash] = useState<string | null>()
const [txHash, setTxHash] = useState<string | null>()
const [error, setError] = useState<string | null>()
if (!account || !address) {
return null
}
const handleContractChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0]
const reader = new FileReader()
reader.onload = () => {
setContractJson(reader.result as string)
}
reader.readAsText(file)
}
}
const handleCompiledClassHashChange = (
e: React.ChangeEvent<HTMLInputElement>,
) => {
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0]
const reader = new FileReader()
reader.onload = () => {
setCompiledClassHashJson(reader.result as string)
}
reader.readAsText(file)
}
}
const onDeclare = async () => {
try {
const { class_hash, transaction_hash } = await declareAsync({
contract_class: JSON.parse(contractJson || ""),
compiled_class_hash: hash.computeCompiledClassHash(
JSON.parse(compiledClassHashJson || ""),
),
})
setDeclaredClassHash(class_hash)
setTxHash(transaction_hash)
} catch (e) {
setError((e as Error).message)
}
}
const openTxOnVoyager = () => {
const hexChainId = toHexChainid(chainId)
window.open(
isMainnet(hexChainId)
? `https://voyager.online/tx/${txHash}`
: `https://sepolia.voyager.online/tx/${txHash}`,
"_blank",
)
}
const openClassHashOnVoyager = () => {
const hexChainId = toHexChainid(chainId)
window.open(
isMainnet(hexChainId)
? `https://voyager.online/class/${declaredClassHash}`
: `https://sepolia.voyager.online/class/${declaredClassHash}`,
"_blank",
)
}
return (
<SectionLayout sectionTitle="Declare Contract">
<div className="flex flex-1 w-full bg-raisin-black rounded-lg p-3">
<div className="flex flex-col gap-4 w-full">
<span className="text-base font-medium leading-6">
Upload Contract Files
</span>
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<label htmlFor="contract-file" className="text-sm">
Contract File (sierra)
</label>
<input
type="file"
id="contract-file"
accept=".json"
onChange={handleContractChange}
className="w-full outline-none focus:border-white focus:text-white"
/>
</div>
<div className="flex flex-col gap-2">
<label htmlFor="compiled-file" className="text-sm">
Compiled Contract File (casm)
</label>
<input
type="file"
id="compiled-file"
accept=".json"
onChange={handleCompiledClassHashChange}
className="w-full outline-none focus:border-white focus:text-white"
/>
</div>
</div>
</div>
</div>
{error && (
<div className="flex flex-col gap-2 w-full p-4 border border-solid border-raisin-black rounded-lg shadow-md">
<span className="text-md font-semibold text-red-600">Error</span>
<span className="text-sm ">{error}</span>
</div>
)}
{declaredClassHash && txHash && (
<div className="flex flex-col gap-6 w-full p-4 border border-solid border-raisin-black rounded-lg shadow-md">
<div className="flex flex-col items-center">
<span className="text-md font-semibold text-white">
Declared Contract Hash
</span>
<span
className="text-sm text-gray-400 break-all cursor-pointer"
onClick={openClassHashOnVoyager}
>
{declaredClassHash}
</span>
</div>
<div className="flex flex-col items-center">
<span className="text-md font-semibold text-white">
Transaction Hash
</span>
<span
className="text-sm text-gray-400 break-all cursor-pointer"
onClick={openTxOnVoyager}
>
{txHash}
</span>
</div>
</div>
)}
<div className="flex justify-center">
<Button className="w-full mt-3" onClick={onDeclare} hideChevron>
Declare contract
</Button>
</div>
</SectionLayout>
)
}
export { DeclareContract }