You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial builds on your understanding of [accounts] and [resources]. You'll learn how to interact with resources using [capabilities] and [entitlements].
24
21
25
22
:::tip[Reminder]
26
23
27
-
In Cadence, resources are a composite type like a struct or a class, but with some **special rules**:
24
+
In Cadence, resources are a composite type like a `struct` or a class in other languages, but with some **special rules**:
28
25
29
26
- Each instance of a resource can only exist in exactly one location and cannot be copied.
30
27
- Resources must be explicitly moved from one location to another when accessed.
@@ -48,12 +45,14 @@ If you're working on an app that allows users to exchange tokens, you'll want di
48
45
49
46
:::info
50
47
51
-
In Cadence, users have complete control over their storage, and their storage is tied directly to their accounts. This feature allows amazing benefits including peer-to-peer transfers of property and it being impossible to accidentally burn an asset by sending it to an unused address. The one mixed blessing is that you can't airdrop tokens or NFTs without the recipient signing a transaction. Less spam, but you'll need to use a claim mechanism if the recipient doesn't already have a vault for your asset.
48
+
In Cadence, users have complete control over their storage, and their storage is tied directly to their accounts. This feature allows amazing benefits including peer-to-peer transfers of property and it being impossible to accidentally burn an asset by sending it to an unused address. The one mixed blessing is that you can't airdrop tokens or NFTs without the recipient signing a transaction. Less spam, but you'll need to use a claim mechanism if the recipient doesn't already have a vault for your asset.
52
49
53
50
:::
54
51
55
52
Capabilities and entitlements are what allows for this detailed control of access to owned assets. They allow a user to indicate which of the functionality of their account and owned objects should be accessible to themselves, their trusted friends, and the public.
56
53
54
+

55
+
57
56
For example, a user might want to allow a friend of theirs to use some of their money to spend. In this case, they could create an entitled capability that gives the friend access to only this part of their account, instead of having to hand over full control.
58
57
59
58
Another example is when a user authenticates a trading app for the first time, the trading app could ask the user for a capability object that allows the app to access the trading functionality of a user's account so that the app doesn't need to ask the user for a signature every time it wants to do a trade. The user can choose to empower the app, and that app alone, for this functionality and this functionality alone.
@@ -68,7 +67,7 @@ Next, you'll write a script that anyone can use that links to borrow a [referenc
68
67
69
68
## Creating capabilities and references to stored resources
70
69
71
-
Continue working with your code from the previous tutorial. Alternately, open a fresh copy here: [play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6].
70
+
Continue working with your code from the previous tutorial. Alternately, open a fresh copy here: [https://play.flow.com/8b28da4e-0235-499f-8653-1f55e1b3b725].
72
71
73
72
If you started with the playground linked above, be sure to deploy the `HelloResource` contract with account `0x06` and call the `Create Hello` transaction, also with `0x06`.
74
73
@@ -78,20 +77,24 @@ To prepare:
78
77
79
78
1. Create a new transaction called `Create Link`.
80
79
1. Import `HelloResource` and stub out a `transaction` with a `prepare` phase.
80
+
81
81
- Cadence allows for static analysis of imported contracts. You'll get errors in the transactions and scripts that import `HelloResource` from `0x06` if you haven't deployed that contract.
82
+
82
83
```cadence create_link.cdc
83
84
import HelloResource from 0x06
84
-
85
+
85
86
transaction {
86
87
prepare() {
87
88
// TODO
88
89
}
89
90
}
90
91
```
92
+
91
93
1. Pass an `&Account` reference into `prepare` with the capabilities needed to give the `transaction` the ability to create and publish a capability:
94
+
92
95
```cadence create_link.cdc
93
96
import HelloResource from 0x06
94
-
97
+
95
98
transaction {
96
99
prepare(account: auth(
97
100
IssueStorageCapabilityController,
@@ -210,40 +213,46 @@ Now that you've published the capability with `public` `access`, **anyone** who
210
213
211
214
1. Create a script called `GetGreeting`.
212
215
1. Import `HelloResource` and give it public `access`. To avoid syntax errors while writing the function, you may wish to add a temporary and obvious `return` value:
216
+
213
217
```cadence GetGreeting.cdc
214
218
import HelloResource from 0x06
215
-
219
+
216
220
access(all) fun main(): String {
217
221
// TODO
218
222
return "TODO";
219
223
}
220
224
```
225
+
221
226
- You'll need a reference to the public account object for the `0x06` account to be able to access public capabilities within it.
227
+
222
228
1. Use `getAccount` to get a reference to account `0x06`. Hardcode it for now:
223
229
```cadence
224
230
let helloAccount = getAccount(0x06)
225
231
```
226
232
- Addresses are **not** strings and thus do **not** have quotes around them.
227
233
1. Use `borrow` to borrow the public capability for your `Create Link` transaction saved in `/public/HelloAssetTutorial`.
234
+
228
235
- Your script should return `helloReference.hello()`.
229
236
- You've already borrowed something before. Try to implement this on your own. **Hint:** this time, you're borrowing a `capability` from the account, **not** something from `storage`. Don't forget to handle the case where the object can't be found!
230
237
<dl><dd><em>You should end up with a script similar to:</em></dd></dl>
?? panic("Could not borrow a reference to the HelloAsset capability")
241
-
249
+
242
250
return helloReference.hello()
243
251
}
244
252
```
253
+
245
254
1. Use `Execute` to execute your script.
246
-
<dl><dd><em>You'll see `"Hello, World!"` logged to the console.</em></dd></dl>
255
+
<dl><dd><em>You'll see `"Hello, World!"` logged to the console.</em></dd></dl>
247
256
248
257
Note that scripts don't need any authorization and can only access public information. You've enabled the user to make this capability public through the transaction you wrote and they signed. **Anyone** can write their own scripts to interact with your contracts this way!
249
258
@@ -309,4 +318,4 @@ Reference solutions are functional, but may not be optimal.
309
318
[Cadence Best Practices document]: ../design-patterns.md
0 commit comments