Smart Contract
ChainzNFT
A.ff342b6df9ef019d.ChainzNFT
1/*
2*
3* This is an example implementation of a Flow Non-Fungible Token
4* It is not part of the official standard but it assumed to be
5* similar to how many NFTs would implement the core functionality.
6*
7* This contract does not implement any sophisticated classification
8* system for its NFTs. It defines a simple NFT with minimal metadata.
9*
10*/
11
12import NonFungibleToken from 0x1d7e57aa55817448
13import MetadataViews from 0x1d7e57aa55817448
14
15pub contract ChainzNFT: NonFungibleToken {
16
17 pub var totalSupply: UInt64
18
19 pub event ContractInitialized()
20 pub event Withdraw(id: UInt64, from: Address?)
21 pub event Deposit(id: UInt64, to: Address?)
22
23 pub let CollectionStoragePath: StoragePath
24 pub let CollectionPublicPath: PublicPath
25
26 pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
27 pub let id: UInt64
28 pub let sequence: UInt64
29 pub let name: String
30 pub let description: String
31 pub let thumbnail: String
32 pub let metadata: {String: AnyStruct}
33
34 init(
35 name: String,
36 description: String,
37 thumbnail: String,
38 metadata: {String: String},
39 ) {
40 ChainzNFT.totalSupply = ChainzNFT.totalSupply + 1
41 self.id = self.uuid
42 self.sequence = ChainzNFT.totalSupply
43 self.name = name
44 self.description = description
45 self.thumbnail = thumbnail
46 self.metadata = metadata
47 }
48
49 pub fun getViews(): [Type] {
50 return [
51 Type<MetadataViews.Display>()
52 ]
53 }
54
55 pub fun resolveView(_ view: Type): AnyStruct? {
56 switch view {
57 case Type<MetadataViews.Display>():
58 return MetadataViews.Display(
59 name: self.name,
60 description: self.description,
61 thumbnail: MetadataViews.HTTPFile(
62 url: self.thumbnail
63 )
64 )
65 }
66 return nil
67 }
68 }
69
70 pub resource interface CollectionPublic {
71 pub fun deposit(token: @NonFungibleToken.NFT)
72 pub fun getIDs(): [UInt64]
73 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
74 pub fun borrowChainzNFT(id: UInt64): &ChainzNFT.NFT? {
75 post {
76 (result == nil) || (result?.id == id):
77 "Cannot borrow ChainzNFT reference: the ID of the returned reference is incorrect"
78 }
79 }
80 }
81
82 pub resource Collection: CollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
83 // dictionary of NFT conforming tokens
84 // NFT is a resource type with an `UInt64` ID field
85 pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
86
87 init () {
88 self.ownedNFTs <- {}
89 }
90
91 // withdraw removes an NFT from the collection and moves it to the caller
92 pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
93 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
94
95 emit Withdraw(id: token.id, from: self.owner?.address)
96
97 return <-token
98 }
99
100 // deposit takes a NFT and adds it to the collections dictionary
101 // and adds the ID to the id array
102 pub fun deposit(token: @NonFungibleToken.NFT) {
103 let token <- token as! @ChainzNFT.NFT
104 let id: UInt64 = token.id
105 self.ownedNFTs[id] <-! token
106 emit Deposit(id: id, to: self.owner?.address)
107 }
108
109 // getIDs returns an array of the IDs that are in the collection
110 pub fun getIDs(): [UInt64] {
111 return self.ownedNFTs.keys
112 }
113
114 // borrowNFT gets a reference to an NFT in the collection
115 // so that the caller can read its metadata and call its methods
116 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
117 return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
118 }
119
120 pub fun borrowChainzNFT(id: UInt64): &ChainzNFT.NFT? {
121 if self.ownedNFTs[id] != nil {
122 // Create an authorized reference to allow downcasting
123 let ref = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
124 return ref as! &ChainzNFT.NFT
125 }
126
127 return nil
128 }
129
130 pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
131 let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
132 let ChainzNFT = nft as! &ChainzNFT.NFT
133 return ChainzNFT as &AnyResource{MetadataViews.Resolver}
134 }
135
136 destroy() {
137 destroy self.ownedNFTs
138 }
139 }
140
141 // public function that anyone can call to create a new empty collection
142 pub fun createEmptyCollection(): @NonFungibleToken.Collection {
143 return <- create Collection()
144 }
145
146 access(account) fun createNFT(name: String, description: String, thumbnail: String, metadata: {String: String}): @NFT {
147 return <- create NFT(name: name, description: description, thumbnail: thumbnail, metadata: metadata)
148 }
149
150 init() {
151 // Initialize the total supply
152 self.totalSupply = 0
153
154 // Set the named paths
155 self.CollectionStoragePath = /storage/ChainzNFTCollection
156 self.CollectionPublicPath = /public/ChainzNFTCollection
157
158 emit ContractInitialized()
159 }
160}
161