Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update display type serialization #131

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions cadence/contracts/utils/SerializeMetadata.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ access(all) contract SerializeMetadata {
// Append results from the token-level Display view to the serialized JSON compatible string
if nftDisplay != nil {
serializedResult = serializedResult
.concat(name).concat(Serialize.tryToJSONString(nftDisplay!.name)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(nftDisplay!.description)!).concat(", ")
.concat(image).concat(Serialize.tryToJSONString(nftDisplay!.thumbnail.uri())!)
.concat(name).concat(Serialize.tryToJSONString(nftDisplay!.name)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(nftDisplay!.description)!).concat(", ")
.concat(image).concat(Serialize.tryToJSONString(nftDisplay!.thumbnail.uri())!)
// Append the `externa_url` value from NFTCollectionDisplay view if present
if collectionDisplay != nil {
return serializedResult.concat(", ")
.concat(externalURL).concat(Serialize.tryToJSONString(collectionDisplay!.externalURL.url)!)
.concat(externalURL).concat(Serialize.tryToJSONString(collectionDisplay!.externalURL.url)!)
}
}

Expand All @@ -100,10 +100,10 @@ access(all) contract SerializeMetadata {

// Without token-level view, serialize as contract-level metadata
return serializedResult
.concat(name).concat(Serialize.tryToJSONString(collectionDisplay!.name)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(collectionDisplay!.description)!).concat(", ")
.concat(image).concat(Serialize.tryToJSONString(collectionDisplay!.squareImage.file.uri())!).concat(", ")
.concat(externalLink).concat(Serialize.tryToJSONString(collectionDisplay!.externalURL.url)!)
.concat(name).concat(Serialize.tryToJSONString(collectionDisplay!.name)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(collectionDisplay!.description)!).concat(", ")
.concat(image).concat(Serialize.tryToJSONString(collectionDisplay!.squareImage.file.uri())!).concat(", ")
.concat(externalLink).concat(Serialize.tryToJSONString(collectionDisplay!.externalURL.url)!)
}

/// Serializes given Traits view as a JSON compatible string. If a given Trait is not serializable, it is skipped
Expand All @@ -112,7 +112,7 @@ access(all) contract SerializeMetadata {
/// @param traits: The Traits view to be serialized
///
/// @returns: A JSON compatible string containing the serialized traits as:
/// `\"attributes\": [{\"trait_type\": \"<trait.name>\", \"value\": \"<trait.value>\"}, {...}]`
/// `\"attributes\": [{\"trait_type\": \"<trait.name>\", \"display_type\": \"<trait.displayType>\", \"value\": \"<trait.value>\"}, {...}]`
///
access(all)
fun serializeNFTTraitsAsAttributes(_ traits: MetadataViews.Traits): String {
Expand All @@ -130,7 +130,11 @@ access(all) contract SerializeMetadata {
}
serializedResult = serializedResult.concat("{")
.concat("\"trait_type\": ").concat(Serialize.tryToJSONString(trait.name)!)
.concat(", \"value\": ").concat(value!)
if trait.displayType != nil {
serializedResult = serializedResult.concat(", \"display_type\": ")
.concat(Serialize.tryToJSONString(trait.displayType)!)
}
serializedResult = serializedResult.concat(", \"value\": ").concat(value!)
.concat("}")
if i < traits!.traits.length - 1 {
serializedResult = serializedResult.concat(",")
Expand Down Expand Up @@ -160,11 +164,11 @@ access(all) contract SerializeMetadata {
let externalLink = "\"external_link\": "

return "data:application/json;utf8,{"
.concat(name).concat(Serialize.tryToJSONString(ftDisplay.name)!).concat(", ")
.concat(symbol).concat(Serialize.tryToJSONString(ftDisplay.symbol)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(ftDisplay.description)!).concat(", ")
.concat(externalLink).concat(Serialize.tryToJSONString(ftDisplay.externalURL.url)!)
.concat("}")
.concat(name).concat(Serialize.tryToJSONString(ftDisplay.name)!).concat(", ")
.concat(symbol).concat(Serialize.tryToJSONString(ftDisplay.symbol)!).concat(", ")
.concat(description).concat(Serialize.tryToJSONString(ftDisplay.description)!).concat(", ")
.concat(externalLink).concat(Serialize.tryToJSONString(ftDisplay.externalURL.url)!)
.concat("}")
}

/// Derives a symbol for use as an ERC20 or ERC721 symbol from a given string, presumably a Cadence contract name.
Expand Down
Loading