-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractReadLine.tsx
More file actions
181 lines (174 loc) · 4.56 KB
/
ContractReadLine.tsx
File metadata and controls
181 lines (174 loc) · 4.56 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
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Contract } from "ethers";
import { Fragment, useState } from "react";
import { cn } from "../utils/cn";
import Highlight from "react-highlight";
import { LuFileCode } from "react-icons/lu";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { LuChevronDown } from "react-icons/lu";
import { LuZap } from "react-icons/lu";
import { LuSendHorizonal } from "react-icons/lu";
export default function ContractReadLine({
contract,
method,
}: {
contract: Contract;
method: any;
}) {
const [collapse, setCollapse] = useState(false);
const [loading, setLoading] = useState(false);
const [copied, setCopied] = useState(false);
const [showCode, setShowCode] = useState(false);
const [output, setOutput] = useState("");
const [data, setData] = useState(
method.inputs.map((input: any) => ({
...input,
value: "",
}))
);
const sourceCode = () => {
if (method.stateMutability !== "view") {
return `
${
data.length > 0
? data.map((d: any) => `const ${d.name} = "${d.value}";`).join("\n")
: ""
}
const call = await contract["${method.name}"](${data
.map((d: any) => d.name)
.join(", ")});
const tx = await call.wait();`;
}
return `
${
data.length > 0
? data.map((d: any) => `const ${d.name} = "${d.value}";`).join("\n")
: ""
}
const query = await contract["${method.name}"](${data
.map((d: any) => d.name)
.join(", ")});`;
};
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e?.preventDefault();
setLoading(true);
if (data.length > 0) {
const query = await contract[method.name](
...data.map((d: any) => d.value)
);
if (method.stateMutability !== "view") {
const tx = await query.wait();
setOutput(tx.hash);
} else {
setOutput(query.toString());
}
setCollapse(true);
} else {
const query = await contract[method.name]();
setOutput(query.toString());
setCollapse(true);
}
setLoading(false);
};
return (
<form className="flex flex-col w-full gap-2 py-4" onSubmit={handleSubmit}>
<div className="flex place-items-center">
<div className="flex flex-col flex-grow gap-1">
<h3 className="capitalize">{method.name}</h3>
{data.length === 0 ? null : (
<div className="badge badge-accent badge-xs">
{data.length} param(s)
</div>
)}
</div>
<div className="join">
<button
type="submit"
className={cn("btn join-item", {
"bg-warning/20": method.stateMutability !== "view",
"bg-primary/20": method.stateMutability === "view",
})}
>
{loading ? (
<span className="loading loading-spinner"></span>
) : method.stateMutability === "view" ? (
<LuZap size={20} />
) : (
<LuSendHorizonal size={20} />
)}{" "}
{method.stateMutability === "view" ? "Exec" : "Send"}
</button>
<button
type="button"
className={cn("btn bg-secondary/20 join-item")}
onClick={() => {
setShowCode(!showCode);
}}
>
<LuFileCode size={20} /> Code
</button>
{data.length === 0 ? null : (
<button
type="button"
className="btn join-item bg-info/20"
onClick={() => setCollapse(!collapse)}
>
<LuChevronDown
size={20}
className={cn("transition-all", {
"rotate-90": collapse,
})}
/>
</button>
)}
</div>
</div>
{!collapse ? null : (
<Fragment>
{data.length > 0 &&
data.map((input: any, i: number) => (
<div key={i} className="w-full">
<input
type="text"
className="block w-full border-2 input input-bordered"
placeholder={`${input.name} (${input.type})`}
name={input.name}
value={input.value}
onChange={(e) => {
setData((prev: any) =>
prev.map((field: any) => {
if (field.name === input.name) {
field.value = e.target.value;
}
return field;
})
);
}}
/>
</div>
))}
</Fragment>
)}
{output === "" ? null : (
<textarea className="w-full textarea textarea-bordered">
{output}
</textarea>
)}
{!showCode ? null : (
<Fragment>
<Highlight className="select-text javascript">
{sourceCode().trim()}
</Highlight>
<CopyToClipboard
text={sourceCode().trim()}
onCopy={() => setCopied(true)}
>
<button className="btn btn-block">
{copied ? "Copied" : "Copy code"}
</button>
</CopyToClipboard>
</Fragment>
)}
</form>
);
}