This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathget_kitty_item.cdc
78 lines (63 loc) · 2.22 KB
/
get_kitty_item.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
import NonFungibleToken from "../../contracts/NonFungibleToken.cdc"
import MetadataViews from "../../contracts/MetadataViews.cdc"
import KittyItems from "../../contracts/KittyItems.cdc"
pub struct KittyItem {
pub let name: String
pub let description: String
pub let thumbnail: String
pub let itemID: UInt64
pub let resourceID: UInt64
pub let kind: KittyItems.Kind
pub let rarity: KittyItems.Rarity
pub let owner: Address
init(
name: String,
description: String,
thumbnail: String,
itemID: UInt64,
resourceID: UInt64,
kind: KittyItems.Kind,
rarity: KittyItems.Rarity,
owner: Address,
) {
self.name = name
self.description = description
self.thumbnail = thumbnail
self.itemID = itemID
self.resourceID = resourceID
self.kind = kind
self.rarity = rarity
self.owner = owner
}
}
pub fun dwebURL(_ file: MetadataViews.IPFSFile): String {
var url = "https://"
.concat(file.cid)
.concat(".ipfs.dweb.link/")
if let path = file.path {
return url.concat(path)
}
return url
}
pub fun main(address: Address, itemID: UInt64): KittyItem? {
if let collection = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {
if let item = collection.borrowKittyItem(id: itemID) {
if let view = item.resolveView(Type<MetadataViews.Display>()) {
let display = view as! MetadataViews.Display
let owner: Address = item.owner!.address!
let ipfsThumbnail = display.thumbnail as! MetadataViews.IPFSFile
return KittyItem(
name: display.name,
description: display.description,
thumbnail: dwebURL(ipfsThumbnail),
itemID: itemID,
resourceID: item.uuid,
kind: item.kind,
rarity: item.rarity,
owner: address,
)
}
}
}
return nil
}