-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProfile.cdc
686 lines (586 loc) · 19.5 KB
/
Profile.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Inspiration: https://flow-view-source.com/testnet/account/0xba1132bc08f82fe2/contract/Ghost
*/
import FungibleToken from "../contracts/standard/FungibleToken.cdc"
import ProfileCache from "../contracts/ProfileCache.cdc"
import FindUtils from "../contracts/FindUtils.cdc"
pub contract Profile {
pub let publicPath: PublicPath
pub let publicReceiverPath: PublicPath
pub let storagePath: StoragePath
//and event emitted when somebody follows another user
pub event Follow(follower:Address, following: Address, tags: [String])
//an event emitted when somebody unfollows somebody
pub event Unfollow(follower:Address, unfollowing: Address)
//and event emitted when a user verifies something
pub event Verification(account:Address, message:String)
pub event Created(account:Address, userName:String, findName:String, createdAt:String)
pub event Updated(account:Address, userName:String, findName:String, thumbnail:String)
/*
Represents a Fungible token wallet with a name and a supported type.
*/
pub struct Wallet {
pub let name: String
pub let receiver: Capability<&{FungibleToken.Receiver}>
pub let balance: Capability<&{FungibleToken.Balance}>
pub let accept: Type
pub let tags: [String]
init(
name: String,
receiver: Capability<&{FungibleToken.Receiver}>,
balance: Capability<&{FungibleToken.Balance}>,
accept: Type,
tags: [String]
) {
self.name=name
self.receiver=receiver
self.balance=balance
self.accept=accept
self.tags=tags
}
}
/*
Represent a collection of a Resource that you want to expose
Since NFT standard is not so great at just add Type and you have to use instanceOf to check for now
*/
pub struct ResourceCollection {
pub let collection: Capability
pub let tags: [String]
pub let type: Type
pub let name: String
init(name: String, collection:Capability, type: Type, tags: [String]) {
self.name=name
self.collection=collection
self.tags=tags
self.type=type
}
}
pub struct CollectionProfile{
pub let tags: [String]
pub let type: String
pub let name: String
init(_ collection: ResourceCollection){
self.name=collection.name
self.type=collection.type.identifier
self.tags=collection.tags
}
}
/*
A link that you could add to your profile
*/
pub struct Link {
pub let url: String
pub let title: String
pub let type: String
init(title: String, type: String, url: String) {
self.url=url
self.title=title
self.type=type
}
}
/*
Information about a connection between one profile and another.
*/
pub struct FriendStatus {
pub let follower: Address
pub let following:Address
pub let tags: [String]
init(follower: Address, following:Address, tags: [String]) {
self.follower=follower
self.following=following
self.tags= tags
}
}
pub struct WalletProfile {
pub let name: String
pub let balance: UFix64
pub let accept: String
pub let tags: [String]
init(_ wallet: Wallet) {
self.name=wallet.name
self.balance=wallet.balance.borrow()?.balance ?? 0.0
self.accept=wallet.accept.identifier
self.tags=wallet.tags
}
}
//This is the new return struct from the profile
pub struct UserReport {
pub let findName: String
pub let createdAt: String
pub let address: Address
pub let name: String
pub let gender: String
pub let description: String
pub let tags: [String]
pub let avatar: String
pub let links: {String:Link}
pub let wallets: [WalletProfile]
pub let following: [FriendStatus]
pub let followers: [FriendStatus]
pub let allowStoringFollowers: Bool
init(
findName:String,
address: Address,
name: String,
gender: String,
description: String,
tags: [String],
avatar: String,
links: {String:Link},
wallets: [WalletProfile],
following: [FriendStatus],
followers: [FriendStatus],
allowStoringFollowers:Bool,
createdAt: String
) {
self.findName=findName
self.address=address
self.name=name
self.gender=gender
self.description=description
self.tags=tags
self.avatar=avatar
self.links=links
self.wallets=wallets
self.following=following
self.followers=followers
self.allowStoringFollowers=allowStoringFollowers
self.createdAt=createdAt
}
}
//This format is deperated
pub struct UserProfile {
pub let findName: String
pub let createdAt: String
pub let address: Address
pub let name: String
pub let gender: String
pub let description: String
pub let tags: [String]
pub let avatar: String
pub let links: [Link]
pub let wallets: [WalletProfile]
pub let collections: [CollectionProfile]
pub let following: [FriendStatus]
pub let followers: [FriendStatus]
pub let allowStoringFollowers: Bool
init(
findName:String,
address: Address,
name: String,
gender: String,
description: String,
tags: [String],
avatar: String,
links: [Link],
wallets: [WalletProfile],
collections: [CollectionProfile],
following: [FriendStatus],
followers: [FriendStatus],
allowStoringFollowers:Bool,
createdAt: String
) {
self.findName=findName
self.address=address
self.name=name
self.gender=gender
self.description=description
self.tags=tags
self.avatar=avatar
self.links=links
self.collections=collections
self.wallets=wallets
self.following=following
self.followers=followers
self.allowStoringFollowers=allowStoringFollowers
self.createdAt=createdAt
}
}
pub resource interface Public{
pub fun getAddress() : Address
pub fun getName(): String
pub fun getFindName(): String
pub fun getCreatedAt(): String
pub fun getGender(): String
pub fun getDescription(): String
pub fun getTags(): [String]
pub fun getAvatar(): String
pub fun getCollections(): [ResourceCollection]
pub fun follows(_ address: Address) : Bool
pub fun getFollowers(): [FriendStatus]
pub fun getFollowing(): [FriendStatus]
pub fun getWallets() : [Wallet]
pub fun hasWallet(_ name: String) : Bool
pub fun getLinks() : [Link]
pub fun deposit(from: @FungibleToken.Vault)
pub fun supportedFungigleTokenTypes() : [Type]
pub fun asProfile() : UserProfile
pub fun asReport() : UserReport
pub fun isBanned(_ val: Address): Bool
pub fun isPrivateModeEnabled() : Bool
access(contract) fun internal_addFollower(_ val: FriendStatus)
access(contract) fun internal_removeFollower(_ address: Address)
access(account) fun setFindName(_ val: String)
}
pub resource interface Owner {
pub fun setName(_ val: String) {
pre {
val.length <= 64: "Name must be 64 or less characters"
}
}
pub fun setGender(_ val:String){
pre {
val.length <= 64: "Gender must be 64 or less characters"
}
}
pub fun setAvatar(_ val: String){
pre {
val.length <= 1024: "Avatar must be 1024 characters or less"
}
}
pub fun setTags(_ val: [String]) {
pre {
Profile.verifyTags(tags: val, tagLength:64, tagSize:32) : "cannot have more then 32 tags of length 64"
}
}
//validate length of description to be 255 or something?
pub fun setDescription(_ val: String) {
pre {
val.length <= 1024: "Description must be 1024 characters or less"
}
}
pub fun follow(_ address: Address, tags:[String]) {
pre {
Profile.verifyTags(tags: tags, tagLength:64, tagSize:32) : "cannot have more then 32 tags of length 64"
}
}
pub fun unfollow(_ address: Address)
pub fun removeCollection(_ val: String)
pub fun addCollection(_ val: ResourceCollection)
pub fun addWallet(_ val : Wallet)
pub fun removeWallet(_ val: String)
pub fun setWallets(_ val: [Wallet])
pub fun hasWallet(_ name: String) : Bool
pub fun addLink(_ val: Link)
pub fun addLinkWithName(name:String, link:Link)
pub fun removeLink(_ val: String)
//Verify that this user has signed something.
pub fun verify(_ val:String)
//A user must be able to remove a follower since this data in your account is added there by another user
pub fun removeFollower(_ val: Address)
//manage bans
pub fun addBan(_ val: Address)
pub fun removeBan(_ val: Address)
pub fun getBans(): [Address]
//Set if user is allowed to store followers or now
pub fun setAllowStoringFollowers(_ val: Bool)
//set if this user prefers sensitive information about his account to be kept private, no guarantee here but should be honored
pub fun setPrivateMode(_ val: Bool)
}
pub resource User: Public, Owner, FungibleToken.Receiver {
access(self) var name: String
access(self) var findName: String
access(self) var createdAt: String
access(self) var gender: String
access(self) var description: String
access(self) var avatar: String
access(self) var tags: [String]
access(self) var followers: {Address: FriendStatus}
access(self) var bans: {Address: Bool}
access(self) var following: {Address: FriendStatus}
access(self) var collections: {String: ResourceCollection}
access(self) var wallets: [Wallet]
access(self) var links: {String: Link}
access(self) var allowStoringFollowers: Bool
//this is just a bag of properties if we need more fields here, so that we can do it with contract upgrade
access(self) var additionalProperties: {String : String}
init(name:String, createdAt: String) {
let randomNumber = (1 as UInt64) + (unsafeRandom() % 25)
self.createdAt=createdAt
self.name = name
self.findName=""
self.gender=""
self.description=""
self.tags=[]
self.avatar = "https://find.xyz/assets/img/avatars/avatar".concat(randomNumber.toString()).concat(".png")
self.followers = {}
self.following = {}
self.collections={}
self.wallets=[]
self.links={}
self.allowStoringFollowers=true
self.bans={}
self.additionalProperties={}
}
/// We do not have a seperate field for this so we use the additionalProperties 'bag' to store this in
pub fun setPrivateMode(_ val: Bool) {
var private="true"
if !val{
private="false"
}
self.additionalProperties["privateMode"] = private
}
pub fun emitUpdatedEvent() {
emit Updated(account:self.owner!.address, userName:self.name, findName:self.findName, thumbnail:self.avatar)
}
pub fun emitCreatedEvent() {
emit Created(account:self.owner!.address, userName:self.name, findName:self.findName, createdAt:self.createdAt)
}
pub fun isPrivateModeEnabled() : Bool {
let boolString= self.additionalProperties["privateMode"]
if boolString== nil || boolString=="false" {
return false
}
return true
}
pub fun addBan(_ val: Address) { self.bans[val]= true}
pub fun removeBan(_ val:Address) { self.bans.remove(key: val) }
pub fun getBans() : [Address] { return self.bans.keys }
pub fun isBanned(_ val:Address) : Bool { return self.bans.containsKey(val)}
pub fun setAllowStoringFollowers(_ val: Bool) {
self.allowStoringFollowers=val
}
pub fun verify(_ val:String) {
emit Verification(account: self.owner!.address, message:val)
}
pub fun asReport() : UserReport {
let wallets: [WalletProfile]=[]
for w in self.wallets {
wallets.append(WalletProfile(w))
}
return UserReport(
findName: self.getFindName(),
address: self.owner!.address,
name: self.getName(),
gender: self.getGender(),
description: self.getDescription(),
tags: self.getTags(),
avatar: self.getAvatar(),
links: self.getLinksMap(),
wallets: wallets,
following: self.getFollowing(),
followers: self.getFollowers(),
allowStoringFollowers: self.allowStoringFollowers,
createdAt:self.getCreatedAt()
)
}
pub fun getAddress() : Address {
return self.owner!.address
}
pub fun asProfile() : UserProfile {
let wallets: [WalletProfile]=[]
for w in self.wallets {
wallets.append(WalletProfile(w))
}
let collections:[CollectionProfile]=[]
for c in self.getCollections() {
collections.append(CollectionProfile(c))
}
return UserProfile(
findName: self.getFindName(),
address: self.owner!.address,
name: self.getName(),
gender: self.getGender(),
description: self.getDescription(),
tags: self.getTags(),
avatar: self.getAvatar(),
links: self.getLinks(),
wallets: wallets,
collections: collections,
following: self.getFollowing(),
followers: self.getFollowers(),
allowStoringFollowers: self.allowStoringFollowers,
createdAt:self.getCreatedAt()
)
}
pub fun getLinksMap() : {String: Link} {
return self.links
}
pub fun getLinks() : [Link] {
return self.links.values
}
pub fun addLinkWithName(name:String, link:Link) {
self.links[name]=link
}
pub fun addLink(_ val: Link) {
self.links[val.title]=val
}
pub fun removeLink(_ val: String) {
self.links.remove(key: val)
}
pub fun supportedFungigleTokenTypes() : [Type] {
let types: [Type] =[]
for w in self.wallets {
if !types.contains(w.accept) {
types.append(w.accept)
}
}
return types
}
pub fun deposit(from: @FungibleToken.Vault) {
let walletIndexCache = ProfileCache.getWalletIndex(address: self.owner!.address, walletType: from.getType())
if walletIndexCache != nil {
let ref = self.wallets[walletIndexCache!].receiver.borrow() ?? panic("This vault is not set up. ".concat(from.getType().identifier).concat(self.owner!.address.toString()).concat(" . ").concat(from.balance.toString()))
ref.deposit(from: <- from)
return
}
for i , w in self.wallets {
if from.isInstance(w.accept) {
ProfileCache.setWalletIndexCache(address: self.owner!.address, walletType: from.getType(), index: i)
let ref = w.receiver.borrow() ?? panic("This vault is not set up. ".concat(from.getType().identifier).concat(self.owner!.address.toString()).concat(" . ").concat(from.balance.toString()))
ref.deposit(from: <- from)
return
}
}
let identifier=from.getType().identifier
// Try borrow that in a standard way. Only work for flow, usdc and fusd
// Check the vault type
var ref : &{FungibleToken.Receiver}? = nil
if FindUtils.contains(identifier, element: "FlowToken.Vault") {
ref = self.owner!.getCapability<&{FungibleToken.Receiver}>(/public/flowTokenReceiver).borrow()
} else if FindUtils.contains(identifier, element: "FiatToken.Vault") {
ref = self.owner!.getCapability<&{FungibleToken.Receiver}>(/public/USDCVaultReceiver).borrow()
} else if FindUtils.contains(identifier, element: "FUSD.Vault") {
ref = self.owner!.getCapability<&{FungibleToken.Receiver}>(/public/fusdReceiver).borrow()
} else if FindUtils.contains(identifier, element: "FlowUtilityToken.Vault") {
ref = self.owner!.getCapability<&{FungibleToken.Receiver}>(/public/flowUtilityTokenReceiver).borrow()
} else if FindUtils.contains(identifier, element: "DapperUtilityCoin.Vault") {
ref = self.owner!.getCapability<&{FungibleToken.Receiver}>(/public/dapperUtilityCoinReceiver).borrow()
}
if ref != nil {
ref!.deposit(from: <- from)
return
}
//I need to destroy here for this to compile, but WHY?
// oh we dont neet this anymore
// destroy from
panic("could not find a supported wallet for:".concat(identifier).concat(" for address ").concat(self.owner!.address.toString()))
}
pub fun hasWallet(_ name: String) : Bool {
for wallet in self.wallets {
if wallet.name == name || wallet.accept.identifier == name {
return wallet.receiver.check()
}
}
return false
}
pub fun getWallets() : [Wallet] { return self.wallets}
pub fun addWallet(_ val: Wallet) { self.wallets.append(val) }
pub fun removeWallet(_ val: String) {
let numWallets=self.wallets.length
var i=0
while(i < numWallets) {
if self.wallets[i].name== val {
self.wallets.remove(at: i)
ProfileCache.resetWalletIndexCache(address: self.owner!.address)
return
}
i=i+1
}
}
pub fun setWallets(_ val: [Wallet]) {
self.wallets=val
ProfileCache.resetWalletIndexCache(address: self.owner!.address)
}
pub fun removeFollower(_ val: Address) {
self.followers.remove(key:val)
}
pub fun follows(_ address: Address) : Bool {
return self.following.containsKey(address)
}
pub fun getName(): String { return self.name }
pub fun getFindName(): String { return self.findName }
pub fun getCreatedAt(): String { return self.createdAt }
pub fun getGender() : String { return self.gender }
pub fun getDescription(): String{ return self.description}
pub fun getTags(): [String] { return self.tags}
pub fun getAvatar(): String { return self.avatar }
pub fun getFollowers(): [FriendStatus] { return self.followers.values }
pub fun getFollowing(): [FriendStatus] { return self.following.values }
pub fun setName(_ val: String) { self.name = val }
pub fun setFindName(_ val: String) {
emit Updated(account:self.owner!.address, userName:self.name, findName:val, thumbnail:self.avatar)
ProfileCache.resetLeaseCache(address: self.owner!.address, leaseName: self.findName)
self.findName = val
}
pub fun setGender(_ val: String) { self.gender = val }
pub fun setAvatar(_ val: String) { self.avatar = val }
pub fun setDescription(_ val: String) { self.description=val}
pub fun setTags(_ val: [String]) { self.tags=val}
pub fun removeCollection(_ val: String) { self.collections.remove(key: val)}
pub fun addCollection(_ val: ResourceCollection) { self.collections[val.name]=val}
pub fun getCollections(): [ResourceCollection] { return self.collections.values}
pub fun follow(_ address: Address, tags:[String]) {
let friendProfile=Profile.find(address)
let owner=self.owner!.address
let status=FriendStatus(follower:owner, following:address, tags:tags)
self.following[address] = status
friendProfile.internal_addFollower(status)
emit Follow(follower:owner, following: address, tags:tags)
}
pub fun unfollow(_ address: Address) {
self.following.remove(key: address)
Profile.find(address).internal_removeFollower(self.owner!.address)
emit Unfollow(follower: self.owner!.address, unfollowing:address)
}
access(contract) fun internal_addFollower(_ val: FriendStatus) {
if self.allowStoringFollowers && !self.bans.containsKey(val.follower) {
self.followers[val.follower] = val
}
}
access(contract) fun internal_removeFollower(_ address: Address) {
if self.followers.containsKey(address) {
self.followers.remove(key: address)
}
}
}
pub fun findReceiverCapability(address: Address, path: PublicPath, type: Type) : Capability<&{FungibleToken.Receiver}>? {
let profileCap = self.findWalletCapability(address)
if profileCap.check() {
if let profile = getAccount(address).getCapability<&Profile.User{Profile.Public}>(Profile.publicPath).borrow() {
if profile.hasWallet(type.identifier) {
return profileCap
}
}
}
let cap = getAccount(address).getCapability<&{FungibleToken.Receiver}>(path)
if cap.check() {
return cap
}
return nil
}
pub fun findWalletCapability(_ address: Address) : Capability<&{FungibleToken.Receiver}> {
return getAccount(address)
.getCapability<&{FungibleToken.Receiver}>(Profile.publicReceiverPath)
}
pub fun find(_ address: Address) : &{Profile.Public} {
return getAccount(address)
.getCapability<&{Profile.Public}>(Profile.publicPath)
.borrow()!
}
pub fun createUser(name: String, createdAt:String) : @Profile.User {
if name.length > 64 {
panic("Name must be 64 or less characters")
}
if createdAt.length > 32 {
panic("createdAt must be 32 or less characters")
}
return <- create Profile.User(name: name,createdAt: createdAt)
}
pub fun verifyTags(tags : [String], tagLength: Int, tagSize: Int): Bool {
if tags.length > tagSize {
return false
}
for t in tags {
if t.length > tagLength {
return false
}
}
return true
}
init() {
self.publicPath = /public/findProfile
self.publicReceiverPath = /public/findProfileReceiver
self.storagePath = /storage/findProfile
}
}