Creating Collection For Account
Creating Collection For Account
14 Oct 2022
Contributed by Flow Blockchain
Beginner
Create a new collection for an existing Flow account that doesn't have one so that it can store your NFTS in it.
Smart Contract Example
1
2
3
4
// creates a new empty Collection resource and returns it
pub fun createEmptyCollection(): @Collection {
return <- create Collection()
}
This is a function you would include in your smart contract to create collections for accounts without them.
A collection resource is returned which you then save into your account with a transaction.
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
33
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 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.
ProgressNFT Fundamentals
Up Next: NFT with Metadata
23%