-
Notifications
You must be signed in to change notification settings - Fork 57
Metadata Feature Implementation #16
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
Open
satyamakgec
wants to merge
2
commits into
bebner:master
Choose a base branch
from
satyamakgec:feature-metadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
|
|
||
| This contract implements the metadata standard proposed | ||
| in FLIP-0636. | ||
|
|
||
| Ref: https://github.com/onflow/flow/blob/master/flips/20210916-nft-metadata.md | ||
|
|
||
| Structs and resources can implement one or more | ||
| metadata types, called views. Each view type represents | ||
| a different kind of metadata, such as a creator biography | ||
| or a JPEG image file. | ||
| */ | ||
|
|
||
| pub contract MetadataViews { | ||
|
|
||
| // A Resolver provides access to a set of metadata views. | ||
| // | ||
| // A struct or resource (e.g. an NFT) can implement this interface | ||
| // to provide access to the views that it supports. | ||
| // | ||
| pub resource interface Resolver { | ||
| pub fun getViews(): [Type] | ||
| pub fun resolveView(_ view: Type): AnyStruct? | ||
| } | ||
|
|
||
| // A ResolverCollection is a group of view resolvers index by ID. | ||
| // | ||
| pub resource interface ResolverCollection { | ||
| pub fun borrowViewResolver(id: UInt64): &{Resolver} | ||
| pub fun getIDs(): [UInt64] | ||
| } | ||
|
|
||
| // Display is a basic view that includes the name and description | ||
| // of an object. Most objects should implement this view. | ||
| // | ||
| pub struct Display { | ||
| pub let name: String | ||
| pub let description: String | ||
| pub let thumbnail: AnyStruct{File} | ||
|
|
||
| init( | ||
| name: String, | ||
| description: String, | ||
| thumbnail: AnyStruct{File} | ||
| ) { | ||
| self.name = name | ||
| self.description = description | ||
| self.thumbnail = thumbnail | ||
| } | ||
| } | ||
|
|
||
| // File is a generic interface that represents a file stored on or off chain. | ||
| // | ||
| // Files can be used to references images, videos and other media. | ||
| // | ||
| pub struct interface File { | ||
| pub fun uri(): String | ||
| } | ||
|
|
||
| // HTTPFile is a file that is accessible at an HTTP (or HTTPS) URL. | ||
| // | ||
| pub struct HTTPFile: File { | ||
| pub let url: String | ||
|
|
||
| init(url: String) { | ||
| self.url = url | ||
| } | ||
|
|
||
| pub fun uri(): String { | ||
| return self.url | ||
| } | ||
| } | ||
|
|
||
| // IPFSThumbnail returns a thumbnail image for an object | ||
| // stored as an image file in IPFS. | ||
| // | ||
| // IPFS images are referenced by their content identifier (CID) | ||
| // rather than a direct URI. A client application can use this CID | ||
| // to find and load the image via an IPFS gateway. | ||
| // | ||
| pub struct IPFSFile: File { | ||
|
|
||
| // CID is the content identifier for this IPFS file. | ||
| // | ||
| // Ref: https://docs.ipfs.io/concepts/content-addressing/ | ||
| // | ||
| pub let cid: String | ||
|
|
||
| // Path is an optional path to the file resource in an IPFS directory. | ||
| // | ||
| // This field is only needed if the file is inside a directory. | ||
| // | ||
| // Ref: https://docs.ipfs.io/concepts/file-systems/ | ||
| // | ||
| pub let path: String? | ||
|
|
||
| init(cid: String, path: String?) { | ||
| self.cid = cid | ||
| self.path = path | ||
| } | ||
|
|
||
| // This function returns the IPFS native URL for this file. | ||
| // | ||
| // Ref: https://docs.ipfs.io/how-to/address-ipfs-on-web/#native-urls | ||
| // | ||
| pub fun uri(): String { | ||
| if let path = self.path { | ||
| return "ipfs://".concat(self.cid).concat("/").concat(path) | ||
| } | ||
|
|
||
| return "ipfs://".concat(self.cid) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,70 @@ | ||
| import DappyContract from "../contracts/DappyContract.cdc" | ||
| import MetadataViews from "../contracts/MetadataViews.cdc"; | ||
|
|
||
| pub fun main(addr: Address): {UInt64: DappyContract.Template} { | ||
| pub struct DappyDetails { | ||
| pub let name: String | ||
| pub let description: String | ||
| pub let thumbnail: String | ||
|
|
||
| pub let id: UInt64 | ||
| pub let templateId: UInt64 | ||
| pub let dna: DappyContract.Dna | ||
| pub let owner: Address | ||
|
|
||
| init( | ||
| name: String, | ||
| description: String, | ||
| thumbnail: String, | ||
| id: UInt64, | ||
| templateId: UInt64, | ||
| dna: DappyContract.Dna, | ||
| owner: Address, | ||
| ) { | ||
| self.name = name | ||
| self.description = description | ||
| self.thumbnail = thumbnail | ||
| self.id = id | ||
| self.templateId = templateId | ||
| self.dna = dna | ||
| self.owner = owner | ||
| } | ||
| } | ||
|
|
||
| pub fun dwebURL(_ file: MetadataViews.IPFSFile): String { | ||
| var url = "https://" | ||
| .concat(file.cid) | ||
| .concat(".ipfs.dweb.link/") | ||
|
|
||
| if let path = file.path { | ||
| return url.concat(path) | ||
| } | ||
|
|
||
| return url | ||
| } | ||
|
|
||
| pub fun main(addr: Address): [DappyDetails]? { | ||
| var dappies: [DappyDetails] = [] | ||
| let account = getAccount(addr) | ||
| let ref = account.getCapability<&{DappyContract.CollectionPublic}>(DappyContract.CollectionPublicPath) | ||
| .borrow() ?? panic("Cannot borrow reference") | ||
| let dappies = ref.listDappies() | ||
| return dappies | ||
| if let collection = account.getCapability<&{DappyContract.CollectionPublic}>(DappyContract.CollectionPublicPath).borrow() { | ||
| let dappiesId = collection.getIDs() | ||
| for id in dappiesId { | ||
| if let dappy = collection.borrowDappy(id: id) { | ||
| if let view = dappy.resolveView(Type<MetadataViews.Display>()) { | ||
| let display = view as! MetadataViews.Display | ||
| let ipfsThumbnail = display.thumbnail as! MetadataViews.IPFSFile | ||
| dappies.append(DappyDetails( | ||
| name: display.name, | ||
| description: display.description, | ||
| thumbnail: dwebURL(ipfsThumbnail), | ||
| id: dappy.id, | ||
| templateId: dappy.data.templateID, | ||
| dna: dappy.data.dna, | ||
| owner: addr, | ||
| )) | ||
| } | ||
| } | ||
| } | ||
| return dappies | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to make defined in the contract instead of in a script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has no use in the contract as the data structure but can define in the contract as well, But I think it will just eat space besides making the system better,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't have use in the contract necessarily, but it might be more useful to have it centrally defined instead of having to define it in every script. For cryptodappy, it doesn't really matter because it isn't a production level contract that will actually be in use in the real world, but might be a good practice in smart contracts in general. But if you don't think it should be included in the contract, then I'll defer to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree with you that it is good to have it in the smart contract for the production-grade system so everyone can use that data structure.