Smart Contract
Kings_Table_Crowns
A.bb39f0dae1547256.Kings_Table_Crowns
1import NonFungibleToken from 0x1d7e57aa55817448
2import MetadataViews from 0x1d7e57aa55817448
3
4pub contract Kings_Table_Crowns : NonFungibleToken {
5
6 pub var totalSupply: UInt64
7
8 pub let CollectionStoragePath: StoragePath
9 pub let CollectionPublicPath: PublicPath
10 pub let MinterStoragePath: StoragePath
11 pub let MinterPublicPath: PublicPath
12 //pub var CollectionPrivatePath: PrivatePath
13 pub event ContractInitialized()
14 pub event Withdraw(id: UInt64, from: Address?)
15 pub event Deposit(id: UInt64, to: Address?)
16 pub event Mint(id: UInt64, creator: Address, metadata: {String:String})
17 pub event Destroy(id: UInt64)
18
19 // We use dict to store raw metadata
20 pub resource interface RawMetadata {
21 pub fun getRawMetadata(): {String: String}
22 }
23
24 pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver, RawMetadata {
25 pub let id: UInt64
26 pub let creator: Address
27 access(self) let metadata: {String:String}
28
29 init(
30 id: UInt64,
31 creator: Address,
32 metadata: {String: String}
33 ) {
34 self.id = id
35 self.creator = creator
36 self.metadata = metadata
37 }
38
39 pub fun getViews(): [Type] {
40 return [
41 Type<MetadataViews.Display>()
42 ]
43 }
44
45 pub fun resolveView(_ view: Type): AnyStruct? {
46 switch view {
47 case Type<MetadataViews.Display>():
48 return MetadataViews.Display(
49 name: self.metadata["name"]!,
50 description: self.metadata["description"]!,
51 thumbnail: MetadataViews.HTTPFile(
52 url: self.metadata["thumbnail"]!
53 )
54 )
55 }
56
57 return nil
58 }
59
60 pub fun getRawMetadata(): {String: String} {
61 return self.metadata
62 }
63
64 destroy() {
65 emit Destroy(id: self.id)
66 }
67 }
68
69 pub resource interface Kings_Table_CrownsCollectionPublic {
70 pub fun deposit(token: @NonFungibleToken.NFT)
71 pub fun getIDs(): [UInt64]
72 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
73 pub fun borrowKings_Table_Crowns(id: UInt64): &Kings_Table_Crowns.NFT? {
74 post {
75 (result == nil) || (result?.id == id):
76 "Cannot borrow NFT reference: the ID of the returned reference is incorrect"
77 }
78 }
79 }
80
81 pub resource Collection: Kings_Table_CrownsCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
82
83 pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
84
85 init () {
86 self.ownedNFTs <- {}
87 }
88
89 // withdraw removes an NFT from the collection and moves it to the caller
90 pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
91 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
92
93 emit Withdraw(id: token.id, from: self.owner?.address)
94
95 return <-token
96 }
97
98 // deposit takes a NFT and adds it to the collections dictionary
99 // and adds the ID to the id array
100 pub fun deposit(token: @NonFungibleToken.NFT) {
101 let token <- token as! @Kings_Table_Crowns.NFT
102
103 let id: UInt64 = token.id
104
105 // add the new token to the dictionary which removes the old one
106 let oldToken <- self.ownedNFTs[id] <- token
107
108 emit Deposit(id: id, to: self.owner?.address)
109
110 destroy oldToken
111 }
112
113 // getIDs returns an array of the IDs that are in the collection
114 pub fun getIDs(): [UInt64] {
115 return self.ownedNFTs.keys
116 }
117
118 // borrowNFT gets a reference to an NFT in the collection
119 // so that the caller can read its metadata and call its methods
120 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
121 return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
122 }
123
124 pub fun borrowKings_Table_Crowns(id: UInt64): &Kings_Table_Crowns.NFT? {
125 if self.ownedNFTs[id] != nil {
126 // Create an authorized reference to allow downcasting
127 let ref = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT?
128 return ref as! &Kings_Table_Crowns.NFT?
129 }
130
131 return nil
132 }
133
134 pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
135 let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
136 let mlNFT = nft as! &Kings_Table_Crowns.NFT
137 return mlNFT
138 }
139
140 destroy() {
141 destroy self.ownedNFTs
142 }
143 }
144
145 // public function that anyone can call to create a new empty collection
146 pub fun createEmptyCollection(): @NonFungibleToken.Collection {
147 return <- create Collection()
148 }
149
150 // Resource that an admin or something similar would own to be
151 // able to mint new NFTs
152 //
153 pub resource NFTMinter {
154
155 // mintNFT mints a new NFT with a new ID
156 // and deposit it in the recipients collection using their collection reference
157 pub fun mintNFT(
158 recipient: &{NonFungibleToken.CollectionPublic},
159 metadata: {String: String}
160 ): &NonFungibleToken.NFT {
161
162 let creator = self.owner!.address
163 // create a new NFT
164 var newNFT <- create NFT(
165 id: Kings_Table_Crowns.totalSupply,
166 creator: creator,
167 metadata: metadata
168 )
169
170 let tokenRef = &newNFT as &NonFungibleToken.NFT
171 // deposit it in the recipient's account using their reference
172 recipient.deposit(token: <-newNFT)
173
174 Kings_Table_Crowns.totalSupply = Kings_Table_Crowns.totalSupply + 1
175
176 emit Mint(id: tokenRef.id, creator: creator, metadata: metadata)
177
178 return tokenRef
179 }
180 }
181
182 init() {
183 // Initialize the total supply
184 self.totalSupply = 0
185
186 // Set the named paths
187 self.CollectionStoragePath = /storage/MatrixMarketKings_Table_CrownsCollection
188 self.CollectionPublicPath = /public/MatrixMarketKings_Table_CrownsCollection
189 self.MinterStoragePath = /storage/MatrixMarketKings_Table_CrownsMinter
190 self.MinterPublicPath = /public/MatrixMarketKings_Table_CrownsMinter
191
192 // Create a Collection resource and save it to storage
193 let collection <- create Collection()
194 self.account.save(<-collection, to: self.CollectionStoragePath)
195
196 // create a public capability for the collection
197 self.account.link<&Kings_Table_Crowns.Collection{NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, Kings_Table_Crowns.Kings_Table_CrownsCollectionPublic, MetadataViews.ResolverCollection}>(
198 self.CollectionPublicPath,
199 target: self.CollectionStoragePath
200 )
201
202 // Create a Minter resource and save it to storage
203 let minter <- create NFTMinter()
204 self.account.save(<-minter, to: self.MinterStoragePath)
205
206 emit ContractInitialized()
207 }
208}
209