This repository was archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy patherc1155.go
More file actions
868 lines (799 loc) · 21.8 KB
/
Copy patherc1155.go
File metadata and controls
868 lines (799 loc) · 21.8 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
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
package thirdweb
import (
"context"
"fmt"
"math/big"
"sort"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/mitchellh/mapstructure"
"github.com/thirdweb-dev/go-sdk/v2/abi"
)
// This interface is currently support by the Edition and Edition Drop contracts.
// You can access all of its functions through an Edition or Edition Drop contract instance.
type ERC1155 struct {
token *abi.TokenERC1155
drop *abi.DropERC1155
helper *contractHelper
storage storage
ClaimConditions *EditionDropClaimConditions
}
type EditionResult struct {
nft *EditionMetadata
err error
}
func newERC1155(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*ERC1155, error) {
token, err := abi.NewTokenERC1155(address, provider)
if err != nil {
return nil, err
}
drop, err := abi.NewDropERC1155(address, provider)
if err != nil {
return nil, err
}
helper, err := newContractHelper(address, provider, privateKey)
if err != nil {
return nil, err
}
claimConditions, err := newEditionDropClaimConditions(address, provider, helper, storage)
if err != nil {
return nil, err
}
return &ERC1155{
token,
drop,
helper,
storage,
claimConditions,
}, nil
}
// Get an NFT
//
// @extension: ERC1155
//
// tokenId: token ID of the token to get the metadata for
//
// returns: the metadata for the NFT and its supply
//
// Example
//
// nft, err := contract.Get(context.Background(), 0)
// supply := nft.Supply
// name := nft.Metadata.Name
func (erc1155 *ERC1155) Get(ctx context.Context, tokenId int) (*EditionMetadata, error) {
supply := 0
if totalSupply, err := erc1155.token.TotalSupply(&bind.CallOpts{Context: ctx}, big.NewInt(int64(tokenId))); err == nil {
supply = int(totalSupply.Int64())
}
if metadata, err := erc1155.getTokenMetadata(ctx, tokenId); err != nil {
return nil, err
} else {
metadataOwner := &EditionMetadata{
Metadata: metadata,
Supply: supply,
}
return metadataOwner, nil
}
}
// Get all NFTs
//
// @extension: ERC1155
//
// returns: the metadatas and supplies of all the NFTs on this contract
//
// Example
//
// nfts, err := contract.GetAll(context.Background())
// supplyOne := nfts[0].Supply
// nameOne := nfts[0].Metadata.Name
func (erc1155 *ERC1155) GetAll(ctx context.Context) ([]*EditionMetadata, error) {
if totalCount, err := erc1155.GetTotalCount(ctx); err != nil {
return nil, err
} else {
tokenIds := []*big.Int{}
for i := 0; i < totalCount; i++ {
tokenIds = append(tokenIds, big.NewInt(int64(i)))
}
return fetchEditionsByTokenId(ctx, erc1155, tokenIds)
}
}
// Get the total number of NFTs
//
// @extension: ERC1155Enumerable
//
// returns: the total number of NFTs on this contract
//
// Example
//
// totalCount, err := contract.GetTotalCount(context.Background())
func (erc1155 *ERC1155) GetTotalCount(ctx context.Context) (int, error) {
count, err := erc1155.token.NextTokenIdToMint(&bind.CallOpts{Context: ctx})
if err != nil {
return 0, err
}
return int(count.Int64()), nil
}
// Get owned NFTs
//
// @extension: ERC1155Enumerable
//
// address: the address of the owner of the NFTs
//
// returns: the metadatas and supplies of all the NFTs owned by the address
//
// Example
//
// owner := "{{wallet_address}}"
// nfts, err := contract.GetOwned(context.Background(), owner)
// name := nfts[0].Metadata.Name
func (erc1155 *ERC1155) GetOwned(ctx context.Context, address string) ([]*EditionMetadataOwner, error) {
if address == "" {
address = erc1155.helper.GetSignerAddress().String()
}
maxId, err := erc1155.token.NextTokenIdToMint(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
owners := []common.Address{}
ids := []*big.Int{}
for i := 0; i < int(maxId.Int64()); i++ {
owners = append(owners, common.HexToAddress(address))
ids = append(ids, big.NewInt(int64(i)))
}
balances, err := erc1155.token.BalanceOfBatch(&bind.CallOpts{Context: ctx}, owners, ids)
if err != nil {
return nil, err
}
metadataOwners := []*EditionMetadataOwner{}
metadatas, err := fetchEditionsByTokenId(ctx, erc1155, ids)
if err != nil {
return nil, err
}
for index, balance := range balances {
metadata := metadatas[index]
if err == nil {
metadataOwner := &EditionMetadataOwner{
Metadata: metadata.Metadata,
Supply: metadata.Supply,
Owner: address,
QuantityOwned: int(balance.Int64()),
}
metadataOwners = append(metadataOwners, metadataOwner)
}
}
return metadataOwners, nil
}
// Get the total supply of an NFT
//
// @extension: ERC1155
//
// tokenId: the token ID to check the total supply of
//
// returns: the supply of NFTs on the specified token ID
//
// Example
//
// tokenId := 0
//
// totalSupply, err := contract.TotalSupply(context.Background, tokenId)
func (erc1155 *ERC1155) TotalSupply(ctx context.Context, tokenId int) (int, error) {
supply, err := erc1155.token.TotalSupply(&bind.CallOpts{Context: ctx}, big.NewInt(int64(tokenId)))
if err != nil {
return 0, err
}
return int(supply.Int64()), nil
}
// Get NFT balance
//
// @extension: ERC1155
//
// tokenId: the token ID of a specific token to check the balance of
//
// returns: the number of NFTs of the specified token ID owned by the connected wallet
func (erc1155 *ERC1155) Balance(ctx context.Context, tokenId int) (int, error) {
address := erc1155.helper.GetSignerAddress().String()
return erc1155.BalanceOf(ctx, address, tokenId)
}
// Get NFT balance of a specific wallet
//
// @extension: ERC1155
//
// address: the address of the wallet to get the NFT balance of
//
// returns: the number of NFTs of the specified token ID owned by the specified wallet
//
// Example
//
// address := "{{wallet_address}}"
// tokenId := 0
// balance, err := contract.BalanceOf(context.Background(), address, tokenId)
func (erc1155 *ERC1155) BalanceOf(ctx context.Context, address string, tokenId int) (int, error) {
balance, err := erc1155.token.BalanceOf(&bind.CallOpts{Context: ctx}, common.HexToAddress(address), big.NewInt(int64(tokenId)))
if err != nil {
return 0, err
}
return int(balance.Int64()), nil
}
// Check NFT approval
//
// @extension: ERC1155
//
// address: the address whose assets are to be checked
//
// operator: the address of the operator to check
//
// returns: true if the operator is approved for all operations of the assets, otherwise false
//
// Example
//
// owner := "{{wallet_address}}"
// operator := "0x..."
//
// isApproved, err := contract.IsApproved(context.Background, owner, operator)
func (erc1155 *ERC1155) IsApproved(ctx context.Context, owner string, operator string) (bool, error) {
return erc1155.token.IsApprovedForAll(&bind.CallOpts{Context: ctx}, common.HexToAddress(owner), common.HexToAddress(operator))
}
// Transfer NFTs
//
// @extension: ERC1155
//
// to: wallet address to transfer the tokens to
//
// tokenId: the token ID of the NFT to transfer
//
// amount: number of NFTs of the token ID to transfer
//
// returns: the transaction of the NFT transfer
//
// Example
//
// to := "0x..."
// tokenId := 0
// amount := 1
//
// tx, err := contract.Transfer(context.Background(), to, tokenId, amount)
func (erc1155 *ERC1155) Transfer(ctx context.Context, to string, tokenId int, amount int) (*types.Transaction, error) {
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
if tx, err := erc1155.token.SafeTransferFrom(
txOpts,
erc1155.helper.GetSignerAddress(),
common.HexToAddress(to),
big.NewInt(int64(tokenId)),
big.NewInt(int64(amount)),
[]byte{},
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}
// Burn NFTs
//
// @extension: ERC1155Burnable
//
// tokenId: tokenID of the token to burn
//
// amount: number of NFTs of the token ID to burn
//
// returns: the transaction receipt of the burn
//
// Example
//
// tokenId := 0
// amount := 1
// tx, err := contract.Burn(context.Background(), tokenId, amount)
func (erc1155 *ERC1155) Burn(ctx context.Context, tokenId int, amount int) (*types.Transaction, error) {
address := erc1155.helper.GetSignerAddress()
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
if tx, err := erc1155.token.Burn(
txOpts,
address,
big.NewInt(int64(tokenId)),
big.NewInt(int64(amount)),
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}
// Set approval for all NFTs
//
// @extension: ERC1155
//
// address: the address whose assets are to be approved
//
// operator: the address of the operator to set the approval for
//
// approved: true if the operator is approved for all operations of the assets, otherwise false
//
// returns: the transaction receipt of the approval
//
// Example
//
// operator := "{{wallet_address}}"
// approved := true
//
// tx, err := contract.SetApprovalForAll(context.Background(), operator, approved)
func (erc1155 *ERC1155) SetApprovalForAll(ctx context.Context, operator string, approved bool) (*types.Transaction, error) {
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
if tx, err := erc1155.token.SetApprovalForAll(
txOpts,
common.HexToAddress(operator),
approved,
); err != nil {
return nil, err
} else {
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
}
// Mint an NFT
//
// @extension: ERC1155Mintable
//
// metadataWithSupply: nft metadata with supply of the NFT to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// image, err := os.Open("path/to/image.jpg")
// defer image.Close()
//
// metadataWithSupply := &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image,
// },
// Supply: 100,
// }
//
// tx, err := contract.Mint(context.Background(), metadataWithSupply)
func (erc1155 *ERC1155) Mint(ctx context.Context, metadataWithSupply *EditionMetadataInput) (*types.Transaction, error) {
address := erc1155.helper.GetSignerAddress().String()
return erc1155.MintTo(ctx, address, metadataWithSupply)
}
// Mint an NFT to a specific wallet
//
// @extension: ERC1155Mintable
//
// address: the wallet address to mint the NFT to
//
// metadataWithSupply: nft metadata with supply of the NFT to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// image, err := os.Open("path/to/image.jpg")
// defer image.Close()
//
// metadataWithSupply := &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image,
// },
// Supply: 100,
// }
//
// tx, err := contract.MintTo(context.Background(), "{{wallet_address}}", metadataWithSupply)
func (erc1155 *ERC1155) MintTo(ctx context.Context, address string, metadataWithSupply *EditionMetadataInput) (*types.Transaction, error) {
uri, err := uploadOrExtractUri(ctx, metadataWithSupply.Metadata, erc1155.storage)
if err != nil {
return nil, err
}
MaxUint256 := new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1)
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := erc1155.token.MintTo(
txOpts,
common.HexToAddress(address),
MaxUint256,
uri,
big.NewInt(int64(metadataWithSupply.Supply)),
)
if err != nil {
return nil, err
}
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
// Mint additionaly supply of an NFT
//
// @extension: ERC1155Mintable
//
// tokenId: token ID to mint additional supply of
//
// additionalSupply: additional supply to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// tokenId := 0
// additionalSupply := 100
//
// tx, err := contract.MintAdditionalSupply(context.Background(), tokenId, additionalSupply)
func (erc1155 *ERC1155) MintAdditionalSupply(ctx context.Context, tokenId int, additionalSupply int) (*types.Transaction, error) {
address := erc1155.helper.GetSignerAddress().String()
return erc1155.MintAdditionalSupplyTo(ctx, address, tokenId, additionalSupply)
}
// Mint additional supply of an NFT to a specific wallet
//
// @extension: ERC1155Mintable
//
// to: address of the wallet to mint NFTs to
//
// tokenId: token Id to mint additional supply of
//
// additionalySupply: additional supply to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// to := "{{wallet_address}}"
// tokenId := 0
// additionalSupply := 100
//
// tx, err := contract.MintAdditionalSupplyTo(context.Background(), to, tokenId, additionalSupply)
func (erc1155 *ERC1155) MintAdditionalSupplyTo(ctx context.Context, to string, tokenId int, additionalSupply int) (*types.Transaction, error) {
metadata, err := erc1155.getTokenMetadata(ctx, tokenId)
if err != nil {
return nil, err
}
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := erc1155.token.MintTo(
txOpts,
common.HexToAddress(to),
big.NewInt(int64(tokenId)),
metadata.Uri,
big.NewInt(int64(additionalSupply)),
)
if err != nil {
return nil, err
}
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
// Mint many NFTs
//
// @extension: ERC1155BatchMintable
//
// metadatasWithSupply: list of NFT metadatas with supplies to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// metadatasWithSupply := []*thirdweb.EditionMetadataInput{
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// },
// Supply: 100,
// },
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// },
// Supply: 100,
// },
// }
//
// tx, err := contract.MintBatch(context.Background(), metadatasWithSupply)
func (erc1155 *ERC1155) MintBatch(ctx context.Context, metadatasWithSupply []*EditionMetadataInput) (*types.Transaction, error) {
return erc1155.MintBatchTo(ctx, erc1155.helper.GetSignerAddress().String(), metadatasWithSupply)
}
// Mint many NFTs to a specific wallet
//
// @extension: ERC1155BatchMintable
//
// to: address of the wallet to mint NFTs to
//
// metadatasWithSupply: list of NFT metadatas with supplies to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// metadatasWithSupply := []*thirdweb.EditionMetadataInput{
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// },
// Supply: 100,
// },
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// },
// Supply: 100,
// },
// }
//
// tx, err := contract.MintBatchTo(context.Background(), "{{wallet_address}}", metadatasWithSupply)
func (erc1155 *ERC1155) MintBatchTo(ctx context.Context, to string, metadatasWithSupply []*EditionMetadataInput) (*types.Transaction, error) {
metadatas := []*NFTMetadataInput{}
for _, metadataWithSupply := range metadatasWithSupply {
metadatas = append(metadatas, metadataWithSupply.Metadata)
}
supplies := []int{}
for _, metadataWithSupply := range metadatasWithSupply {
supplies = append(supplies, metadataWithSupply.Supply)
}
uris, err := uploadOrExtractUris(ctx, metadatas, erc1155.storage)
if err != nil {
return nil, err
}
encoded := [][]byte{}
MaxUint256 := new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1)
for index, uri := range uris {
txOpts, err := erc1155.helper.getEncodedTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := erc1155.token.MintTo(
txOpts,
common.HexToAddress(to),
MaxUint256,
uri,
big.NewInt(int64(supplies[index])),
)
if err != nil {
return nil, err
}
encoded = append(encoded, tx.Data())
}
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := erc1155.token.Multicall(txOpts, encoded)
if err != nil {
return nil, err
}
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
// Lazy mint NFTs
//
// @extension: ERC1155LazyMintableV2
//
// metadatas: a list of the metadatas of the NFTs to create
//
// returns: the transaction receipt of the batch creation
//
// Example
//
// image0, err := os.Open("path/to/image/0.jpg")
// defer image0.Close()
//
// image1, err := os.Open("path/to/image/1.jpg")
// defer image1.Close()
//
// metadatasWithSupply := []*thirdweb.EditionMetadataInput{
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image0,
// },
// Supply: 100,
// },
// &thirdweb.EditionMetadataInput{
// Metadata: &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image1,
// },
// Supply: 100,
// },
// }
//
// tx, err := contract.CreateBatch(context.Background(), metadatasWithSupply)
func (erc1155 *ERC1155) CreateBatch(ctx context.Context, metadatas []*NFTMetadataInput) (*types.Transaction, error) {
startNumber, err := erc1155.drop.NextTokenIdToMint(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
fileStartNumber := int(startNumber.Int64())
contractAddress := erc1155.helper.getAddress().String()
signerAddress := erc1155.helper.GetSignerAddress().String()
data := []interface{}{}
for _, metadata := range metadatas {
data = append(data, metadata)
}
dataToUpload := []map[string]interface{}{}
if err := mapstructure.Decode(data, &dataToUpload); err != nil {
return nil, err
}
batch, err := erc1155.storage.UploadBatch(
ctx,
dataToUpload,
fileStartNumber,
contractAddress,
signerAddress,
)
if err != nil {
return nil, err
}
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := erc1155.drop.LazyMint(
txOpts,
big.NewInt(int64(len(batch.uris))),
batch.baseUri,
[]byte{},
)
if err != nil {
return nil, err
}
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
// Claim an NFT
//
// @extension: ERC1155ClaimCustom | ERC1155ClaimPhasesV2 | ERC1155ClaimConditionsV2
//
// tokenId: the token ID of the NFT to claim
//
// quantity: the number of NFTs to claim
//
// returns: the transaction receipt of the claim
//
// Example
//
// tokenId = 0
// quantity = 1
//
// tx, err := contract.ClaimTo(context.Background(), tokenId, quantity)
func (erc1155 *ERC1155) Claim(ctx context.Context, tokenId int, quantity int) (*types.Transaction, error) {
address := erc1155.helper.GetSignerAddress().String()
return erc1155.ClaimTo(ctx, address, tokenId, quantity)
}
// Claim an NFT to a specific wallet
//
// @extension: ERC1155ClaimCustom | ERC1155ClaimPhasesV2 | ERC1155ClaimConditionsV2
//
// tokenId: the token ID of the NFT to claim
//
// destinationAddress: the address of the wallet to claim the NFTs to
//
// quantity: the number of NFTs to claim
//
// returns: the transaction receipt of the claim
//
// Example
//
// address = "{{wallet_address}}"
// tokenId = 0
// quantity = 1
//
// tx, err := contract.ClaimTo(context.Background(), address, tokenId, quantity)
func (erc1155 *ERC1155) ClaimTo(ctx context.Context, destinationAddress string, tokenId int, quantity int) (*types.Transaction, error) {
claimVerification, err := erc1155.PrepareClaim(ctx, tokenId, quantity)
if err != nil {
return nil, err
}
active, err := erc1155.ClaimConditions.GetActive(ctx, tokenId)
if err != nil {
return nil, err
}
txOpts, err := erc1155.helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
txOpts.Value = claimVerification.Value
proof := abi.IDrop1155AllowlistProof{
Proof: claimVerification.Proofs,
QuantityLimitPerWallet: claimVerification.MaxClaimable,
PricePerToken: claimVerification.Price,
Currency: common.HexToAddress(claimVerification.CurrencyAddress),
}
tx, err := erc1155.drop.Claim(
txOpts,
common.HexToAddress(destinationAddress),
big.NewInt(int64(tokenId)),
big.NewInt(int64(quantity)),
common.HexToAddress(active.CurrencyAddress),
active.Price,
proof,
[]byte{},
)
if err != nil {
return nil, err
}
return erc1155.helper.AwaitTx(ctx, tx.Hash())
}
func (erc1155 *ERC1155) PrepareClaim(ctx context.Context, tokenId int, quantity int) (*ClaimVerification, error) {
addressToClaim := erc1155.helper.GetSignerAddress().Hex()
claimCondition, err := erc1155.ClaimConditions.GetActive(ctx, tokenId)
if err != nil {
return nil, err
}
merkleMetadata, err := erc1155.ClaimConditions.GetMerkleMetadata(ctx)
if err != nil {
return nil, err
}
claimVerification, err := prepareClaim(
ctx,
addressToClaim,
quantity,
claimCondition,
merkleMetadata,
erc1155.helper,
erc1155.storage,
)
if err != nil {
return nil, err
}
return claimVerification, nil
}
func (erc1155 *ERC1155) getTokenMetadata(ctx context.Context, tokenId int) (*NFTMetadata, error) {
if uri, err := erc1155.token.Uri(
&bind.CallOpts{Context: ctx},
big.NewInt(int64(tokenId)),
); err != nil {
return nil, ¬FoundError{
tokenId,
}
} else {
if nft, err := fetchTokenMetadata(ctx, tokenId, uri, erc1155.storage); err != nil {
return nil, err
} else {
return nft, nil
}
}
}
func fetchEditionsByTokenId(ctx context.Context, erc1155 *ERC1155, tokenIds []*big.Int) ([]*EditionMetadata, error) {
total := len(tokenIds)
ch := make(chan *EditionResult)
// fetch all nfts in parallel
for i := 0; i < total; i++ {
go func(id int) {
if nft, err := erc1155.Get(ctx, id); err == nil {
ch <- &EditionResult{nft, nil}
} else {
fmt.Println(err)
ch <- &EditionResult{nil, err}
}
}(i)
}
// wait for all goroutines to emit
results := make([]*EditionResult, total)
for i := range results {
results[i] = <-ch
}
// filter out errors
nfts := []*EditionMetadata{}
for _, res := range results {
if res.nft != nil {
nfts = append(nfts, res.nft)
}
}
// Sort by ID
sort.SliceStable(nfts, func(i, j int) bool {
return nfts[i].Metadata.Id.Cmp(nfts[j].Metadata.Id) < 0
})
return nfts, nil
}