-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProfile.cdc
More file actions
394 lines (323 loc) · 11.4 KB
/
Copy pathProfile.cdc
File metadata and controls
394 lines (323 loc) · 11.4 KB
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
/** Generic Profile Contract
License: MIT
I am trying to figure out a generic re-usable Profile Micro-Contract
that any application can consume and use. It should be easy to integrate
this contract with any application, and as a user moves from application
to application this profile can come with them. A core concept here is
given a Flow Address, a profiles details can be publically known. This
should mean that if an application were to use/store the Flow address of
a user, than this profile could be visible, and maintained with out storing
a copy in an applications own databases. I believe that anytime we can move
a common database table into a publically accessible contract/resource is a
win.
could be a little more than that too. As Flow Accounts can now have
multiple contracts, it could be fun to allow for these accounts to have
some basic information too. https://flow-view-source.com is a side project
of mine (qvvg) and if you are looking at an account on there, or a contract
deployed to an account I will make it so it pulls info from a properly
configured Profile Resource.
====================
## Table of Contents
====================
Line
Intro ......................................................... 1
Table of Contents ............................................. 27
General Profile Contract Info ................................. 41
Examples ...................................................... 50
Initializing a Profile Resource ............................. 59
Interacting with Profile Resource (as Owner) ................ 112
Reading a Profile Given a Flow Address ...................... 160
Reading a Multiple Profiles Given Multiple Flow Addresses ... 192
Checking if Flow Account is Initialized ..................... 225
================================
## General Profile Contract Info
================================
Currently a profile consists of a couple main pieces:
- name – An alias the profile owner would like to be refered as.
- avatar - An href the profile owner would like applications to use to represent them graphically.
- color - A valid html color (not verified in any way) applications can use to accent and personalize the experience.
- info - A short description about the account.
===========
## Examples
===========
The following examples will include both raw cadence transactions and scripts
as well as how you can call them from FCL. The FCL examples are currently assuming
the following configuration is called somewhere in your application before the
the actual calls to the chain are invoked.
==================================
## Initializing a Profile Resource
==================================
Initializing should be done using the paths that the contract exposes.
This will lead to predictability in how applications can look up the data.
-----------
### Cadence
-----------
import Profile from 0xba1132bc08f82fe2
transaction {
let address: address
prepare(currentUser: auth(SaveValue, PublishCapability, IssueStorageCapabilityController) &Account) {
self.address = currentUser.address
if !Profile.check(self.address) {
let profileCapability = currentUser.capabilities.storage.issue<&{Profile.Public}>(Profile.privatePath)
currentUser.capabilities.publish(profileCapability, at: Profile.publicPath)
}
}
post {
Profile.check(self.address): "Account was not initialized"
}
}
-------
### FCL
-------
import {query} from "@onflow/fcl"
await mutate({
cadence: `
import Profile from 0xba1132bc08f82fe2
transaction {
prepare(currentUser: auth(SaveValue, PublishCapability, IssueStorageCapabilityController) &Account) {
self.address = currentUser.address
if !Profile.check(self.address) {
currentUser.save(<- Profile.new(), to: Profile.privatePath)
let profileCapability = currentUser.capabilities.storage.issue<&{Profile.Public}>(Profile.privatePath)
currentUser.capabilities.publish(profileCapability, at: Profile.publicPath)
}
}
post {
Profile.check(self.address): "Account was not initialized"
}
}
`,
limit: 55,
})
===============================================
## Interacting with Profile Resource (as Owner)
===============================================
As the owner of a resource you can update the following:
- name using `.setName("MyNewName")` (as long as you arent verified)
- avatar using `.setAvatar("https://url.to.my.avatar")`
- color using `.setColor("tomato")`
- info using `.setInfo("I like to make things with Flow :wave:")`
-----------
### Cadence
-----------
import Profile from 0xba1132bc08f82fe2
transaction(name: String) {
prepare(currentUser: auth(BorrowValue) &Account) {
currentUser
.storage
.borrow<&{Profile.Owner}>(from: Profile.privatePath)!
.setName(name)
}
}
-------
### FCL
-------
import {mutate} from "@onflow/fcl"
await mutate({
cadence: `
import Profile from 0xba1132bc08f82fe2
transaction(name: String) {
prepare(currentUser: auth(BorrowValue) &Account) {
currentUser
.storage
.borrow<&{Profile.Owner}>(from: Profile.privatePath)!
.setName(name)
}
}
`,
args: (arg, t) => [
arg("qvvg", t.String),
],
limit: 55,
})
=========================================
## Reading a Profile Given a Flow Address
=========================================
-----------
### Cadence
-----------
import Profile from 0xba1132bc08f82fe2
access(all) fun main(address: Address): Profile.ReadOnly? {
return Profile.read(address)
}
-------
### FCL
-------
import {query} from "@onflow/fcl"
await query({
cadence: `
import Profile from 0xba1132bc08f82fe2
access(all) fun main(address: Address): Profile.ReadOnly? {
return Profile.read(address)
}
`,
args: (arg, t) => [
arg("0xba1132bc08f82fe2", t.Address)
]
})
============================================================
## Reading a Multiple Profiles Given Multiple Flow Addresses
============================================================
-----------
### Cadence
-----------
import Profile from 0xba1132bc08f82fe2
access(all) fun main(addresses: [Address]): {Address: Profile.ReadOnly} {
return Profile.readMultiple(addresses)
}
-------
### FCL
-------
import {query} from "@onflow/fcl"
await query({
cadence: `
import Profile from 0xba1132bc08f82fe2
access(all) fun main(addresses: [Address]): {Address: Profile.ReadOnly} {
return Profile.readMultiple(addresses)
}
`,
args: (arg, t) => [
arg(["0xba1132bc08f82fe2", "0xf76a4c54f0f75ce4", "0xf117a8efa34ffd58"], t.Array(t.Address)),
]
})
==========================================
## Checking if Flow Account is Initialized
==========================================
-----------
### Cadence
-----------
import Profile from 0xba1132bc08f82fe2
access(all) fun main(address: Address): Bool {
return Profile.check(address)
}
-------
### FCL
-------
import {query} from "@onflow/fcl"
await query({
cadence: `
import Profile from 0xba1132bc08f82fe2
access(all) fun main(address: Address): Bool {
return Profile.check(address)
}
`,
args: (arg, t) => [
arg("0xba1132bc08f82fe2", t.Address)
]
})
*/
access(all) contract Profile {
access(all) let publicPath: PublicPath
access(all) let privatePath: StoragePath
access(all) resource interface Public {
access(all) fun getName(): String
access(all) fun getAvatar(): String
access(all) fun getColor(): String
access(all) fun getInfo(): String
access(all) fun asReadOnly(): Profile.ReadOnly
}
access(all) resource interface Owner {
access(all) fun getName(): String
access(all) fun getAvatar(): String
access(all) fun getColor(): String
access(all) fun getInfo(): String
access(all) fun setName(_ name: String) {
pre {
name.length <= 15: "Names must be under 15 characters long."
}
}
access(all) fun setAvatar(_ src: String)
access(all) fun setColor(_ color: String)
access(all) fun setInfo(_ info: String) {
pre {
info.length <= 280: "Profile Info can at max be 280 characters long."
}
}
}
access(all) resource Base: Owner, Public {
access(self) var name: String
access(self) var avatar: String
access(self) var color: String
access(self) var info: String
init() {
self.name = "Anon"
self.avatar = ""
self.color = "#232323"
self.info = ""
}
access(all) fun getName(): String { return self.name }
access(all) fun getAvatar(): String { return self.avatar }
access(all) fun getColor(): String {return self.color }
access(all) fun getInfo(): String { return self.info }
access(all) fun setName(_ name: String) { self.name = name }
access(all) fun setAvatar(_ src: String) { self.avatar = src }
access(all) fun setColor(_ color: String) { self.color = color }
access(all) fun setInfo(_ info: String) { self.info = info }
access(all) fun asReadOnly(): Profile.ReadOnly {
return Profile.ReadOnly(
address: self.owner?.address,
name: self.getName(),
avatar: self.getAvatar(),
color: self.getColor(),
info: self.getInfo()
)
}
}
access(all) struct ReadOnly {
access(all) let address: Address?
access(all) let name: String
access(all) let avatar: String
access(all) let color: String
access(all) let info: String
init(address: Address?, name: String, avatar: String, color: String, info: String) {
self.address = address
self.name = name
self.avatar = avatar
self.color = color
self.info = info
}
}
access(all) fun new(): @Profile.Base {
return <- create Base()
}
access(all) fun check(_ address: Address): Bool {
return getAccount(address)
.capabilities
.get<&{Profile.Public}>(Profile.publicPath)
.check()
}
access(all) fun fetch(_ address: Address): &{Profile.Public} {
return getAccount(address)
.capabilities
.get<&{Profile.Public}>(Profile.publicPath)
.borrow()!
}
access(all) fun read(_ address: Address): Profile.ReadOnly? {
if let profile = getAccount(address).capabilities.get<&{Profile.Public}>(Profile.publicPath).borrow() {
return profile.asReadOnly()
} else {
return nil
}
}
access(all) fun readMultiple(_ addresses: [Address]): {Address: Profile.ReadOnly} {
let profiles: {Address: Profile.ReadOnly} = {}
for address in addresses {
let profile = Profile.read(address)
if profile != nil {
profiles[address] = profile!
}
}
return profiles
}
init() {
self.publicPath = /public/profile
self.privatePath = /storage/profile
self.account.storage.save(<- self.new(), to: self.privatePath)
let profileCapability = self.account.capabilities.storage.issue<&{Public}>(self.privatePath)
self.account.capabilities.publish(profileCapability, at: self.publicPath)
self.account
.storage
.borrow<&{Owner}>(from: self.privatePath)!
.setName("qvvg")
}
}