Cadence Cookbook

Contribute

Add a Play to TopShot Set

Add a Play to TopShot Set

01 Apr 2022

Contributed by Flow Blockchain

Intermediate

Once you have a set created and some plays, you can use this to add a play to a TopShot Set that can later be minted.

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 //More TopShot Code Above pub resource Set { // addPlay adds a play to the set // // Parameters: playID: The ID of the Play that is being added // // Pre-Conditions: // The Play needs to be an existing play // The Set needs to be not locked // The Play can't have already been added to the Set // pub fun addPlay(playID: UInt32) { pre { TopShot.playDatas[playID] != nil: "Cannot add the Play to Set: Play doesn't exist." !self.locked: "Cannot add the play to the Set after the set has been locked." self.numberMintedPerPlay[playID] == nil: "The play has already beed added to the set." } // Add the Play to the array of Plays self.plays.append(playID) // Open the Play up for minting self.retired[playID] = false // Initialize the Moment count to zero self.numberMintedPerPlay[playID] = 0 emit PlayAddedToSet(setID: self.setID, playID: playID) } .... } //More TopShot Code Below

Once you have a set created and you've created some plays, you can then add a play to your set by calling the addPlay function in your set.

You would need to pass in the playId's to have them available in the set. Before the function goes through it will check to see if the playID actually exists. If not you'll need to use another one, or create on.

It will also check if the Set has been locked, meaning no more plays can be added, or if the play has already been added to the set.

Once that check is complete, it will add the play to the array of play ID's in the set. The function will also include the play ID in a retired dictionary and set the boolean as false. The number minted per play ID will also be included in a dictionary and initialized as 0.

Finally you would emit an event that the play has been added to the 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 import TopShot from 0x01 transaction { let admin: &TopShot.Admin let borrowedSet: &TopShot.Set prepare(acct: AuthAccount) { self.admin = acct.borrow<&TopShot.Admin>(from: /storage/TopShotAdmin) ?? panic("Cant borrow admin resource") self.borrowedSet = self.admin.borrowSet(setID: 1) } execute{ self.borrowedSet.addPlay(playID: 1) self.borrowedSet.addPlay(playID: 2) self.borrowedSet.addPlay(playID: 3) log("play added") } }

To add a play to a set you will need to borrow a reference to the admin resource from the Auth Account.

Once you do, you will need to borrow the set that you would like access to by calling the borrowSet function. This gets whatever setID is created that you want to have access to.

When that happens you would just use the addPlay function to add whichever plays you have already created to this set.


ProgressNFT Fundamentals

Up Next: Minting a Moment in TopShot Set

92%


Related Recipes

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