-
Notifications
You must be signed in to change notification settings - Fork 3
/
Minter.tsx
276 lines (252 loc) · 10.7 KB
/
Minter.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { useEffect, useState } from "react";
import { ethers } from "ethers";
import NFT from "../artifacts/contracts/SleepyKoala.sol/ISleepyKoala.json";
import { formatFixed } from "@ethersproject/bignumber";
import { useRouter } from "next/router";
const contractAddress = "0x8e1Ef80410758A45387E3777bba7cA814Be61B8E";
interface Props {
wallet: string;
hasNft: boolean;
setHasNft: React.Dispatch<React.SetStateAction<any>>;
}
interface IContract {
hasNFT(): Promise<boolean>;
mintNFT(): Promise<void>;
}
class MockContract implements IContract {
async hasNFT(): Promise<boolean> {
return true;
}
async mintNFT(): Promise<void> {}
}
const Minter: React.FC<Props> = ({ wallet }) => {
const [injectContract, setInjectContract] = useState<
IContract | MockContract
>();
const [loading, setLoading] = useState(false);
const [hasNft, setHasNft] = useState(false);
const [hasTrueNft, setHasTrueNft] = useState(false);
const [tokenId, setTokenId] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const router = useRouter();
const mintNFT = async () => {
try {
const { ethereum } = window;
const provider = new ethers.providers.Web3Provider(
ethereum,
window.ethereum.givenProvider
);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(
contractAddress,
NFT.abi,
signer
);
setLoading(true);
let nftTx = await nftContract.functions.mint(wallet, false);
console.log("Mining....", nftTx.hash);
let tx = await nftTx.wait();
console.log("Mined!", tx);
setHasNft(true);
setLoading(false);
console.log(
`Mined, see transaction: https://rinkeby.etherscan.io/tx/${nftTx.hash}`
);
getNFTdata(tx);
} catch (error) {
console.log("Error minting character", error);
setErrorMessage("Minting Error");
setLoading(false);
}
};
function getNFTdata(contractTX: any) {
const { ethereum } = window;
const provider = new ethers.providers.Web3Provider(
ethereum,
window.ethereum.givenProvider
);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(
contractAddress,
NFT.abi,
signer
);
const tokenId = contractTX.logs[0].topics[3];
const tokenInt = ethers.BigNumber.from(tokenId);
const token = formatFixed(tokenInt, 0);
setTokenId(token);
nftContract.getTokenData(token).then((data: boolean) => {
setHasTrueNft(data);
});
}
function invertNft(tokenId: string) {
const { ethereum } = window;
const provider = new ethers.providers.Web3Provider(
ethereum,
window.ethereum.givenProvider
);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(
contractAddress,
NFT.abi,
signer
);
setLoading(true);
nftContract.functions
.invertTokenData(tokenId)
.then(async (data: any) => {
let tx = await data.wait();
console.log("Mined!", tx);
console.log(
`Mined, see transaction: https://rinkeby.etherscan.io/tx/${data.hash}`
);
nftContract.getTokenData(tokenId).then((data: any) => {
console.log("getTokenData", data);
setHasTrueNft(data);
setErrorMessage("");
setLoading(false);
router.push("/success");
});
})
.catch(() => {
setErrorMessage("Inverting NFT Error");
setLoading(false);
});
}
const NFTsymbol = "KOL";
return (
<>
<div className="relative p-5 bg-white rounded-lg shadow mt-4">
<div className="relative">
<div className="text-center">
<div className="mt-8 text-center">
<h2 className="text-l">Welcome {wallet}</h2>
</div>
{!hasNft ? (
<div>
<div className="mt-6">
<h2 className="text-l text-gray-600">
You need to mint {NFTsymbol}
</h2>
</div>
<div className="mt-6 md:mt-8">
{loading ? (
<button
type="button"
className="w-full md:w-auto text-sm bg-blue-600 px-4 py-2 text-white rounded-3xl font-medium"
disabled
>
<svg
role="status"
className="inline mr-2 w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-white align-baseline"
viewBox="0 0 100 100"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
Minting...
</button>
) : !errorMessage ? (
<button
className="w-full md:w-auto text-sm bg-gray-500 px-4 py-2 text-white rounded-3xl font-medium hover:bg-blue-600"
onClick={() => mintNFT()}
>
Mint {NFTsymbol}
</button>
) : (
<div>
<button
type="button"
className="w-full md:w-auto text-sm bg-red-600 px-4 py-2 text-white rounded-3xl font-medium mr-2"
disabled
>
{errorMessage}
</button>
<button
className="w-full md:w-auto text-sm bg-blue-600 px-4 py-2 text-white rounded-3xl font-medium"
onClick={() => window.location.reload()}
>
Click to Reload!
</button>
</div>
)}
</div>
</div>
) : (
<div>
<div>
<div className="mt-6">
<h2 className="text-l text-gray-600">
{!hasTrueNft && `Unfortunately your NFT doesn't meet the conditions for entry `}
</h2>
</div>
<div className="mt-6 md:mt-8">
{loading ? (
<button
type="button"
className="w-full md:w-auto text-sm bg-blue-600 px-4 py-2 text-white rounded-3xl font-medium"
disabled
>
<svg
role="status"
className="inline mr-2 w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-white align-baseline"
viewBox="0 0 100 100"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
Inverting...
</button>
) : !errorMessage ? (
!hasTrueNft && (
<button
className="w-full md:w-auto text-sm bg-gray-500 px-4 py-2 text-white rounded-3xl font-medium hover:bg-blue-600"
onClick={() => invertNft(tokenId)}
>
Invert {NFTsymbol}
</button>
)
) : (
<div>
<button
type="button"
className="w-full md:w-auto text-sm bg-red-600 px-4 py-2 text-white rounded-3xl font-medium mr-2"
disabled
>
{errorMessage}
</button>
<button
className="w-full md:w-auto text-sm bg-blue-600 px-4 py-2 text-white rounded-3xl font-medium"
onClick={() => invertNft(tokenId)}
>
Retry {NFTsymbol} Inversion
</button>
</div>
)}
</div>
</div>
</div>
)}
</div>
</div>
</div>
</>
);
};
export default Minter;