forked from thirdweb-dev/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nft_drop.go
258 lines (230 loc) · 7.11 KB
/
nft_drop.go
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
package thirdweb
import (
"context"
"math/big"
"strings"
"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/mry-am/thirdweb-go-sdk/v2/abi"
)
// You can access the NFT Drop interface from the SDK as follows:
//
// import (
// "github.com/mry-am/thirdweb-go-sdk/v2/thirdweb"
// )
//
// privateKey = "..."
//
// sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
// PrivateKey: privateKey,
// })
//
// contract, err := sdk.GetNFTDrop("{{contract_address}}")
type NFTDrop struct {
*ERC721Standard
Abi *abi.DropERC721
Helper *contractHelper
ClaimConditions *NFTDropClaimConditions
Encoder *NFTDropEncoder
Events *ContractEvents
}
func newNFTDrop(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*NFTDrop, error) {
if contractAbi, err := abi.NewDropERC721(address, provider); err != nil {
return nil, err
} else {
if helper, err := newContractHelper(address, provider, privateKey); err != nil {
return nil, err
} else {
if erc721, err := newERC721Standard(provider, address, privateKey, storage); err != nil {
return nil, err
} else {
claimConditions, err := newNFTDropClaimConditions(address, provider, helper, storage)
if err != nil {
return nil, err
}
encoder, err := newNFTDropEncoder(contractAbi, helper, claimConditions, storage)
if err != nil {
return nil, err
}
events, err := newContractEvents(abi.DropERC721ABI, helper)
if err != nil {
return nil, err
}
nftCollection := &NFTDrop{
erc721,
contractAbi,
helper,
claimConditions,
encoder,
events,
}
return nftCollection, nil
}
}
}
}
// Get the metadatas of all the NFTs owned by a specific address.
//
// address: the address of the owner of the NFTs
//
// returns: the metadata 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 (nft *NFTDrop) GetOwned(ctx context.Context, address string) ([]*NFTMetadataOwner, error) {
if address == "" {
address = nft.Helper.GetSignerAddress().String()
}
if tokenIds, err := nft.GetOwnedTokenIDs(ctx, address); err != nil {
return nil, err
} else {
return nft.erc721.fetchNFTsByTokenId(ctx, tokenIds)
}
}
// Get the tokenIds of all the NFTs owned by a specific address.
//
// address: the address of the owner of the NFTs
//
// returns: the tokenIds of all the NFTs owned by the address
func (nft *NFTDrop) GetOwnedTokenIDs(ctx context.Context, address string) ([]*big.Int, error) {
if address == "" {
address = nft.Helper.GetSignerAddress().String()
}
totalCount, err := nft.erc721.drop.NextTokenIdToMint(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
tokenIds := []*big.Int{}
for i := 0; i < int(totalCount.Int64()); i++ {
owner, err := nft.erc721.drop.OwnerOf(&bind.CallOpts{Context: ctx}, big.NewInt(int64(i)))
if err != nil {
return nil, err
}
if strings.ToLower(owner.String()) == strings.ToLower(address) {
tokenIds = append(tokenIds, big.NewInt(int64(i)))
}
}
return tokenIds, nil
}
// Get a list of all the NFTs that have been claimed from this contract.
//
// returns: a list of the metadatas of the claimed NFTs
//
// Example
//
// claimedNfts, err := contract.GetAllClaimed(context.Background())
// firstOwner := claimedNfts[0].Owner
func (drop *NFTDrop) GetAllClaimed(ctx context.Context) ([]*NFTMetadataOwner, error) {
return drop.erc721.GetAllClaimed(ctx)
}
// Get a list of all the NFTs on this contract that have not yet been claimed.
//
// returns: a list of the metadatas of the unclaimed NFTs
//
// Example
//
// unclaimedNfts, err := contract.GetAllUnclaimed(context.Background())
// firstNftName := unclaimedNfts[0].Name
func (drop *NFTDrop) GetAllUnclaimed(ctx context.Context) ([]*NFTMetadata, error) {
return drop.erc721.GetAllUnclaimed(ctx)
}
// Get the total number of NFTs that have been claimed.
func (drop *NFTDrop) TotalClaimedSupply(ctx context.Context) (int, error) {
return drop.erc721.TotalClaimedSupply(ctx)
}
// Get the total number of NFTs that have not yet been claimed.
func (drop *NFTDrop) TotalUnclaimedSupply(ctx context.Context) (int, error) {
return drop.erc721.TotalUnclaimedSupply(ctx)
}
func (drop *NFTDrop) GetTotalClaimed(ctx context.Context, address string) (*big.Int, error) {
events, err := drop.Events.GetEvents(ctx, "TokensClaimed", EventQueryOptions{
Filters: map[string]interface{}{
"receiver": common.HexToAddress(address),
},
})
if err != nil {
return nil, err
}
totalClaimed := big.NewInt(0)
for _, event := range events {
totalClaimed.Add(totalClaimed, event.Data["quantityClaimed"].(*big.Int))
}
return totalClaimed, nil
}
func (drop *NFTDrop) GetClaimInfo(ctx context.Context, address string) (*ClaimInfo, error) {
return drop.erc721.GetClaimInfo(ctx, address)
}
func (drop *NFTDrop) GetClaimIneligibilityReasons(ctx context.Context, quantity int, addressToCheck string) ([]ClaimEligibility, error) {
return drop.erc721.GetClaimIneligibilityReasons(ctx, quantity, addressToCheck)
}
// Create a batch of NFTs on this contract.
//
// 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()
//
// metadatas := []*thirdweb.NFTMetadataInput{
// &thirdweb.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image1
// }
// &thirdweb.NFTMetadataInput{
// Name: "Cool NFT 2",
// Description: "This is also a cool NFT",
// Image: image2
// }
// }
//
// tx, err := contract.CreateBatch(context.Background(), metadatas)
func (drop *NFTDrop) CreateBatch(ctx context.Context, metadatas []*NFTMetadataInput) (*types.Transaction, error) {
return drop.erc721.CreateBatch(ctx, metadatas)
}
// Claim NFTs from this contract to the connect wallet.
//
// quantity: the number of NFTs to claim
//
// returns: the transaction receipt of the claim
func (drop *NFTDrop) Claim(ctx context.Context, quantity int) (*types.Transaction, error) {
return drop.erc721.Claim(ctx, quantity)
}
// Claim NFTs from this contract to the connect wallet.
//
// 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}}"
// quantity = 1
//
// tx, err := contract.ClaimTo(context.Background(), address, quantity)
func (drop *NFTDrop) ClaimTo(ctx context.Context, destinationAddress string, quantity int) (*types.Transaction, error) {
return drop.erc721.ClaimTo(ctx, destinationAddress, quantity)
}
func (drop *NFTDrop) GetClaimArguments(
ctx context.Context,
destinationAddress string,
quantity int,
) (
*ClaimArguments,
error,
) {
return drop.erc721.GetClaimArguments(ctx, destinationAddress, quantity)
}