Cadence Cookbook

Contribute

Create a TopShot Play

Create a TopShot Play

01 Apr 2022

Contributed by Flow Blockchain

Intermediate

Using the TopShot contract, this is how you would create a Play to start adding them to your sets and mint moments out of them.

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 //TopShot Contract Code Above ... access(self) var playDatas: {UInt32: Play} pub var nextPlayID: UInt32 ..... // Play is a Struct that holds metadata associated // with a specific NBA play, like the legendary moment when // Ray Allen hit the 3 to tie the Heat and Spurs in the 2013 finals game 6 // or when Lance Stephenson blew in the ear of Lebron James. // // Moment NFTs will all reference a single play as the owner of // its metadata. The plays are publicly accessible, so anyone can // read the metadata associated with a specific play ID // pub struct Play { // The unique ID for the Play pub let playID: UInt32 // Stores all the metadata about the play as a string mapping // This is not the long term way NFT metadata will be stored. It's a temporary // construct while we figure out a better way to do metadata. // pub let metadata: {String: String} init(metadata: {String: String}) { pre { metadata.length != 0: "New Play metadata cannot be empty" } self.playID = TopShot.nextPlayID self.metadata = metadata } } ..... pub resource Admin { // createPlay creates a new Play struct // and stores it in the Plays dictionary in the TopShot smart contract // // Parameters: metadata: A dictionary mapping metadata titles to their data // example: {"Player Name": "Kevin Durant", "Height": "7 feet"} // (because we all know Kevin Durant is not 6'9") // // Returns: the ID of the new Play object // pub fun createPlay(metadata: {String: String}): UInt32 { // Create the new Play var newPlay = Play(metadata: metadata) let newID = newPlay.playID // Increment the ID so that it isn't used again TopShot.nextPlayID = TopShot.nextPlayID + UInt32(1) emit PlayCreated(id: newPlay.playID, metadata: metadata) // Store it in the contract storage TopShot.playDatas[newID] = newPlay return newID } .... } //rest of TopShot contract below

In the TopShot contract when you are creating a Play that will be included in sets as a moment you first have to start your contract off by creating a dictionary that stores all of the plays you create. Also you would create a variable called nextPlayID to make sure you aren't overlapping on ID's.

Then you would create a structure that would be stored in the playDatas dictionary. Here, all that is needed for the Play struct is to input a parameter for metadata which is an object containing strings.

Once these are established, you would have an admin resource that would hold the ability to create a new play. For more information on Admin resources check out the Admin resource example.

Here you would call the createPlay function that takes in a metadata argument. You would then create the new play by passing in the metadata. You would also take the newPlay's ID so that you can use it to assign the struct to the dictionary.

Then you would increment the ID so that it can't be used again, emit a PlayCreated event, and store the newPlay in the dictionary.

Transaction Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import TopShot from 0x01 transaction { let admin: &TopShot.Admin prepare(acct: AuthAccount) { self.admin = acct.borrow<&TopShot.Admin>(from: /storage/TopShotAdmin) ?? panic("Cant borrow admin resource") } execute { self.admin.createPlay(metadata: {"Rookie": "2004", "Player Name": "Dwight Howard"}) self.admin.createPlay(metadata: {"Rookie": "2003", "Player Name": "Dwayne Wade"}) log("play created") } }

To create a play, you first need to get a reference to the admin resource from the AuthAccount.

Once you receive that reference you can then create a play that gets stored in the playDatas dictionary.


ProgressNFT Fundamentals

Up Next: Create a TopShot Set

77%


Related Recipes

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