forked from onflow/flow-nft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_nft_metadata.cdc
149 lines (133 loc) · 5.53 KB
/
get_nft_metadata.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
/// This script gets all the view-based metadata associated with the specified NFT
/// and returns it as a single struct
import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
pub struct NFT {
pub let name: String
pub let description: String
pub let thumbnail: String
pub let owner: Address
pub let type: String
pub let royalties: [MetadataViews.Royalty]
pub let externalURL: String
pub let serialNumber: UInt64
pub let collectionPublicPath: PublicPath
pub let collectionStoragePath: StoragePath
pub let collectionProviderPath: PrivatePath
pub let collectionPublic: String
pub let collectionPublicLinkedType: String
pub let collectionProviderLinkedType: String
pub let collectionName: String
pub let collectionDescription: String
pub let collectionExternalURL: String
pub let collectionSquareImage: String
pub let collectionBannerImage: String
pub let collectionSocials: {String: String}
pub let edition: MetadataViews.Edition
pub let traits: MetadataViews.Traits
pub let medias: MetadataViews.Medias?
pub let license: MetadataViews.License?
init(
name: String,
description: String,
thumbnail: String,
owner: Address,
nftType: String,
royalties: [MetadataViews.Royalty],
externalURL: String,
serialNumber: UInt64,
collectionPublicPath: PublicPath,
collectionStoragePath: StoragePath,
collectionProviderPath: PrivatePath,
collectionPublic: String,
collectionPublicLinkedType: String,
collectionProviderLinkedType: String,
collectionName: String,
collectionDescription: String,
collectionExternalURL: String,
collectionSquareImage: String,
collectionBannerImage: String,
collectionSocials: {String: String},
edition: MetadataViews.Edition,
traits: MetadataViews.Traits,
medias:MetadataViews.Medias?,
license:MetadataViews.License?
) {
self.name = name
self.description = description
self.thumbnail = thumbnail
self.owner = owner
self.type = nftType
self.royalties = royalties
self.externalURL = externalURL
self.serialNumber = serialNumber
self.collectionPublicPath = collectionPublicPath
self.collectionStoragePath = collectionStoragePath
self.collectionProviderPath = collectionProviderPath
self.collectionPublic = collectionPublic
self.collectionPublicLinkedType = collectionPublicLinkedType
self.collectionProviderLinkedType = collectionProviderLinkedType
self.collectionName = collectionName
self.collectionDescription = collectionDescription
self.collectionExternalURL = collectionExternalURL
self.collectionSquareImage = collectionSquareImage
self.collectionBannerImage = collectionBannerImage
self.collectionSocials = collectionSocials
self.edition = edition
self.traits = traits
self.medias=medias
self.license=license
}
}
pub fun main(address: Address, id: UInt64): NFT {
let account = getAccount(address)
let collection = account
.getCapability(ExampleNFT.CollectionPublicPath)
.borrow<&{ExampleNFT.ExampleNFTCollectionPublic}>()
?? panic("Could not borrow a reference to the collection")
let nft = collection.borrowExampleNFT(id: id)!
// Get the basic display information for this NFT
let display = MetadataViews.getDisplay(nft)!
// Get the royalty information for the given NFT
let royaltyView = MetadataViews.getRoyalties(nft)!
let externalURL = MetadataViews.getExternalURL(nft)!
let collectionDisplay = MetadataViews.getNFTCollectionDisplay(nft)!
let nftCollectionView = MetadataViews.getNFTCollectionData(nft)!
let nftEditionView = MetadataViews.getEditions(nft)!
let serialNumberView = MetadataViews.getSerial(nft)!
let owner: Address = nft.owner!.address!
let nftType = nft.getType()
let collectionSocials: {String: String} = {}
for key in collectionDisplay.socials.keys {
collectionSocials[key] = collectionDisplay.socials[key]!.url
}
let traits = MetadataViews.getTraits(nft)!
let medias=MetadataViews.getMedias(nft)
let license=MetadataViews.getLicense(nft)
return NFT(
name: display.name,
description: display.description,
thumbnail: display.thumbnail.uri(),
owner: owner,
nftType: nftType.identifier,
royalties: royaltyView.getRoyalties(),
externalURL: externalURL.url,
serialNumber: serialNumberView.number,
collectionPublicPath: nftCollectionView.publicPath,
collectionStoragePath: nftCollectionView.storagePath,
collectionProviderPath: nftCollectionView.providerPath,
collectionPublic: nftCollectionView.publicCollection.identifier,
collectionPublicLinkedType: nftCollectionView.publicLinkedType.identifier,
collectionProviderLinkedType: nftCollectionView.providerLinkedType.identifier,
collectionName: collectionDisplay.name,
collectionDescription: collectionDisplay.description,
collectionExternalURL: collectionDisplay.externalURL.url,
collectionSquareImage: collectionDisplay.squareImage.file.uri(),
collectionBannerImage: collectionDisplay.bannerImage.file.uri(),
collectionSocials: collectionSocials,
edition: nftEditionView.infoList[0],
traits: traits,
medias:medias,
license:license
)
}