Cadence Cookbook

Contribute

Multiple Metadata Views

Multiple Metadata Views

01 Apr 2022

Contributed by Flow Blockchain

Intermediate

Have more views you want to create for metadata? This is how you create new views in your smart contract and how you execute a transaction to display them all.

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 70 71 72 73 74 //Structure created so that a traits view can be displayed pub struct Traits{ pub let power: String pub let will: String pub let determination: String init( power: String will: String determination: String ){ self.power=power self.will=will self.determination=determination } } //NFT resources pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver { pub let id: UInt64 pub let name: String pub let thumbnail: String pub let description: String pub let power: String pub let will: String pub let determination: String pub fun getViews(): [Type] { return [ Type<MetadataViews.Display>(), Type<NewExampleNFT.Traits>() ] } pub fun resolveView(_ view: Type): AnyStruct? { switch view { case Type<MetadataViews.Display>(): return MetadataViews.Display( name: self.name, description: self.description, thumbnail: self.thumbnail ) case Type<NewExampleNFT.Traits>(): return NewExampleNFT.Traits( power: self.power, will: self.will, determination: self.determination ) } return nil } init( id: UInt64, name: String, description: String, thumbnail: String, power: String, will: String, determination: String ) { self.id = id self.name = name self.thumbnail = thumbnail self.description = description self.power = power self.will = will self.determination= determination } }

To have more than just the display view, you can create your own Struct in your contract so that it may be used as a view in your NFT.

Here we create a Traits structure. We then include the Traits view in the getViews function as well as include it as an option in the resolve view function.

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 import MetadataViews from 0x01 import NewExampleNFT from 0x02 pub fun main(): AnyStruct { let address: Address = 0x02 let id: UInt64 = 0 let account = getAccount(address) let collection = account .getCapability(/public/exampleNFTCollection) .borrow<&{MetadataViews.ResolverCollection}>() ?? panic("Could not borrow a reference to the collection") let nft = collection.borrowViewResolver(id: id) // Get the basic display information for this NFT let view = nft.resolveView(Type<NewExampleNFT.Traits>()) let oview = nft.resolveView(Type<MetadataViews.Display>()) let object = {"Traits": view, "Display": oview} return object }

Now, just as we did with the Display view, we resolve the view for the Traits view and can return it as a dictionary, or however you choose.

Now we have both views and metadata available with very little code needed.


ProgressNFT Fundamentals

Up Next: Implementing Series for NFTs

46%


Related Recipes

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