-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
325 lines (293 loc) · 9.59 KB
/
index.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import {
useActiveClaimConditionForWallet,
useAddress,
useClaimConditions,
useClaimedNFTSupply,
useClaimerProofs,
useClaimIneligibilityReasons,
useContract,
useContractMetadata,
useUnclaimedNFTSupply,
Web3Button,
} from "@thirdweb-dev/react";
import { BigNumber, utils } from "ethers";
import type { NextPage } from "next";
import { useMemo, useState } from "react";
import styles from "../styles/Theme.module.css";
import { parseIneligibility } from "../utils/parseIneligibility";
// Put Your NFT Drop Contract address from the dashboard here
const myNftDropContractAddress = "0x44ea9eD1D488C41bD80e056df2619F4D917a2d6b";
const Home: NextPage = () => {
const { contract: nftDrop } = useContract(myNftDropContractAddress);
const address = useAddress();
const [quantity, setQuantity] = useState(1);
const { data: contractMetadata } = useContractMetadata(nftDrop);
const claimConditions = useClaimConditions(nftDrop);
const activeClaimCondition = useActiveClaimConditionForWallet(
nftDrop,
address || ""
);
const claimerProofs = useClaimerProofs(nftDrop, address || "");
const claimIneligibilityReasons = useClaimIneligibilityReasons(nftDrop, {
quantity,
walletAddress: address || "",
});
const unclaimedSupply = useUnclaimedNFTSupply(nftDrop);
const claimedSupply = useClaimedNFTSupply(nftDrop);
const numberClaimed = useMemo(() => {
return BigNumber.from(claimedSupply.data || 0).toString();
}, [claimedSupply]);
const numberTotal = useMemo(() => {
return BigNumber.from(claimedSupply.data || 0)
.add(BigNumber.from(unclaimedSupply.data || 0))
.toString();
}, [claimedSupply.data, unclaimedSupply.data]);
const priceToMint = useMemo(() => {
const bnPrice = BigNumber.from(
activeClaimCondition.data?.currencyMetadata.value || 0
);
return `${utils.formatUnits(
bnPrice.mul(quantity).toString(),
activeClaimCondition.data?.currencyMetadata.decimals || 18
)} ${activeClaimCondition.data?.currencyMetadata.symbol}`;
}, [
activeClaimCondition.data?.currencyMetadata.decimals,
activeClaimCondition.data?.currencyMetadata.symbol,
activeClaimCondition.data?.currencyMetadata.value,
quantity,
]);
const maxClaimable = useMemo(() => {
let bnMaxClaimable;
try {
bnMaxClaimable = BigNumber.from(
activeClaimCondition.data?.maxClaimableSupply || 0
);
} catch (e) {
bnMaxClaimable = BigNumber.from(1_000_000);
}
let perTransactionClaimable;
try {
perTransactionClaimable = BigNumber.from(
activeClaimCondition.data?.maxClaimablePerWallet || 0
);
} catch (e) {
perTransactionClaimable = BigNumber.from(1_000_000);
}
if (perTransactionClaimable.lte(bnMaxClaimable)) {
bnMaxClaimable = perTransactionClaimable;
}
const snapshotClaimable = claimerProofs.data?.maxClaimable;
if (snapshotClaimable) {
if (snapshotClaimable === "0") {
// allowed unlimited for the snapshot
bnMaxClaimable = BigNumber.from(1_000_000);
} else {
try {
bnMaxClaimable = BigNumber.from(snapshotClaimable);
} catch (e) {
// fall back to default case
}
}
}
const maxAvailable = BigNumber.from(unclaimedSupply.data || 0);
let max;
if (maxAvailable.lt(bnMaxClaimable)) {
max = maxAvailable;
} else {
max = bnMaxClaimable;
}
if (max.gte(1_000_000)) {
return 1_000_000;
}
return max.toNumber();
}, [
claimerProofs.data?.maxClaimable,
unclaimedSupply.data,
activeClaimCondition.data?.maxClaimableSupply,
activeClaimCondition.data?.maxClaimablePerWallet,
]);
const isSoldOut = useMemo(() => {
try {
return (
(activeClaimCondition.isSuccess &&
BigNumber.from(activeClaimCondition.data?.availableSupply || 0).lte(
0
)) ||
numberClaimed === numberTotal
);
} catch (e) {
return false;
}
}, [
activeClaimCondition.data?.availableSupply,
activeClaimCondition.isSuccess,
numberClaimed,
numberTotal,
]);
console.log("claimIneligibilityReasons", claimIneligibilityReasons.data);
const canClaim = useMemo(() => {
return (
activeClaimCondition.isSuccess &&
claimIneligibilityReasons.isSuccess &&
claimIneligibilityReasons.data?.length === 0 &&
!isSoldOut
);
}, [
activeClaimCondition.isSuccess,
claimIneligibilityReasons.data?.length,
claimIneligibilityReasons.isSuccess,
isSoldOut,
]);
const isLoading = useMemo(() => {
return (
activeClaimCondition.isLoading ||
unclaimedSupply.isLoading ||
claimedSupply.isLoading ||
!nftDrop
);
}, [
activeClaimCondition.isLoading,
nftDrop,
claimedSupply.isLoading,
unclaimedSupply.isLoading,
]);
const buttonLoading = useMemo(
() => isLoading || claimIneligibilityReasons.isLoading,
[claimIneligibilityReasons.isLoading, isLoading]
);
const buttonText = useMemo(() => {
if (isSoldOut) {
return "Sold Out";
}
if (canClaim) {
const pricePerToken = BigNumber.from(
activeClaimCondition.data?.currencyMetadata.value || 0
);
if (pricePerToken.eq(0)) {
return "Mint (Free)";
}
return `Mint (${priceToMint})`;
}
if (claimIneligibilityReasons.data?.length) {
return parseIneligibility(claimIneligibilityReasons.data, quantity);
}
if (buttonLoading) {
return "Checking eligibility...";
}
return "Claiming not available";
}, [
isSoldOut,
canClaim,
claimIneligibilityReasons.data,
buttonLoading,
activeClaimCondition.data?.currencyMetadata.value,
priceToMint,
quantity,
]);
return (
<div className={styles.container}>
<div className={styles.mintInfoContainer}>
{isLoading ? (
<p>Loading...</p>
) : (
<>
<div className={styles.infoSide}>
{/* Title of your NFT Collection */}
<h1>{contractMetadata?.name}</h1>
{/* Description of your NFT Collection */}
<p className={styles.description}>
{contractMetadata?.description}
</p>
</div>
<div className={styles.imageSide}>
{/* Image Preview of NFTs */}
<image
className={styles.image}
src={contractMetadata?.image}
alt={`${contractMetadata?.name} preview image`}
/>
<div>
{/* Powered by thirdweb */}{" "}
<a href="https://goerli.etherscan.io/address/0x44ea9ed1d488c41bd80e056df2619f4d917a2d6b" target="_blank" rel="noreferrer">0x44ea...2d6b</a>
</div>
{/* Amount claimed so far */}
<div className={styles.mintCompletionArea}>
<div className={styles.mintAreaLeft}>
<p>Total Minted</p>
</div>
<div className={styles.mintAreaRight}>
{claimedSupply && unclaimedSupply ? (
<p>
<b>{numberClaimed}</b>
{" / "}
{numberTotal}
</p>
) : (
// Show loading state if we're still loading the supply
<p>Loading...</p>
)}
</div>
</div>
{claimConditions.data?.length === 0 ||
claimConditions.data?.every(
(cc) => cc.maxClaimableSupply === "0"
) ? (
<div>
<h2>
This drop is not ready to be minted yet. (No claim condition
set)
</h2>
</div>
) : (
<>
<p>Quantity</p>
<div className={styles.quantityContainer}>
<button
className={`${styles.quantityControlButton}`}
onClick={() => setQuantity(quantity - 1)}
disabled={quantity <= 1}
>
-
</button>
<h4>{quantity}</h4>
<button
className={`${styles.quantityControlButton}`}
onClick={() => setQuantity(quantity + 1)}
disabled={quantity >= maxClaimable}
>
+
</button>
</div>
<div className={styles.mintContainer}>
{isSoldOut ? (
<div>
<h2>Sold Out</h2>
</div>
) : (
<Web3Button
contractAddress={nftDrop?.getAddress() || ""}
action={(cntr) => cntr.erc721.claim(quantity)}
isDisabled={!canClaim || buttonLoading}
onError={(err) => {
console.error(err);
alert("Error claiming NFTs");
}}
onSuccess={() => {
setQuantity(1);
alert("Successfully claimed NFTs");
}}
>
{buttonLoading ? "Loading..." : buttonText}
</Web3Button>
)}
</div>
</>
)}
</div>
</>
)}
</div>
</div>
);
};
export default Home;