Collection for Holding NFTs
Collection for Holding NFTs
14 Oct 2022
Contributed by Flow Blockchain
This resource holds your NFTS in a collection in your account.
Smart Contract Example
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
pub resource Collection: NFTReceiver {
// dictionary of NFT conforming tokens
// NFT is a resource type with an UInt64 ID field
pub var ownedNFTs: @{UInt64: NFT}
// Initialize the NFTs field to an empty collection
init () {
self.ownedNFTs <- {}
}
// withdraw
//
// Function that removes an NFT from the collection
// and moves it to the calling context
pub fun withdraw(withdrawID: UInt64): @NFT {
// If the NFT isn't found, the transaction panics and reverts
let token <- self.ownedNFTs.remove(key: withdrawID)!
return <-token
}
// deposit
//
// Function that takes a NFT as an argument and
// adds it to the collections dictionary
pub fun deposit(token: @NFT) {
// add the new token to the dictionary with a force assignment
// if there is already a value at that key, it will fail and revert
self.ownedNFTs[token.id] <-! token
}
// idExists checks to see if a NFT
// with the given ID exists in the collection
pub fun idExists(id: UInt64): Bool {
return self.ownedNFTs[id] != nil
}
// getIDs returns an array of the IDs that are in the collection
pub fun getIDs(): [UInt64] {
return self.ownedNFTs.keys
}
destroy() {
destroy self.ownedNFTs
}
}
This transaction is showing us how we would create a new empty collection and save that resource into a users account.
We then create a public capability to this resource by linking the interface that contains functions the public can use on this resource for this account.
Transaction Example
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
import NonFungibleToken from 0x03
import NewExampleNFT from 0x02
import MetadataViews from 0x01
// This transaction is what an account would run
// to set itself up to receive NFTs
transaction {
prepare(signer: AuthAccount) {
// Return early if the account already has a collection
if signer.borrow<&NewExampleNFT.Collection>(from: NewExampleNFT.CollectionStoragePath) != nil {
return
}
// Create a new empty collection
let collection <- NewExampleNFT.createEmptyCollection()
// save it to the account
signer.save(<-collection, to: NewExampleNFT.CollectionStoragePath)
// create a public capability for the collection
signer.link<&{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection}>(
NewExampleNFT.CollectionPublicPath,
target: NewExampleNFT.CollectionStoragePath
)
}
execute {
log("Setup account")
}
}
This transaction is used to mint an NFT from the account that has the minting resource saved to it. You are able to give other users minting rights, check out the Admin Minting example for that.
In this case you are defining who is the person that will be receiving the NFT and checking if they actually have the capability to store the NFT in their collection before moving forward. As well as borrowing the Minting resource to mint new NFTS that should be in the account that has the minter saved.
At the end you are executing the transaction and minting the NFT.
Up Next: Creating Collection For Account
15%