forked from onflow/nft-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFTCatalog.cdc
422 lines (343 loc) · 16.6 KB
/
NFTCatalog.cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import MetadataViews from "./MetadataViews.cdc"
// NFTCatalog
//
// A general purpose NFT registry for Flow NonFungibleTokens.
//
// Each catalog entry stores data about the NFT including
// its collection identifier, nft type, storage and public paths, etc.
//
// To make an addition to the catalog you can propose an NFT and provide its metadata.
// An Admin can approve a proposal which would add the NFT to the catalog
pub contract NFTCatalog {
// EntryAdded
// An NFT collection has been added to the catalog
pub event EntryAdded(
collectionIdentifier : String,
contractName : String,
contractAddress : Address,
nftType : Type,
storagePath: StoragePath,
publicPath: PublicPath,
privatePath: PrivatePath,
publicLinkedType : Type,
privateLinkedType : Type,
displayName : String,
description: String,
externalURL : String
)
// EntryUpdated
// An NFT Collection has been updated in the catalog
pub event EntryUpdated(
collectionIdentifier : String,
contractName : String,
contractAddress : Address,
nftType : Type,
storagePath: StoragePath,
publicPath: PublicPath,
privatePath: PrivatePath,
publicLinkedType : Type,
privateLinkedType : Type,
displayName : String,
description: String,
externalURL : String
)
// EntryRemoved
// An NFT Collection has been removed from the catalog
pub event EntryRemoved(collectionIdentifier : String, nftType: Type)
// ProposalEntryAdded
// A new proposal to make an addtion to the catalog has been made
pub event ProposalEntryAdded(proposalID : UInt64, collectionIdentifier : String, message: String, status: String, proposer : Address)
// ProposalEntryUpdated
// A proposal has been updated
pub event ProposalEntryUpdated(proposalID : UInt64, collectionIdentifier : String, message: String, status: String, proposer : Address)
// ProposalEntryRemoved
// A proposal has been removed from storage
pub event ProposalEntryRemoved(proposalID : UInt64)
pub let ProposalManagerStoragePath: StoragePath
pub let ProposalManagerPublicPath: PublicPath
access(self) let catalog: {String : NFTCatalog.NFTCatalogMetadata} // { collectionIdentifier -> Metadata }
access(self) let catalogTypeData: {String : {String : Bool}} // Additional view to go from { NFT Type Identifier -> {Collection Identifier : Bool } }
access(self) let catalogProposals : {UInt64 : NFTCatalogProposal} // { ProposalID : Metadata }
access(self) var totalProposals : UInt64
// NFTCatalogProposalManager
// Used to authenticate proposals made to the catalog
pub resource interface NFTCatalogProposalManagerPublic {
pub fun getCurrentProposalEntry(): String?
}
pub resource NFTCatalogProposalManager : NFTCatalogProposalManagerPublic {
access(self) var currentProposalEntry: String?
pub fun getCurrentProposalEntry(): String? {
return self.currentProposalEntry
}
pub fun setCurrentProposalEntry(identifier: String?) {
self.currentProposalEntry = identifier
}
init () {
self.currentProposalEntry = nil
}
}
pub resource Snapshot {
pub var catalogSnapshot: {String : NFTCatalogMetadata}
pub var shouldUseSnapshot: Bool
pub fun setPartialSnapshot(_ snapshotKey: String, _ snapshotEntry: NFTCatalogMetadata) {
self.catalogSnapshot[snapshotKey] = snapshotEntry
}
pub fun setShouldUseSnapshot(_ shouldUseSnapshot: Bool) {
self.shouldUseSnapshot = shouldUseSnapshot
}
pub fun getCatalogSnapshot(): {String : NFTCatalogMetadata} {
return self.catalogSnapshot
}
init() {
self.shouldUseSnapshot = false
self.catalogSnapshot = {}
}
}
pub fun createEmptySnapshot(): @Snapshot {
return <- create Snapshot()
}
// NFTCollectionData
// Represents information about an NFT collection resource
// Note: Not suing the struct from Metadata standard due to
// inability to store functions
pub struct NFTCollectionData {
pub let storagePath : StoragePath
pub let publicPath : PublicPath
pub let privatePath: PrivatePath
pub let publicLinkedType: Type
pub let privateLinkedType: Type
init(
storagePath : StoragePath,
publicPath : PublicPath,
privatePath : PrivatePath,
publicLinkedType : Type,
privateLinkedType : Type
) {
self.storagePath = storagePath
self.publicPath = publicPath
self.privatePath = privatePath
self.publicLinkedType = publicLinkedType
self.privateLinkedType = privateLinkedType
}
}
// NFTCatalogMetadata
// Represents data about an NFT
pub struct NFTCatalogMetadata {
pub let contractName : String
pub let contractAddress : Address
pub let nftType: Type
pub let collectionData: NFTCollectionData
pub let collectionDisplay: MetadataViews.NFTCollectionDisplay
init (contractName : String, contractAddress : Address, nftType: Type, collectionData : NFTCollectionData, collectionDisplay : MetadataViews.NFTCollectionDisplay) {
self.contractName = contractName
self.contractAddress = contractAddress
self.nftType = nftType
self.collectionData = collectionData
self.collectionDisplay = collectionDisplay
}
}
// NFTCatalogProposal
// Represents a proposal to the catalog
// Includes data about an NFT
pub struct NFTCatalogProposal {
pub let collectionIdentifier : String
pub let metadata : NFTCatalogMetadata
pub let message : String
pub let status : String
pub let proposer : Address
pub let createdTime : UFix64
init(collectionIdentifier : String, metadata : NFTCatalogMetadata, message : String, status : String, proposer : Address) {
self.collectionIdentifier = collectionIdentifier
self.metadata = metadata
self.message = message
self.status = status
self.proposer = proposer
self.createdTime = getCurrentBlock().timestamp
}
}
/*
DEPRECATED
If obtaining all elements from the catalog is essential, please
use the getCatalogKeys and forEachCatalogKey methods instead.
*/
pub fun getCatalog() : {String : NFTCatalogMetadata} {
let snapshot = self.account.borrow<&NFTCatalog.Snapshot>(from: /storage/CatalogSnapshot)
if snapshot != nil {
let snapshot = snapshot!
if snapshot.shouldUseSnapshot {
return snapshot.getCatalogSnapshot()
} else {
return self.catalog
}
} else {
return self.catalog
}
}
pub fun getCatalogKeys(): [String] {
return self.catalog.keys
}
pub fun forEachCatalogKey(_ function: ((String): Bool)) {
self.catalog.forEachKey(function)
}
pub fun getCatalogEntry(collectionIdentifier : String) : NFTCatalogMetadata? {
return self.catalog[collectionIdentifier]
}
pub fun getCollectionsForType(nftTypeIdentifier: String) : {String : Bool}? {
return self.catalogTypeData[nftTypeIdentifier]
}
pub fun getCatalogTypeData() : {String : {String : Bool}} {
return self.catalogTypeData
}
// Propose an NFT collection to the catalog
// @param collectionIdentifier: The unique name assinged to this nft collection
// @param metadata: The Metadata for the NFT collection that will be stored in the catalog
// @param message: A message to the catalog owners
// @param proposer: Who is making the proposition(the address needs to be verified)
pub fun proposeNFTMetadata(collectionIdentifier : String, metadata : NFTCatalogMetadata, message : String, proposer : Address) : UInt64 {
let proposerManagerCap = getAccount(proposer).getCapability<&NFTCatalogProposalManager{NFTCatalog.NFTCatalogProposalManagerPublic}>(NFTCatalog.ProposalManagerPublicPath)
assert(proposerManagerCap.check(), message : "Proposer needs to set up a manager")
let proposerManagerRef = proposerManagerCap.borrow()!
assert(proposerManagerRef.getCurrentProposalEntry()! == collectionIdentifier, message: "Expected proposal entry does not match entry for the proposer")
let catalogProposal = NFTCatalogProposal(collectionIdentifier : collectionIdentifier, metadata : metadata, message : message, status: "IN_REVIEW", proposer: proposer)
self.totalProposals = self.totalProposals + 1
self.catalogProposals[self.totalProposals] = catalogProposal
emit ProposalEntryAdded(proposalID : self.totalProposals, collectionIdentifier : collectionIdentifier, message: catalogProposal.message, status: catalogProposal.status, proposer: catalogProposal.proposer)
return self.totalProposals
}
// Withdraw a proposal from the catalog
// @param proposalID: The ID of proposal you want to withdraw
pub fun withdrawNFTProposal(proposalID : UInt64) {
pre {
self.catalogProposals[proposalID] != nil : "Invalid Proposal ID"
}
let proposal = self.catalogProposals[proposalID]!
let proposer = proposal.proposer
let proposerManagerCap = getAccount(proposer).getCapability<&NFTCatalogProposalManager{NFTCatalog.NFTCatalogProposalManagerPublic}>(NFTCatalog.ProposalManagerPublicPath)
assert(proposerManagerCap.check(), message : "Proposer needs to set up a manager")
let proposerManagerRef = proposerManagerCap.borrow()!
assert(proposerManagerRef.getCurrentProposalEntry()! == proposal.collectionIdentifier, message: "Expected proposal entry does not match entry for the proposer")
self.removeCatalogProposal(proposalID : proposalID)
}
pub fun getCatalogProposals() : {UInt64 : NFTCatalogProposal} {
return self.catalogProposals
}
pub fun getCatalogProposalEntry(proposalID : UInt64) : NFTCatalogProposal? {
return self.catalogProposals[proposalID]
}
pub fun getCatalogProposalKeys() : [UInt64] {
return self.catalogProposals.keys
}
pub fun forEachCatalogProposalKey(_ function: ((UInt64): Bool)) {
self.catalogProposals.forEachKey(function)
}
pub fun createNFTCatalogProposalManager(): @NFTCatalogProposalManager {
return <-create NFTCatalogProposalManager()
}
access(account) fun addCatalogEntry(collectionIdentifier : String, metadata: NFTCatalogMetadata) {
pre {
self.catalog[collectionIdentifier] == nil : "The nft name has already been added to the catalog"
}
self.addCatalogTypeEntry(collectionIdentifier : collectionIdentifier , metadata: metadata)
self.catalog[collectionIdentifier] = metadata
emit EntryAdded(
collectionIdentifier : collectionIdentifier,
contractName : metadata.contractName,
contractAddress : metadata.contractAddress,
nftType: metadata.nftType,
storagePath: metadata.collectionData.storagePath,
publicPath: metadata.collectionData.publicPath,
privatePath: metadata.collectionData.privatePath,
publicLinkedType : metadata.collectionData.publicLinkedType,
privateLinkedType : metadata.collectionData.privateLinkedType,
displayName : metadata.collectionDisplay.name,
description: metadata.collectionDisplay.description,
externalURL : metadata.collectionDisplay.externalURL.url
)
}
access(account) fun updateCatalogEntry(collectionIdentifier : String , metadata: NFTCatalogMetadata) {
pre {
self.catalog[collectionIdentifier] != nil : "Invalid collection identifier"
}
// remove previous nft type entry
self.removeCatalogTypeEntry(collectionIdentifier : collectionIdentifier , metadata: metadata)
// add updated nft type entry
self.addCatalogTypeEntry(collectionIdentifier : collectionIdentifier , metadata: metadata)
self.catalog[collectionIdentifier] = metadata
let nftType = metadata.nftType
emit EntryUpdated(
collectionIdentifier : collectionIdentifier,
contractName : metadata.contractName,
contractAddress : metadata.contractAddress,
nftType: metadata.nftType,
storagePath: metadata.collectionData.storagePath,
publicPath: metadata.collectionData.publicPath,
privatePath: metadata.collectionData.privatePath,
publicLinkedType : metadata.collectionData.publicLinkedType,
privateLinkedType : metadata.collectionData.privateLinkedType,
displayName : metadata.collectionDisplay.name,
description: metadata.collectionDisplay.description,
externalURL : metadata.collectionDisplay.externalURL.url
)
}
access(account) fun removeCatalogEntry(collectionIdentifier : String) {
pre {
self.catalog[collectionIdentifier] != nil : "Invalid collection identifier"
}
let removedType = self.removeCatalogTypeEntry(collectionIdentifier : collectionIdentifier , metadata: self.catalog[collectionIdentifier]!)
self.catalog.remove(key: collectionIdentifier)
emit EntryRemoved(collectionIdentifier : collectionIdentifier, nftType: removedType)
}
// This function is not preferred, and was used for the following issue:
// https://github.com/onflow/cadence/issues/2649
// If a contract's type is no longer resolvable in cadence and crashing,
// this function can be used to remove it from the catalog.
access(account) fun removeCatalogEntryUnsafe(collectionIdentifier: String, nftTypeIdentifier: String) {
// Remove the catalog entry
self.catalog.remove(key: collectionIdentifier)
// Remove the type entry
if (self.catalogTypeData[nftTypeIdentifier]!.keys.length == 1) {
self.catalogTypeData.remove(key: nftTypeIdentifier)
} else {
self.catalogTypeData[nftTypeIdentifier]!.remove(key: collectionIdentifier)
}
}
access(account) fun updateCatalogProposal(proposalID: UInt64, proposalMetadata : NFTCatalogProposal) {
self.catalogProposals[proposalID] = proposalMetadata
emit ProposalEntryUpdated(proposalID : proposalID, collectionIdentifier : proposalMetadata.collectionIdentifier, message: proposalMetadata.message, status: proposalMetadata.status, proposer: proposalMetadata.proposer)
}
access(account) fun removeCatalogProposal(proposalID : UInt64) {
self.catalogProposals.remove(key : proposalID)
emit ProposalEntryRemoved(proposalID : proposalID)
}
access(contract) fun addCatalogTypeEntry(collectionIdentifier : String , metadata: NFTCatalogMetadata) {
if self.catalogTypeData[metadata.nftType.identifier] != nil {
let typeData : {String : Bool} = self.catalogTypeData[metadata.nftType.identifier]!
assert(self.catalogTypeData[metadata.nftType.identifier]![collectionIdentifier] == nil, message : "The nft name has already been added to the catalog")
typeData[collectionIdentifier] = true
self.catalogTypeData[metadata.nftType.identifier] = typeData
} else {
let typeData : {String : Bool} = {}
typeData[collectionIdentifier] = true
self.catalogTypeData[metadata.nftType.identifier] = typeData
}
}
access(contract) fun removeCatalogTypeEntry(collectionIdentifier : String , metadata: NFTCatalogMetadata): Type {
let prevMetadata = self.catalog[collectionIdentifier]!
let prevCollectionsForType = self.catalogTypeData[prevMetadata.nftType.identifier]!
prevCollectionsForType.remove(key : collectionIdentifier)
if prevCollectionsForType.length == 0 {
self.catalogTypeData.remove(key: prevMetadata.nftType.identifier)
} else {
self.catalogTypeData[prevMetadata.nftType.identifier] = prevCollectionsForType
}
return metadata.nftType
}
init() {
self.ProposalManagerStoragePath = /storage/nftCatalogProposalManager
self.ProposalManagerPublicPath = /public/nftCatalogProposalManager
self.totalProposals = 0
self.catalog = {}
self.catalogTypeData = {}
self.catalogProposals = {}
}
}