forked from onflow/nft-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFTRetrieval.cdc
214 lines (183 loc) · 8.71 KB
/
NFTRetrieval.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
import MetadataViews from "./MetadataViews.cdc"
import NFTCatalog from "./NFTCatalog.cdc"
// NFTRetrieval
//
// A helper contract to get NFT's in a users account
// leveraging the NFTCatalog Smart Contract
pub contract NFTRetrieval {
pub fun getRecommendedViewsTypes(version: String) : [Type] {
switch version {
case "v1":
return [
Type<MetadataViews.Display>(),
Type<MetadataViews.ExternalURL>(),
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.Royalties>()
]
default:
panic("Version not supported")
}
return []
}
pub fun getNFTIDsFromCap(collectionIdentifier: String, collectionCap : Capability<&AnyResource{MetadataViews.ResolverCollection}>) : [UInt64] {
pre {
NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier) != nil : "Invalid collection identifier"
}
let value = NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier)!
// Check if we have multiple collections for the NFT type...
let hasMultipleCollections = self.hasMultipleCollections(nftTypeIdentifier : value.nftType.identifier)
if collectionCap.check() {
let collectionRef = collectionCap.borrow()!
if !hasMultipleCollections {
return collectionRef.getIDs()
}
var ids : [UInt64] = []
for id in collectionRef.getIDs() {
let nftResolver = collectionRef.borrowViewResolver(id: id)
let nftViews = MetadataViews.getNFTView(id: id, viewResolver: nftResolver)
if nftViews.display!.name == value.collectionDisplay.name {
ids.append(id)
}
}
return ids
}
return []
}
pub fun getNFTCountFromCap(collectionIdentifier: String, collectionCap : Capability<&AnyResource{MetadataViews.ResolverCollection}>) : UInt64 {
pre {
NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier) != nil : "Invalid collection identifier"
}
let value = NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier)!
// Check if we have multiple collections for the NFT type...
let hasMultipleCollections = self.hasMultipleCollections(nftTypeIdentifier : value.nftType.identifier)
if collectionCap.check() {
let collectionRef = collectionCap.borrow()!
if !hasMultipleCollections {
return UInt64(collectionRef.getIDs().length)
}
var count : UInt64 = 0
for id in collectionRef.getIDs() {
let nftResolver = collectionRef.borrowViewResolver(id: id)
let nftViews = MetadataViews.getNFTView(id: id, viewResolver: nftResolver)
if nftViews.display!.name == value.collectionDisplay.name {
count = count + 1
}
}
return count
}
return 0
}
pub fun getAllMetadataViewsFromCap(collectionIdentifier: String, collectionCap : Capability<&AnyResource{MetadataViews.ResolverCollection}>) : {String: AnyStruct} {
pre {
NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier) != nil : "Invalid collection identifier"
}
let items : {String: AnyStruct} = {}
let value = NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier)!
// Check if we have multiple collections for the NFT type...
let hasMultipleCollections = self.hasMultipleCollections(nftTypeIdentifier : value.nftType.identifier)
if collectionCap.check() {
let collectionRef = collectionCap.borrow()!
for id in collectionRef.getIDs() {
let nftResolver = collectionRef.borrowViewResolver(id: id)
let supportedNftViewTypes = nftResolver.getViews()
for supportedViewType in supportedNftViewTypes {
if let view = nftResolver.resolveView(supportedViewType) {
if !hasMultipleCollections {
items.insert(key : supportedViewType.identifier, view)
} else if MetadataViews.getDisplay(nftResolver)!.name == value.collectionDisplay.name {
items.insert(key : supportedViewType.identifier, view)
}
}
}
}
}
return items
}
pub fun getNFTViewsFromCap(collectionIdentifier: String, collectionCap : Capability<&AnyResource{MetadataViews.ResolverCollection}>) : [MetadataViews.NFTView] {
pre {
NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier) != nil : "Invalid collection identifier"
}
let items : [MetadataViews.NFTView] = []
let value = NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier)!
// Check if we have multiple collections for the NFT type...
let hasMultipleCollections = self.hasMultipleCollections(nftTypeIdentifier : value.nftType.identifier)
if collectionCap.check() {
let collectionRef = collectionCap.borrow()!
for id in collectionRef.getIDs() {
let nftResolver = collectionRef.borrowViewResolver(id: id)
let nftViews = MetadataViews.getNFTView(id: id, viewResolver: nftResolver)
if !hasMultipleCollections {
items.append(nftViews)
} else if nftViews.display!.name == value.collectionDisplay.name {
items.append(nftViews)
}
}
}
return items
}
pub fun getNFTViewsFromIDs(collectionIdentifier : String, ids: [UInt64], collectionCap : Capability<&AnyResource{MetadataViews.ResolverCollection}>) : [MetadataViews.NFTView] {
pre {
NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier) != nil : "Invalid collection identifier"
}
let items : [MetadataViews.NFTView] = []
let value = NFTCatalog.getCatalogEntry(collectionIdentifier: collectionIdentifier)!
// Check if we have multiple collections for the NFT type...
let hasMultipleCollections = self.hasMultipleCollections(nftTypeIdentifier : value.nftType.identifier)
if collectionCap.check() {
let collectionRef = collectionCap.borrow()!
for id in ids {
if !collectionRef.getIDs().contains(id) {
continue
}
let nftResolver = collectionRef.borrowViewResolver(id: id)
let nftViews = MetadataViews.getNFTView(id: id, viewResolver: nftResolver)
if !hasMultipleCollections {
items.append(nftViews)
} else if nftViews.display!.name == value.collectionDisplay.name {
items.append(nftViews)
}
}
}
return items
}
access(contract) fun hasMultipleCollections(nftTypeIdentifier : String): Bool {
let typeCollections = NFTCatalog.getCollectionsForType(nftTypeIdentifier: nftTypeIdentifier)!
var numberOfCollections = 0
for identifier in typeCollections.keys {
let existence = typeCollections[identifier]!
if existence {
numberOfCollections = numberOfCollections + 1
}
if numberOfCollections > 1 {
return true
}
}
return false
}
//LEGACY - DO NOT USE
pub struct BaseNFTViewsV1 {
pub let id: UInt64
pub let display: MetadataViews.Display?
pub let externalURL: MetadataViews.ExternalURL?
pub let collectionData: MetadataViews.NFTCollectionData?
pub let collectionDisplay: MetadataViews.NFTCollectionDisplay?
pub let royalties: MetadataViews.Royalties?
init(
id : UInt64,
display : MetadataViews.Display?,
externalURL : MetadataViews.ExternalURL?,
collectionData : MetadataViews.NFTCollectionData?,
collectionDisplay : MetadataViews.NFTCollectionDisplay?,
royalties : MetadataViews.Royalties?
) {
self.id = id
self.display = display
self.externalURL = externalURL
self.collectionData = collectionData
self.collectionDisplay = collectionDisplay
self.royalties = royalties
}
}
init() {}
}