Cadence Cookbook

Contribute

Minting NFTs in a Set

Minting NFTs in a Set

14 Oct 2022

Contributed by Flow Blockchain

Intermediate

You've created your set in your series and now you're ready to mint your NFTS. This code shows you how to do that.

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 //more code from series resource above... pub fun mintSetAndSeriesNFT( recipient: &{NonFungibleToken.CollectionPublic}, tokenId: UInt64, setId: UInt32) { pre { self.numberEditionsMintedPerSet[setId] != nil: "The Set does not exist." self.numberEditionsMintedPerSet[setId]! < SetAndSeries.getSetMaxEditions(setId: setId)!: "Set has reached maximum NFT edition capacity." } // Gets the number of editions that have been minted so far in // this set let editionNum: UInt32 = self.numberEditionsMintedPerSet[setId]! + (1 as UInt32) // deposit it in the recipient's account using their reference recipient.deposit(token: <-create SetAndSeries.NFT( tokenId: tokenId, setId: setId, editionNum: editionNum )) // Increment the count of global NFTs SetAndSeries.totalSupply = SetAndSeries.totalSupply + (1 as UInt64) // Update the count of Editions minted in the set self.numberEditionsMintedPerSet[setId] = editionNum } //more code from series resource below...

Once you've created a set in a series, you can now mint as many NFTS as your set allows. The parameters you take in are a recepient with the capability to hold these NFTS in their collection, a tokenID and the setID you will be minting these NFTS into.

Before you execute the function, you check to see if the set exists and if the set has not exceeded the max # of editions(NFTS) allowed to be minted per set.

If that all looks good, you then add to the editions minted in the current set. Then you deposit the NFT into the recepients collection.

Afterwards you increase the totaly supply of the NFTS and you update the minted # of editions in the current set.

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 import SetAndSeries from 0x01 import NonFungibleToken from 0x02 transaction { let adminCheck: &SetAndSeries.Admin let seriesRef: &SetAndSeries.Series let receiver: &{NonFungibleToken.CollectionPublic} prepare(acct: AuthAccount) { self.adminCheck = acct.borrow<&SetAndSeries.Admin>(from: SetAndSeries.AdminStoragePath) ?? panic("could not borrow admin reference") self.seriesRef = self.adminCheck.borrowSeries(seriesId: 1) self.receiver = acct.getCapability<&SetAndSeries.Collection{NonFungibleToken.CollectionPublic}>(SetAndSeries.CollectionPublicPath).borrow() ?? panic("could not borrow capability") } execute { self.seriesRef.mintSetAndSeriesNFT(recipient: self.receiver , tokenId: 1, setId: 1) log("minted NFT in account 1") } }

Before you execute the transaction to mint an NFT you need to borrow the admin resource from the AuthAccount if it has one. If it does, you then borrow the series you would like access to for minting NFTS.

You also need to get the capability for the receiver to store the NFT in their collection.

After that, you're able to access the mint function for NFTS in the contract. Enter in all the parameters needed and then execute your function.


ProgressNFT Fundamentals

Up Next: Create a TopShot Play

69%


Related Recipes

14 Oct 2022
Mint NFT
Beginner
14 Oct 2022
Collection for Holding NFTs
Beginner
14 Oct 2022
NFT with Metadata
Beginner