/
drop.ts
816 lines (799 loc) · 24.2 KB
/
drop.ts
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
import {
RequiredParam,
requiredParamInvariant,
} from "../../../core/query-utils/required-param";
import { useSDKChainId } from "../useSDK";
import {
ClaimNFTParams,
ClaimNFTReturnType,
DelayedRevealLazyMintInput,
DropContract,
RevealLazyMintInput,
getErcs,
RevealableContract,
} from "../../types";
import {
cacheKeys,
invalidateContractAndBalances,
} from "../../utils/cache-keys";
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
import {
useMutation,
UseMutationResult,
useQueryClient,
UseQueryResult,
} from "@tanstack/react-query";
import type {
NFTMetadata,
NFTMetadataInput,
QueryAllParams,
UploadProgressEvent,
} from "@thirdweb-dev/sdk";
import type { NFTDrop } from "@thirdweb-dev/sdk";
import type { SignatureDrop } from "@thirdweb-dev/sdk";
import type { SmartContract } from "@thirdweb-dev/sdk";
import type { providers } from "ethers";
import invariant from "tiny-invariant";
/** **********************/
/** READ HOOKS **/
/** **********************/
/**
* Hook for fetching information about all NFTs that haven't been claimed yet from an NFT Drop contract.
*
* Available to use on contracts that extends the ERC721 spec
*
* @example
*
* ```jsx
* import { useUnclaimedNFTs, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { data, isLoading, error } = useUnclaimedNFTs(contract);
* }
* ```
*
* @param contract - Instance of a contract that extends the ERC721 spec (NFT drop, Signature Drop, or any custom contract that extends the ERC721 spec)
*
* @param queryParams -
* query params to pass to the query for the sake of pagination
* By default, the hook returns the first 100 unclaimed NFTs from the contract.
*
* Paginate the results by providing a `queryParams` object as the second argument.
*
* ```jsx
* import { useUnclaimedNFTs, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { data, isLoading, error } = useUnclaimedNFTs(
* contract,
* {
* count: 10, // Limit the number of results
* start: 0, // Start from the nth result (useful for pagination)
* },
* );
* }
* ```
*
* @returns
* The hook's `data` property, once loaded, contains an array of `NFT` objects.
*
* @twfeature ERC721LazyMintable
* @nftDrop
*/
export function useUnclaimedNFTs(
contract: RequiredParam<NFTDrop>,
queryParams?: QueryAllParams,
): UseQueryResult<NFTMetadata[]> {
const contractAddress = contract?.getAddress();
return useQueryWithNetwork(
cacheKeys.contract.nft.drop.getAllUnclaimed(contractAddress, queryParams),
() => {
requiredParamInvariant(contract, "No Contract instance provided");
// TODO make this work for custom contracts (needs ABI change)
invariant(
contract.getAllUnclaimed,
"Contract instance does not support getAllUnclaimed",
);
const data: Promise<NFTMetadata[]> =
contract.getAllUnclaimed(queryParams);
return data;
},
{ enabled: !!contract },
);
}
/**
* Hook for fetching all claimed NFTs from a given NFT Drop contract.
*
* Available to use on contracts that implement [`ERC721Claimable`](https://portal.thirdweb.com/contracts/build/extensions/erc-721/ERC721Claimable),
* such as the [NFT Drop](https://thirdweb.com/thirdweb.eth/DropERC721).
*
* @example
*
* ```jsx
* import { useClaimedNFTs, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress, "nft-drop");
* const { data: nfts, isLoading, error } = useClaimedNFTs(contract);
* }
* ```
*
* @param contract - Instance of a contract that extends the ERC721 spec (NFT drop, Signature Drop, or any custom contract that extends the ERC721 spec)
*
* @param queryParams -
* By default, the hook will return the first `100` claimed NFTs
*
* You can use the `queryParams` argument to paginate the NFTs that are returned.
*
* ```jsx
* import { useClaimedNFTs, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress, "nft-drop");
* const { data, isLoading, error } = useClaimedNFTs(
* contract,
* {
* // For example, to only return the first 50 claimed NFTs in the collection
* // in order of token ID
* count: 50,
* start: 0,
* },
* );
* }
* ```
*
* @returns Query Result object that includes an array of NFTs that are claimed in the `data` property
* @twfeature ERC721LazyMintable
* @nftDrop
*/
export function useClaimedNFTs(
contract: RequiredParam<NFTDrop>,
queryParams?: QueryAllParams,
) {
const contractAddress = contract?.getAddress();
return useQueryWithNetwork(
cacheKeys.contract.nft.drop.getAllClaimed(contractAddress, queryParams),
() => {
requiredParamInvariant(contract, "No Contract instance provided");
// TODO make this work for custom contracts (needs ABI change)
invariant(
contract.getAllClaimed,
"Contract instance does not support getAllClaimed",
);
return contract.getAllClaimed(queryParams);
},
{ enabled: !!contract },
);
}
/**
* Hook for fetching the number of unclaimed NFTs from an NFT/Edition Drop contract.
*
* Unclaimed NFTs are tokens that were lazy-minted but have not yet been claimed by a user.
*
* Available to use on contracts that implement the [`LazyMint`](https://portal.thirdweb.com/contracts/build/extensions/general/LazyMint) extension;
* such as the [NFT Drop](https://thirdweb.com/thirdweb.eth/DropERC721) contract.
*
* @example
*
* ```jsx
* import { useUnclaimedNFTSupply, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress, "nftDrop");
* const { data, isLoading, error } = useUnclaimedNFTSupply(contract);
* }
* ```
*
* @param contract - Instance of a contract that extends the ERC721 spec (NFT drop, Signature Drop, or any custom contract that extends the ERC721 spec)
*
* @returns
* The hook's `data` property, once loaded, contains a `BigNumber` representing the number of unclaimed NFTs.
*
*
* @twfeature ERC721LazyMintable
*
* @nftDrop
*/
export function useUnclaimedNFTSupply(
contract: RequiredParam<NFTDrop | SignatureDrop | SmartContract | null>,
) {
const contractAddress = contract?.getAddress();
const { erc721 } = getErcs(contract);
return useQueryWithNetwork(
cacheKeys.contract.nft.drop.totalUnclaimedSupply(contractAddress),
() => {
requiredParamInvariant(contract, "No Contract instance provided");
if (erc721) {
invariant(erc721, "No ERC721 Contract instance provided");
return erc721.totalUnclaimedSupply();
}
invariant(false, "Contract is not an instance of ERC721");
},
{ enabled: !!erc721 },
);
}
/**
* Hook for retrieving the total supply of NFTs claimed from an NFT Drop contract.
*
* Available to use on contracts that implement [`ERC721Claimable`](https://portal.thirdweb.com/contracts/build/extensions/erc-721/ERC721Claimable).
*
* @example
*
* ```jsx
* import { useClaimedNFTSupply, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { data, isLoading } = useClaimedNFTSupply(contract);
* }
* ```
*
* @param contract - Instance of a contract that extends the ERC721 spec (NFT drop, Signature Drop, or any custom contract that extends the ERC721 spec)
*
* @returns
* The hook's `data` property, once loaded, is a `BigNumber` representing the total supply of NFTs claimed from the NFT drop contract so far.
*
* @twfeature ERC721LazyMintable
* @nftDrop
*/
export function useClaimedNFTSupply(
contract: RequiredParam<NFTDrop | SignatureDrop | SmartContract | null>,
) {
const contractAddress = contract?.getAddress();
const { erc721 } = getErcs(contract);
return useQueryWithNetwork(
cacheKeys.contract.nft.drop.totalClaimedSupply(contractAddress),
() => {
requiredParamInvariant(contract, "No Contract instance provided");
if (erc721) {
return erc721.totalClaimedSupply();
}
invariant(false, "Contract is not an instance of ERC721");
},
{ enabled: !!erc721 },
);
}
/**
* Hook for fetching batches of lazy-minted NFTs that were set to be revealed at a later date, but have not yet been revealed.
*
* Available to use on contracts that implement the [ERC721Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-721/ERC721Revealable)
* or [ERC1155Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-1155/ERC1155Revealable) interfaces,
* such as the [NFT Drop](https://thirdweb.com/thirdweb.eth/DropERC721)
* and [Edition Drop](https://thirdweb.com/thirdweb.eth/DropERC1155) smart contracts.
*
* @example
* ```tsx
* import { useBatchesToReveal, useContract } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { data: batches, isLoading, error } = useBatchesToReveal(contract);
* }
* ```
*
* @param contract - Instance of a `RevealableContract`
*
* @returns The hook's `data` property, once loaded, contains an array of batches that need to be revealed.
*
* Each batch is an object with the following properties:
*
* ```ts
* {
* batchId: BigNumber;
* batchUri: string;
* placeholderMetadata: NFTMetadata;
* }
* ```
*
* @twfeature ERC721Revealable | ERC1155Revealable
* @delayedReveal
*/
export function useBatchesToReveal<TContract extends RevealableContract>(
contract: RequiredParam<TContract>,
) {
const contractAddress = contract?.getAddress();
const { erc721, erc1155 } = getErcs(contract);
return useQueryWithNetwork(
cacheKeys.contract.nft.drop.revealer.getBatchesToReveal(contractAddress),
() => {
if (erc721) {
return erc721.revealer.getBatchesToReveal();
}
if (erc1155) {
return erc1155.revealer.getBatchesToReveal();
}
invariant(false, "Contract instance does not support getBatchesToReveal");
},
{ enabled: !!erc721 || !!erc1155 },
);
}
/** **********************/
/** WRITE HOOKS **/
/** **********************/
/**
* Hook for claiming an NFT from a smart contract.
*
* Available to use on smart contracts that implement a `Claimable` interface, and follow either the `ERC721`or `ERC1155` standard.
*
* @example
* ```jsx
* import { useContract, useClaimNFT, Web3Button } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { mutateAsync: claimNft, isLoading, error } = useClaimNFT(contract);
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() =>
* claimNft({
* to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
* quantity: 1,
* })
* }
* >
* Claim NFT
* </Web3Button>
* );
* }
* ```
*
* @param contract - Instance of a `DropContract`
*
* @returns A mutation object to claim a NFT to the wallet specified in the params
*
* ```ts
* const { mutateAsync, isLoading, error } = useClaimNFT(contract);
* ```
*
* ### options
*
* The mutation function takes an object as argument with below properties:
*
* #### to
*
* The wallet address to mint the NFT(s) to.
*
* Use the `useAddress` hook to get the currently connected wallet address.
*
* #### quantity
*
* The number of NFTs you wish to claim.
*
* - With ERC721 contracts, this represents the number of unique tokens you wish to claim.
* - With ERC1155 contracts, this represents the quantity of the specific `tokenId` you wish to claim.
*
* #### tokenId
*
* For ERC1155 contracts, you must specify a specific `tokenId` to claim.
*
* #### options (optional)
*
* Customizable `ClaimOptions` object to override the default behaviour of the hook.
*
* There are three options available:
*
* - `checkERC20Allowance` - Whether to check the ERC20 allowance of the sender, defaults to true.
* - `currencyAddress` - The currency to pay for each token claimed, defaults to `NATIVE_TOKEN_ADDRESS` for native currency.
* - `pricePerToken` - The price to pay for each token claimed. Not relevant when using claim conditions.
*
* ```jsx
* import { useContract, useClaimNFT, Web3Button } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { mutate: claimNft, isLoading, error } = useClaimNFT(contract);
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() =>
* claimNft({
* to: "{{wallet_address}}",
* quantity: 1,
* options: {
* checkERC20Allowance: true,
* currencyAddress: "{{erc20_address}}",
* pricePerToken: 0,
* },
* })
* }
* >
* Claim NFT
* </Web3Button>
* );
* }
* ```
*
* @twfeature ERC721Claimable | ERC1155Claimable | ERC721ClaimPhasesV2 | ERC721ClaimPhasesV1 | ERC721ClaimConditionsV2 | ERC721ClaimConditionsV1 | ERC1155ClaimPhasesV2 | ERC1155ClaimPhasesV1 | ERC1155ClaimConditionsV2 | ERC1155ClaimConditionsV1
* @nftDrop
*/
export function useClaimNFT<TContract extends DropContract>(
contract: RequiredParam<TContract>,
) {
const activeChainId = useSDKChainId();
const contractAddress = contract?.getAddress();
const queryClient = useQueryClient();
const { erc721, erc1155 } = getErcs(contract);
return useMutation(
async (data: ClaimNFTParams) => {
requiredParamInvariant(contract, "contract is undefined");
if (erc1155) {
requiredParamInvariant(data.tokenId, "tokenId not provided");
if (!data.to) {
return (await erc1155.claim(
data.tokenId,
data.quantity,
data.options,
)) as ClaimNFTReturnType;
}
return (await erc1155.claimTo(
data.to,
data.tokenId,
data.quantity,
data.options,
)) as ClaimNFTReturnType;
}
if (erc721) {
if (!data.to) {
return (await erc721.claim(
data.quantity,
data.options,
)) as ClaimNFTReturnType;
}
return (await erc721.claimTo(
data.to,
data.quantity,
data.options,
)) as ClaimNFTReturnType;
}
invariant(false, "contract is not an Erc721 or Erc1155");
},
{
onSettled: () =>
invalidateContractAndBalances(
queryClient,
contractAddress,
activeChainId,
),
},
);
}
/**
* Hook for lazy minting a batch of NFTs on a drop contract.
*
* Available to use on smart contracts that implement the "Drop" extension, and
* follow either the `ERC721` or `ERC1155` standard.
*
* @example
*
* Provide your drop contract (ERC721 or ERC1155) as the argument to the hook, and an array
* of metadata objects to lazy-mint.
*
* ```jsx
* import { useContract, useLazyMint, Web3Button } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { mutateAsync: lazyMint, isLoading, error } = useLazyMint(contract);
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() =>
* lazyMint({
* // Metadata of the NFTs to upload
* metadatas: [
* {
* name: "My NFT",
* description: "An example NFT",
* image: "{{image_url}}",
* },
* ],
* })
* }
* >
* Lazy Mint NFTs
* </Web3Button>
* );
* }
* ```
*
* @param contract - Instance of a `NFTContract` with the drop extension
*
* @param onProgress - Optional callback that will be called with the progress of the upload
*
* @returns A mutation object to lazy mint a batch of NFTs
*
* ```ts
* const { mutateAsync, isLoading, error } = useLazyMint(contract);
* ```
*
* ### options
*
* The mutation function takes an object as argument with below properties:
*
* #### metadatas
* An array of objects containing the metadata of the NFTs to lazy mint.
*
* Your metadata objects must follow the [Metadata standards](https://docs.opensea.io/docs/metadata-standards#metadata-structure).
*
* ```jsx
* import { useContract, useLazyMint, Web3Button } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const { mutateAsync: lazyMint, isLoading, error } = useLazyMint(contract);
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() =>
* lazyMint({
* // Metadata of the NFTs to upload
* metadatas: [
* {
* name: "My NFT",
* description: "An example NFT",
* image: "{{image_url}}",
* },
* ],
* })
* }
* >
* Lazy Mint NFTs
* </Web3Button>
* );
* }
* ```
*
* @twfeature ERC721LazyMintable | ERC1155LazyMintable
* @nftDrop
*/
export function useLazyMint<TContract extends DropContract>(
contract: RequiredParam<TContract>,
onProgress?: (progress: UploadProgressEvent) => void,
): UseMutationResult<any, unknown, any, unknown> {
const activeChainId = useSDKChainId();
const contractAddress = contract?.getAddress();
const queryClient = useQueryClient();
const { erc721, erc1155 } = getErcs(contract);
return useMutation(
async (data: { metadatas: NFTMetadataInput[] }) => {
requiredParamInvariant(contract, "contract is undefined");
let options;
if (onProgress) {
options = {
onProgress,
};
}
if (erc721) {
return erc721.lazyMint(data.metadatas, options);
}
if (erc1155) {
return erc1155.lazyMint(data.metadatas, options);
}
invariant(false, "contract is not an Erc721 or Erc1155");
},
{
onSettled: () =>
invalidateContractAndBalances(
queryClient,
contractAddress,
activeChainId,
),
},
);
}
/**
* Hook to lazy-mint a batch of NFTs with [delayed reveal](https://portal.thirdweb.com/glossary/delayed-reveal);
* allowing the owner to set placeholder metadata and reveal the metadata of the NFTs at a later time.
*
* Available to use on contracts that implement the
* [ERC721Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-721/ERC721Revealable)
* or [ERC1155Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-1155/ERC1155Revealable)
* interfaces.
*
* @example
* ```tsx
* import {
* useDelayedRevealLazyMint,
* useContract,
* Web3Button,
* } from "@thirdweb-dev/react";
*
* function App() {
* const { contract } = useContract(contractAddress);
* const {
* mutateAsync: mintNft,
* isLoading,
* error,
* } = useDelayedRevealLazyMint(contract);
*
* const nftData = {
* placeholder: {
* name: "My NFT",
* description: "This is my NFT",
* image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
* },
* metadatas: [
* {
* name: "My NFT",
* description: "This is my NFT",
* image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
* },
* ],
* password: "{{password}}", // Password to be used for encryption
* };
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() => mintNft(nftData)}
* >
* Mint NFTs
* </Web3Button>
* );
* }
* ```
*
* @param contract - Instance of a {@link DropContract}
* @param onProgress - an optional callback that will be called with the progress of the upload
* @returns Mutation object to lazy mint a batch of NFTs
*
* ```ts
* const { mutateAsync, isLoading, error } = useDelayedRevealLazyMint(contract);
* ```
*
* ### options
* The mutation function takes an object as argument with below properties:
*
* #### metadatas
* An array of metadata objects, representing the metadata of the NFTs to be lazy-minted. Each metadata object must conform to the [standard metadata properties](https://docs.opensea.io/docs/metadata-standards).
*
* #### password
* The password used to encrypt the metadatas.
*
* __The password CANNOT be recovered once it is set. If you lose the password, you will not be able to reveal the metadata.__
*
* @twfeature ERC721Revealable | ERC1155Revealable
* @delayedReveal
*/
export function useDelayedRevealLazyMint<TContract extends RevealableContract>(
contract: RequiredParam<TContract>,
onProgress?: (progress: UploadProgressEvent) => void,
) {
const activeChainId = useSDKChainId();
const contractAddress = contract?.getAddress();
const queryClient = useQueryClient();
return useMutation(
async (data: DelayedRevealLazyMintInput) => {
requiredParamInvariant(contract, "contract is undefined");
let options;
if (onProgress) {
options = {
onProgress,
};
}
const { erc721, erc1155 } = getErcs(contract);
if (erc721) {
return await erc721.revealer.createDelayedRevealBatch(
data.placeholder,
data.metadatas,
data.password,
options,
);
}
if (erc1155) {
return await erc1155.revealer.createDelayedRevealBatch(
data.placeholder,
data.metadatas,
data.password,
options,
);
}
invariant(false, "contract is not an Erc721 or Erc1155");
},
{
onSettled: () =>
invalidateContractAndBalances(
queryClient,
contractAddress,
activeChainId,
),
},
);
}
/**
* Hook for revealing a batch of delayed reveal NFTs using [delayed reveal](https://portal.thirdweb.com/glossary/delayed-reveal).
*
* Available to use on contracts that implement the
* [ERC721Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-721/ERC721Revealable)
* or [ERC1155Revealable](https://portal.thirdweb.com/contracts/build/extensions/erc-1155/ERC1155Revealable)
* interfaces.
*
* ```jsx
* import { useRevealLazyMint } from "@thirdweb-dev/react";
*
* const { mutateAsync, isLoading, error } = useRevealLazyMint(contract);
* ```
*
* @example
* ```tsx
* import {
* useContract,
* useRevealLazyMint,
* Web3Button,
* } from "@thirdweb-dev/react";
*
* function App() {
* // Contract must be an ERC-721 or ERC-1155 contract that implements the ERC721Revealable or ERC1155Revealable interface
* const { contract } = useContract(contractAddress);
* const {
* mutateAsync: revealLazyMint,
* isLoading,
* error,
* } = useRevealLazyMint(contract);
*
* return (
* <Web3Button
* contractAddress={contractAddress}
* action={() =>
* revealLazyMint({
* batchId: "{{batch_id}}", // ID of the batch to reveal (use useBatchesToReveal to get the batch IDs)
* password: "{{password}}", // Password to reveal the batch
* })
* }
* >
* Reveal Lazy Mint
* </Web3Button>
* );
* }
* ```
*
* @param contract - Instance of a `RevealableContract`
* @twfeature ERC721Revealable | ERC1155Revealable
* @delayedReveal
*/
export function useRevealLazyMint<TContract extends RevealableContract>(
contract: RequiredParam<TContract>,
): UseMutationResult<
Omit<
{
receipt: providers.TransactionReceipt;
data: () => Promise<unknown>;
},
"data"
>,
unknown,
RevealLazyMintInput,
unknown
> {
const activeChainId = useSDKChainId();
const contractAddress = contract?.getAddress();
const queryClient = useQueryClient();
return useMutation(
async (data: RevealLazyMintInput) => {
requiredParamInvariant(contract, "contract is undefined");
const { erc721, erc1155 } = getErcs(contract);
if (erc721) {
return await erc721.revealer.reveal(data.batchId, data.password);
}
if (erc1155) {
return await erc1155.revealer.reveal(data.batchId, data.password);
}
invariant(false, "contract is not an Erc721 or Erc1155");
},
{
onSettled: () =>
invalidateContractAndBalances(
queryClient,
contractAddress,
activeChainId,
),
},
);
}