-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft_collection.go
241 lines (218 loc) · 6.12 KB
/
nft_collection.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
package web3sdkio
import (
"context"
"math/big"
"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/web3sdkio/go-sdk/v2/abi"
)
// You can access the NFT Collection interface from the SDK as follows:
//
// import (
// "github.com/web3sdkio/go-sdk/v2/web3sdkio"
// )
//
// privateKey = "..."
//
// sdk, err := web3sdkio.NewWeb3sdkioSDK("mumbai", &web3sdkio.SDKOptions{
// PrivateKey: privateKey,
// })
//
// contract, err := sdk.GetNFTCollection("{{contract_address}}")
type NFTCollection struct {
abi *abi.TokenERC721
helper *contractHelper
*ERC721
Signature *ERC721SignatureMinting
Encoder *ContractEncoder
Events *ContractEvents
}
func newNFTCollection(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*NFTCollection, error) {
if contractAbi, err := abi.NewTokenERC721(address, provider); err != nil {
return nil, err
} else {
if helper, err := newContractHelper(address, provider, privateKey); err != nil {
return nil, err
} else {
if erc721, err := newERC721(provider, address, privateKey, storage); err != nil {
return nil, err
} else {
signature, err := newERC721SignatureMinting(provider, address, privateKey, storage)
if err != nil {
return nil, err
}
encoder, err := newContractEncoder(abi.TokenERC721ABI, helper)
if err != nil {
return nil, err
}
events, err := newContractEvents(abi.TokenERC721ABI, helper)
if err != nil {
return nil, err
}
nftCollection := &NFTCollection{
contractAbi,
helper,
erc721,
signature,
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 *NFTCollection) 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.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 *NFTCollection) GetOwnedTokenIDs(ctx context.Context, address string) ([]*big.Int, error) {
if address == "" {
address = nft.helper.GetSignerAddress().String()
}
if balance, err := nft.abi.BalanceOf(&bind.CallOpts{Context: ctx}, common.HexToAddress(address)); err != nil {
return nil, err
} else {
tokenIds := []*big.Int{}
for i := 0; i < int(balance.Int64()); i++ {
if tokenId, err := nft.abi.TokenOfOwnerByIndex(&bind.CallOpts{Context: ctx}, common.HexToAddress(address), big.NewInt(int64(i))); err == nil {
tokenIds = append(tokenIds, tokenId)
}
}
return tokenIds, nil
}
}
// Mint a new NFT to the connected wallet.
//
// metadata: metadata of the NFT to mint
//
// returns: the transaction receipt of the mint
func (nft *NFTCollection) Mint(ctx context.Context, metadata *NFTMetadataInput) (*types.Transaction, error) {
address := nft.helper.GetSignerAddress().String()
return nft.MintTo(ctx, address, metadata)
}
// Mint a new NFT to the specified wallet.
//
// address: the wallet address to mint to
//
// metadata: metadata of the NFT to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// image, err := os.Open("path/to/image.jpg")
// defer image.Close()
//
// metadata := &web3sdkio.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// Image: image,
// }
//
// tx, err := contract.MintTo(context.Background(), "{{wallet_address}}", metadata)
func (nft *NFTCollection) MintTo(ctx context.Context, address string, metadata *NFTMetadataInput) (*types.Transaction, error) {
uri, err := uploadOrExtractUri(metadata, nft.storage)
if err != nil {
return nil, err
}
txOpts, err := nft.helper.getTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := nft.abi.MintTo(
txOpts,
common.HexToAddress(address),
uri,
)
if err != nil {
return nil, err
}
return nft.helper.awaitTx(tx.Hash())
}
// Mint a batch of new NFTs to the connected wallet.
//
// metadatas: list of metadata of the NFTs to mint
//
// returns: the transaction receipt of the mint
func (nft *NFTCollection) MintBatch(ctx context.Context, metadatas []*NFTMetadataInput) (*types.Transaction, error) {
address := nft.helper.GetSignerAddress().String()
return nft.MintBatchTo(ctx, address, metadatas)
}
// Mint a batch of new NFTs to the specified wallet.
//
// to: the wallet address to mint to
//
// metadatas: list of metadata of the NFTs to mint
//
// returns: the transaction receipt of the mint
//
// Example
//
// metadatas := []*web3sdkio.NFTMetadataInput{
// &web3sdkio.NFTMetadataInput{
// Name: "Cool NFT",
// Description: "This is a cool NFT",
// }
// &web3sdkio.NFTMetadataInput{
// Name: "Cool NFT 2",
// Description: "This is also a cool NFT",
// }
// }
//
// tx, err := contract.MintBatchTo(context.Background(), "{{wallet_address}}", metadatas)
func (nft *NFTCollection) MintBatchTo(ctx context.Context, address string, metadatas []*NFTMetadataInput) (*types.Transaction, error) {
uris, err := uploadOrExtractUris(metadatas, nft.storage)
if err != nil {
return nil, err
}
encoded := [][]byte{}
for _, uri := range uris {
txOpts, err := nft.helper.getEncodedTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := nft.abi.MintTo(
txOpts,
common.HexToAddress(address),
uri,
)
if err != nil {
return nil, err
}
encoded = append(encoded, tx.Data())
}
txOpts, err := nft.helper.getTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := nft.abi.Multicall(txOpts, encoded)
if err != nil {
return nil, err
}
return nft.helper.awaitTx(tx.Hash())
}