Traits are a very important metadata view because they tell Flowty how to enable filtering. The Traits view itself is a wrapper around an array of traits. Each trait contains:
The name of the trait (example: "Eyes")
The value of the trait (example: "Happy")
Values can be anything, but complex values are currently unsupported
If you want a trait to be grouped properly, limit them to primitive values such as "String", "Int", or "Bool"
If more than 10 values exist in the collection, a search bar will show up instead
The rarity of the trait (Optional)
If you specify rarity, setting its description to one of the following will color-code the trait:
Common
Uncommon
Default (the value if left blank)
Epic
Rare
Legendary
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
// ...
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
// ...
case Type<MetadataViews.Traits>():
// dict must be a subset of `{String: AnyStruct}`, Avataaars uses `{String: String}`
let traitsView = MetadataViews.dictToTraits(dict: self.renderer.flattened, excludedNames: [])
return traitsView
}
return nil
}
// ...
}
/// View to represent a single field of metadata on an NFT.
/// This is used to get traits of individual key/value pairs along with some
/// contextualized data about the trait
///
pub struct Trait {
// The name of the trait. Like Background, Eyes, Hair, etc.
pub let name: String
// The underlying value of the trait, the rest of the fields of a trait provide context to the value.
pub let value: AnyStruct
// displayType is used to show some context about what this name and value represent
// for instance, you could set value to a unix timestamp, and specify displayType as "Date" to tell
// platforms to consume this trait as a date and not a number
pub let displayType: String?
// Rarity can also be used directly on an attribute.
//
// This is optional because not all attributes need to contribute to the NFT's rarity.
pub let rarity: Rarity?
// truncated
}
/// Wrapper view to return all the traits on an NFT.
/// This is used to return traits as individual key/value pairs along with
/// some contextualized data about each trait.
pub struct Traits {
pub let traits: [Trait]
}