Smart Contract
FlunksProfile
A.807c3d470888cc48.FlunksProfile
1// SPDX-License-Identifier: MIT
2
3import Flunks from 0x807c3d470888cc48
4import MetadataViews from 0x1d7e57aa55817448
5
6pub contract FlunksProfile {
7 pub event ContractInitialized()
8
9 pub let AdminStoragePath: StoragePath
10
11 access(self) var WalletToProfile: {Address: {String: String}};
12
13 pub fun updateProfileImg(owner: AuthAccount, tokenID: UInt64) {
14 // Validate user's ownership of the token for PFP
15 let collection = getAccount(owner.address).getCapability<&Flunks.Collection{Flunks.FlunksCollectionPublic}>(Flunks.CollectionPublicPath).borrow()!
16 let ownerCollectionTokenIds = collection.getIDs()
17 if !ownerCollectionTokenIds.contains(tokenID) {
18 panic("Not owner")
19 }
20
21 // Get the metadata for the token in the generic way
22 let item = collection.borrowNFT(id: tokenID)
23 let view = item.resolveView(Type<MetadataViews.Display>())
24 ?? panic("Could not resolve the item's metadata view")
25 let display = view as! MetadataViews.Display
26 let thumbnail = display.thumbnail as! MetadataViews.HTTPFile
27
28 let editionView = item.resolveView(Type<MetadataViews.Edition>())
29 ?? panic("Could not get the item's edition view")
30 let edition = editionView as! MetadataViews.Edition
31
32 if (!FlunksProfile.WalletToProfile.containsKey(owner.address)) {
33 FlunksProfile.WalletToProfile[owner.address] = {
34 "profilePictureTokenId": tokenID.toString(),
35 "profilePictureTemplateId": edition.number.toString(),
36 "profilePictureUrl": thumbnail.url
37 }
38 } else {
39 let currProfileOjb = FlunksProfile.WalletToProfile[owner.address]!
40 currProfileOjb["profilePictureUrl"] = thumbnail.url
41 currProfileOjb["profilePictureTokenId"] = tokenID.toString()
42 currProfileOjb["profilePictureTemplateId"] = edition.number.toString()
43 FlunksProfile.WalletToProfile[owner.address] = currProfileOjb
44 }
45 }
46
47 pub fun getProfile(address: Address): {String: String} {
48 return self.WalletToProfile[address] ?? {}
49 }
50
51 pub resource Admin {
52 pub fun updateProfile(address: Address, newProfileObj: {String: String}) {
53
54 }
55 }
56
57 init() {
58 self.AdminStoragePath = /storage/FlunksProfileAdmin
59
60 self.WalletToProfile = {}
61
62 let admin <- create Admin()
63 self.account.save(<-admin, to: self.AdminStoragePath)
64
65 emit ContractInitialized()
66 }
67}