-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFTMetadataUtility.cdc
321 lines (293 loc) · 13.4 KB
/
NFTMetadataUtility.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
import MetadataViews from 0x1d7e57aa55817448
import NonFungibleToken from 0x1d7e57aa55817448
import NFTCatalog from 0x49a7cda3a1eecc29
import NFTStorefront from 0x4eb8a10cb9f87357
import NFTStorefrontV2 from 0x4eb8a10cb9f87357
import FlowtyStorefront from 0xac62859509383cd0
import TopShot from 0x0b2a3299cc857e29
access(all) contract NFTMetadataUtility {
access(all) struct CollectionItem {
pub let nftID: UInt64
pub let nftUUID: UInt64
pub let name: String
pub let description: String
pub let thumbnail: String
pub let externalURL: String
pub let ownerAddress: Address?
pub let royalties: [MetadataViews.Royalty]
pub let medias: [MetadataViews.Media]
pub let editions: [MetadataViews.Edition]
pub let serialNumber: UInt64?
pub let traits: [MetadataViews.Trait]
pub let rarity: MetadataViews.Rarity?
pub let license: MetadataViews.License?
pub let publicLinkedType: Type
pub let collectionName: String
pub let collectionDescription: String
pub let collectionSquareImage: String
pub let collectionBannerImage: String
pub let collectionSocials: {String: MetadataViews.ExternalURL}
init(
nftID: UInt64,
nftUUID: UInt64,
name: String,
description: String,
thumbnail: String,
externalURL: String,
ownerAddress: Address?,
royalties: [MetadataViews.Royalty],
medias: [MetadataViews.Media],
editions: [MetadataViews.Edition],
serialNumber: UInt64?,
traits: [MetadataViews.Trait],
rarity: MetadataViews.Rarity?,
license: MetadataViews.License?,
publicLinkedType: Type,
collectionName: String,
collectionDescription: String,
collectionSquareImage: String,
collectionBannerImage: String,
collectionSocials: {String: MetadataViews.ExternalURL}
) {
self.nftID = nftID
self.nftUUID = nftUUID
self.name = name
self.description = description
self.thumbnail = thumbnail
self.externalURL = externalURL
self.ownerAddress = ownerAddress
self.royalties = royalties
self.medias = medias
self.editions = editions
self.serialNumber = serialNumber
self.traits = traits
self.rarity = rarity
self.license = license
self.publicLinkedType = publicLinkedType
self.collectionName = collectionName
self.collectionDescription = collectionDescription
self.collectionSquareImage = collectionSquareImage
self.collectionBannerImage = collectionBannerImage
self.collectionSocials = collectionSocials
}
}
pub struct StorefrontItem {
pub let nft: CollectionItem
// Storefront Item info
pub let listingResourceID: UInt64
pub let storefrontID: UInt64
pub let purchased: Bool
pub let nftType: Type
pub let salePaymentVaultType: Type
pub let salePrice: UFix64
pub let saleCuts: [NFTStorefrontV2.SaleCut]
pub let saleCutsV1: [NFTStorefront.SaleCut]
pub let customID: String?
pub let commissionAmount: UFix64?
pub let expiry: UInt64?
init(
nft: CollectionItem,
listingResourceID: UInt64,
storefrontID: UInt64,
purchased: Bool,
nftType: Type,
salePaymentVaultType: Type,
salePrice: UFix64,
saleCuts: [NFTStorefrontV2.SaleCut],
saleCutsV1: [NFTStorefront.SaleCut],
customID: String?,
commissionAmount: UFix64?,
expiry: UInt64?
) {
self.nft = nft
self.listingResourceID = listingResourceID
self.storefrontID = storefrontID
self.purchased = purchased
self.nftType = nftType
self.salePaymentVaultType = salePaymentVaultType
self.salePrice = salePrice
self.saleCuts = saleCuts
self.saleCutsV1 = saleCutsV1
self.customID = customID
self.commissionAmount = commissionAmount
self.expiry = expiry
}
}
access(all) fun getMetadataFromNFTRef(nftRef: &NonFungibleToken.NFT, owner: Address): CollectionItem {
let displayView = nftRef.resolveView(Type<MetadataViews.Display>())! as! MetadataViews.Display
let externalURLView = nftRef.resolveView(Type<MetadataViews.ExternalURL>())! as! MetadataViews.ExternalURL
let collectionDataView = nftRef.resolveView(Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData
let collectionDisplayView = nftRef.resolveView(Type<MetadataViews.NFTCollectionDisplay>())! as! MetadataViews.NFTCollectionDisplay
let royaltyView = nftRef.resolveView(Type<MetadataViews.Royalties>())! as! MetadataViews.Royalties
if (displayView == nil || externalURLView == nil || collectionDataView == nil || collectionDisplayView == nil || royaltyView == nil) {
panic("NFT does not have proper metadata views implemented.")
}
// Optional metadata views
let mediasView = nftRef.resolveView(Type<MetadataViews.Medias>())
let editionsView = nftRef.resolveView(Type<MetadataViews.Editions>())
let serialView = nftRef.resolveView(Type<MetadataViews.Serial>())
let traitsView = nftRef.resolveView(Type<MetadataViews.Traits>())
let rarityView = nftRef.resolveView(Type<MetadataViews.Rarity>())
let licenseView = nftRef.resolveView(Type<MetadataViews.License>())
var medias: [MetadataViews.Media] = []
if mediasView != nil {
medias = (mediasView! as! MetadataViews.Medias).items
}
var editions: [MetadataViews.Edition] = []
if editionsView != nil {
editions = (editionsView! as! MetadataViews.Editions).infoList
}
var serialNumber: UInt64? = nil
if serialView != nil {
serialNumber = (serialView! as! MetadataViews.Serial).number
}
var traits: [MetadataViews.Trait] = []
if traitsView != nil {
traits = (traitsView! as! MetadataViews.Traits).traits
}
var rarity: MetadataViews.Rarity? = nil
if rarityView != nil {
rarity = rarityView! as! MetadataViews.Rarity
}
var license: MetadataViews.License? = nil
if licenseView != nil {
license = licenseView! as! MetadataViews.License
}
return CollectionItem(
nftID: nftRef.id,
nftUUID: nftRef.uuid,
name: displayView!.name,
description : displayView!.description,
thumbnail : displayView!.thumbnail.uri(),
externalURL : externalURLView!.url,
ownerAddress: owner,
royalties : royaltyView!.getRoyalties(),
medias: medias,
editions: editions,
serialNumber: serialNumber,
traits: traits,
rarity: rarity,
license: license,
publicLinkedType : collectionDataView!.publicLinkedType,
collectionName : collectionDisplayView!.name,
collectionDescription : collectionDisplayView!.description,
collectionSquareImage : collectionDisplayView!.squareImage.file.uri(),
collectionBannerImage : collectionDisplayView!.bannerImage.file.uri(),
collectionSocials: collectionDisplayView!.socials
)
}
access(all) fun getStorefrontV2NFTRef(owner: Address, listingResourceID: UInt64): &NonFungibleToken.NFT? {
let storefrontRef = getAccount(owner)
.getCapability<&NFTStorefrontV2.Storefront{NFTStorefrontV2.StorefrontPublic}>(
NFTStorefrontV2.StorefrontPublicPath
)
.borrow()
?? panic("Could not borrow public storefront from address")
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
return listing.borrowNFT()
}
access(all) fun getStorefrontV2ListingMetadata(owner: Address, listingResourceID: UInt64): StorefrontItem {
let storefrontRef = getAccount(owner)
.getCapability<&NFTStorefrontV2.Storefront{NFTStorefrontV2.StorefrontPublic}>(
NFTStorefrontV2.StorefrontPublicPath
)
.borrow()
?? panic("Could not borrow public storefront from address")
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
let listingDetails = listing.getDetails()
let nftRef = listing.borrowNFT()
let collectionItem = NFTMetadataUtility.getMetadataFromNFTRef(nftRef: nftRef!, owner: owner)
return StorefrontItem(
nft: collectionItem,
listingResourceID: listingResourceID,
storefrontID: listingDetails.storefrontID,
purchased: listingDetails.purchased,
nftType: listingDetails.nftType,
salePaymentVaultType: listingDetails.salePaymentVaultType,
salePrice: listingDetails.salePrice,
saleCuts: listingDetails.saleCuts,
saleCutsV1: [],
customID: listingDetails.customID,
commissionAmount: listingDetails.commissionAmount,
expiry: listingDetails.expiry
)
}
access(all) fun getStorefrontV2FlowtyNFTRef(owner: Address, listingResourceID: UInt64): &NonFungibleToken.NFT? {
let storefrontRef = FlowtyStorefront.getStorefrontRef(owner: owner)
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
return listing.borrowNFT()
}
access(all) fun getStorefrontV2FlowtyListingMetadata(owner: Address, listingResourceID: UInt64): StorefrontItem {
let storefrontRef = FlowtyStorefront.getStorefrontRef(owner: owner)
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
let listingDetails = listing.getDetails()
let nftRef = listing.borrowNFT()
let collectionItem = NFTMetadataUtility.getMetadataFromNFTRef(nftRef: nftRef!, owner: owner)
return StorefrontItem(
nft: collectionItem,
listingResourceID: listingResourceID,
storefrontID: listingDetails.storefrontID,
purchased: listingDetails.purchased,
nftType: listingDetails.nftType,
salePaymentVaultType: listingDetails.salePaymentVaultType,
salePrice: listingDetails.salePrice,
saleCuts: [],
saleCutsV1: [],
customID: listingDetails.customID,
commissionAmount: listingDetails.commissionAmount,
expiry: listingDetails.expiry
)
}
access(all) fun getStorefrontV1NFTRef(owner: Address, listingResourceID: UInt64): &NonFungibleToken.NFT? {
let storefrontRef = getAccount(owner)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(
NFTStorefront.StorefrontPublicPath
)
.borrow()
?? panic("Could not borrow public storefront from address")
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
return listing.borrowNFT()
}
access(all) fun getStorefrontV1ListingMetadata(owner: Address, listingResourceID: UInt64): StorefrontItem {
let storefrontRef = getAccount(owner)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(
NFTStorefront.StorefrontPublicPath
)
.borrow()
?? panic("Could not borrow public storefront from address")
let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID)
?? panic("No item with that ID")
let listingDetails = listing.getDetails()
let nftRef = listing.borrowNFT()
let collectionItem = NFTMetadataUtility.getMetadataFromNFTRef(nftRef: nftRef!, owner: owner)
return StorefrontItem(
nft: collectionItem,
listingResourceID: listingResourceID,
storefrontID: listingDetails.storefrontID,
purchased: listingDetails.purchased,
nftType: listingDetails.nftType,
salePaymentVaultType: listingDetails.salePaymentVaultType,
salePrice: listingDetails.salePrice,
saleCuts: [],
saleCutsV1: listingDetails.saleCuts,
customID: nil,
commissionAmount: nil,
expiry: nil
)
}
access(all) fun getTopShotNFTRef(owner: Address, nftID: UInt64): &NonFungibleToken.NFT? {
let collectionRef = getAccount(owner).getCapability(/public/MomentCollection)
.borrow<&{TopShot.MomentCollectionPublic}>()
?? panic("Could not get reference to public TopShot collection")
return collectionRef.borrowNFT(id: nftID)
}
access(all) fun getTopShotMetadata(owner: Address, nftID: UInt64): CollectionItem {
let nftRef = NFTMetadataUtility.getTopShotNFTRef(owner: owner, nftID: nftID)
return NFTMetadataUtility.getMetadataFromNFTRef(nftRef: nftRef!, owner: owner)
}
}