Previous << 2. Hello World
Next >> 4. Capability Tutorial
Overview
ð¡ TIP
ãã®ãã¥ãŒããªã¢ã«ã®ã¹ã¿ãŒã¿ãŒã³ãŒãã Flow Playground ã§éããŸãã
https://play.flow.com/ddf0177e-81c8-4512-ac2e-28036b1a3f89
ãã¥ãŒããªã¢ã«ã§ã¯ããã®ã³ãŒããšããåãããããã«ããŸããŸãªæäœãè¡ãããæ±ããããŸãã
ACTION
ãŠãŒã¶ãŒã«ã¢ã¯ã·ã§ã³ãèŠæ±ããæ瀺ã¯ãåžžã«ãã®å¹ãåºãããã¯ã¹å
ã«è¡šç€ºãããŸãããã€ã©ã€ããããã¢ã¯ã·ã§ã³ã¯ãã³ãŒããå®è¡ããããã«å¿
èŠãªãã¹ãŠã§ãããèšèªã®èšèšãç解ããã«ã¯ãæ®ãã®éšåãèªãããšãå¿
èŠã§ãã
ãã®ãã¥ãŒããªã¢ã«ã¯ãååã®Hello World
ãã¥ãŒããªã¢ã«ãåºã«ããŠããŸãããã®ãã¥ãŒããªã¢ã«ãéå§ããåã«ã次ã®å
容ãç解ããŠããå¿
èŠããããŸãã
- Accounts
- Transactions
- Signers
- Field Types
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ã¢ã«ãŠã³ãã«ã€ããŠç解ãæ·±ãã€ã€ãResourceã玹ä»ããããã«ãã£ãŠã¢ã«ãŠã³ããšã©ããããšãããã®ããç¿åŸããŠãããŸãã
ãªãœãŒã¹ã¯ãCadenceã®æãéèŠãªæ©èœã®1ã€ã§ãã
Cadenceã§ã¯ããªãœãŒã¹ã¯ä»ã®èšèªã«ãããæ§é äœãã¯ã©ã¹ã«äŒŒãè€ååã®äžã€ã§ãããããã€ãã®ç¹å¥ãªã«ãŒã«ãæã£ãŠããŸãã
以äžã«ããªãœãŒã¹ã®å®£èšäŸã瀺ããŸãã
access(all) resource Money {
access(all) let balance: Int
init() {
self.balance = 0
}
}
ã芧äžããããªãœãŒã¹ã¯éåžžã®struct
å®çŸ©ã¿ããã«èŠããŸãããã®éãã¯åäœå
ã«ãããŸãã
Resourceã¯ãã¢ã»ããåã³ãªããžã§ã¯ãã®çŽæ¥ææ(direct ownership)ãã¢ãã«åãããå Žåã«äŸ¿å©ã§ããçŽæ¥ææã«ãããã¢ã¯ã»ã¹ãå¯èœã«ãããã¹ã¯ãŒãã蚌ææžã®ä»£ããã«ããªãã®è³ç£ãè¡šãå®éã®ãªããžã§ã¯ããææã§ããããšãæå³ããŸããä»ã®ããã°ã©ãã³ã°èšèªã®åŸæ¥ã®æ§é äœãã¯ã©ã¹ã¯ãã³ããŒãå¯èœã§ãããããçŽæ¥ææãè¡šãçæ³çãªæ¹æ³ã«ã¯ãªããŸãããã³ãŒãã£ã³ã°ãšã©ãŒã«ãã£ãŠåãè³ç£ã®è€æ°ã³ããŒãç°¡åã«äœæããããšãåºæ¥ããšãè³ç£ã®äŸ¡å€ãæãããããã®åžå°æ§ãå°ç¡ãã«ããããã§ããç§ãã¡ã¯ã家å±ãèªåè»ãæ°çŸäžãã«ã®éè¡å£åº§ããããã¯éŠ¬ãªã©ã®èŠæš¡ã§ã®æ倱ãçé£ãèæ ®ããªããã°ãªããŸããããªãœãŒã¹ã¯ãã®ç¹ã§ãè³ç£ãæ瀺çã«äœæãç Žæ£ã移åãããããšã§ãã®åé¡ã解決ãããŸãã
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ä»¥äžã®ããšãè¡ããŸãã
- ãªãœãŒã¹ã宣èšããã¹ããŒãã³ã³ãã©ã¯ãããããã€ããŸã
- ãªãœãŒã¹ãã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžã«ä¿åããŸã
- ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããŠäœæãããªãœãŒã¹ã«å¯ŸããããšãããŸã
Implementing a Contract with Resources
ãªãœãŒã¹ã«å¯ŸããŠããåãããã«ã¯ãããã€ãã®éèŠãªæŠå¿µãåŠãã§ãã ããã
-
create
ããŒã¯ãŒãã®äœ¿çš -
<-
移åæŒç®å - ã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPI
ãŸããcreate
ããŒã¯ãŒããš<-
移åæŒç®åã䜿ã£ãŠã©ããã£ãŠãªãœãŒã¹ãäœæããŠããããèŠãŠãããŸãããã
create
ããŒã¯ãŒãã䜿çšããŠãªãœãŒã¹ãåæåããŸãããªãœãŒã¹ã¯ãããå®çŸ©ããã¹ããŒãã³ã³ãã©ã¯ãã®äžã§ã®ã¿äœæã§ããŸãã
<-
移åæŒç®åã¯ããªãœãŒã¹ãå€æ°ã«ç§»åãããããã«äœ¿çšããŸãããªãœãŒã¹ã«ã¯ä»£å
¥æŒç®å=
ã䜿çšã§ããªãããããªãœãŒã¹ãåæåããããæ°ããå€æ°ã«ä»£å
¥ããæã«ã¯ã<-
移åæŒç®åã䜿çšããå¿
èŠããããŸããããã¯ããªãœãŒã¹ãæåéãããå Žæããå¥ã®å Žæã«ç§»åããããšãæå³ããŸãããã®ãªãœãŒã¹ãä¿æããŠããå€ãå€æ°ãŸãã¯å Žæã¯ç§»ååŸãç¡å¹ãªå€ã«ãªããŸãããããCadenceããªãœãŒã¹ã1å Žæã«ããåæã«ååšãããªãããšãä¿èšŒããæ段ã®1ã€ã§ãã
ACTION
ãã¡ã€ã«åãHelloWorldResource.cdc
ã§ããã¢ã«ãŠã³ã0x06
ã¿ããéããŸãã
HelloWorldResource.cdc
ã«ã¯ã以äžã®ã³ãŒããå«ãŸããŠããã¯ãã§ãã
access(all) contract HelloWorld {
/* Declare a resource that only includes one function. */
access(all) resource HelloAsset {
/* A transaction can call this function to get the "Hello, World!"
message from the resource. */
access(all) view fun hello(): String {
return "Hello, World!"
}
}
/* We're going to use the built-in create function to create a new instance
of the HelloAsset resource */
access(all) fun createHelloAsset(): @HelloAsset {
return <-create HelloAsset()
}
}
ACTION
Deployãã¿ã³ã䜿çšããŠããã®ã³ãŒããã¢ã«ãŠã³ã0x06
ã«ãããã€ããŸãã
ãŸãç§éã¯ã0x06
ã¢ã«ãŠã³ãã«æ°ããHelloWorld
ã³ã³ãã©ã¯ãã宣èšããŸãããã®HelloWorld
ã³ã³ãã©ã¯ãã§ç§ãã¡ã¯ä»¥äžãããŠããŸãã
-
HelloAsset
ãªãœãŒã¹ããããªãã¯ã¹ã³ãŒãã§ããaccess(all)
ã§å®£èšããŸãã -
hello()
ãªãœãŒã¹é¢æ°ãHelloAsset
ã®äžã«ããããªãã¯ã¹ã³ãŒãã§ããaccess(all)
ã§å®£èšããŸãã -
HelloAsset
ãªãœãŒã¹ãcreate
ããcreateHelloAsset()
ã³ã³ãã©ã¯ãé¢æ°ã宣èšããŸãã -
createHelloAsset()
é¢æ°ã¯ãªãœãŒã¹ãé¢æ°ãè¿ãããã«ã移åæŒç®åïŒ<-
ïŒã䜿çšããŸãã
ããã¯ãã¹ããŒãã³ã³ãã©ã¯ãã§å®è¡ã§ããããšã®äžäŸã§ããCadenceã¯ããããã€ãããã¹ããŒãã³ã³ãã©ã¯ãå ã§åã®å®çŸ©ã宣èšã§ããŸãã(è£è¶³: Cadenceã§ã¯äžã€äžã€ã®ãªãœãŒã¹ãäžã€äžã€ã®åã«ãªããŸãã)åã®å®çŸ©ã¯ãç¹å®ã®ããŒã¿ã»ãããã©ã®ããã«æ§æãããŠãããã説æãããã®ã§ããåã®å®çŸ©èªäœã¯ããã®ããŒã¿ã®ã³ããŒãã€ã³ã¹ã¿ã³ã¹ã§ã¯ãããŸãããä»ã®ã¢ã«ãŠã³ãã¯ãããã®å®çŸ©ãã€ã³ããŒããããããã®åã®ãªããžã§ã¯ããšããåãããããšãã§ããŸãã
ç§éãå
ã»ã©ãããã€ãããã®ã¹ããŒãã³ã³ãã©ã¯ãã§ã¯ãHelloAsset
ãªãœãŒã¹ã®å®çŸ©ãšãªãœãŒã¹ãäœæããé¢æ°ã®å®£èšãããŠããŸãã
ãã®ã¹ããŒãã³ã³ãã©ã¯ãã«ã€ããŠããªãœãŒã¹éšåãã詳ããèŠãŠãããŸãããããªãœãŒã¹ãšã¯ãCadenceãã¹ããŒãã³ã³ãã©ã¯ãèšèšã«å°å ¥ããæãéèŠãªãã®ã®1ã€ã§ãã
access(all)
resource HelloAsset {
access(all)
view fun hello(): String {
return "Hello, World!"
}
}
Resources
resource
ãšãstruct
ãŸãã¯class
ã®äž»ãªéãã¯ããªãœãŒã¹ã®ã¢ã¯ã»ã¹ç¯å²ã§ãã
- resourceã®åã€ã³ã¹ã¿ã³ã¹ã¯ãå³å¯ã«1ã€ã®å Žæã«ã®ã¿ååšããããšãã§ããã³ããŒããããšã¯ã§ããŸãããããã§ããå Žæãšã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžãé¢æ°å ã®äžæå€æ°ãã¹ããŒãã³ã³ãã©ã¯ãå ã®ã¹ãã¬ãŒãž ãã£ãŒã«ããªã©ãæããŸãã
- resourceã¯ãã¢ã¯ã»ã¹ããããšãæ瀺çã«1ã€ã®å Žæããå¥ã®å Žæã«ç§»åãããªããã°ãªããŸããã
- ãŸããresourceã¯é¢æ°å®è¡ã®çµäºæã«ã¹ã³ãŒãå€ã«åºãããšãã§ããŸãããæ瀺çã«ã©ããã«ä¿åããããæ瀺çã«ç Žæ£ããªããã°ãªããŸããã
ãããã®ç¹æ§ã«ãããã³ãŒãã£ã³ã°ã®ãã¹ãããªãœãŒã¹ã誀ã£ãŠå€±ãããšãäžå¯èœã«ãªããŸãã
resourceã¯ãresourceãå®çŸ©ãããã¹ã³ãŒã(è£è¶³: ã¹ããŒãã³ã³ãã©ã¯ã)å ã§ã®ã¿äœæã§ããŸãã
ããã«ãããä»ã®ãŠãŒã¶ãŒãå®çŸ©ãããªãœãŒã¹ãªããžã§ã¯ããã誰ã§ãä»»æã®æ°ã ãäœæã§ããããšãé²æ¢ã§ããŸãã
The Move Operator (<-)
ä»åã®äŸã§ã¯ãHelloAsset
ãªãœãŒã¹ãäœæã§ããé¢æ°ã宣èšããŸããã
access(all)
fun createHelloAsset(): @HelloAsset {
return <-create HelloAsset()
}
@
ã·ã³ãã«ã¯ããããã¹ããŒãã³ã³ãã©ã¯ãå
ã§å®çŸ©ããHelloAsset
åã®ãªãœãŒã¹ã§ããããšãæã瀺ããŠããŸãããã®é¢æ°ã§ã¯ãHelloAsset
åã®ãªãœãŒã¹ãäœæããããè¿ãããã«ã移åæŒç®åã䜿çšããŠããŸããæ°ãããªãœãŒã¹ãªããžã§ã¯ããäœæããããã«ã¯ãcreate
ããŒã¯ãŒãã䜿çšããŸãã
ããã§ã<-
èšå·ã䜿çšããŠããŸããããã¯ç§»åæŒç®åã§ãã<-
移åæŒç®åã¯ããªãœãŒã¹ã«ããã代å
¥ã®ä»£å
¥æŒç®å=
ã眮ãæããŸãããªãœãŒã¹ã®ä»£å
¥ã§ããããšæ瀺ããå¿
èŠãããããã以äžã®åŠçãè¡ãæã«ã¯<-
移åæŒç®åã䜿çšããå¿
èŠããããŸãã
- ãªãœãŒã¹ãå®æ°ãŸãã¯å€æ°ã®åæå€ãšããæ
- ãªãœãŒã¹ãä»£å ¥ã§å¥ã®å€æ°ã«ç§»åããæ
- ãªãœãŒã¹ãåŒæ°ãšããŠé¢æ°ã«ç§»åããæ
- ãªãœãŒã¹ãé¢æ°ããè¿ãããæ
ãªãœãŒã¹ã移åãããšãå€ãå Žæã¯ç¡å¹ãšãªãããªãœãŒã¹ãªããžã§ã¯ãã¯æ°ããå Žæã®ã³ã³ããã¹ãã«ç§»åããŸãã
äŸãã°ãå€æ°first_resource
ã«ãªãœãŒã¹ããããšãããš:
/* Note the `@` symbol to specify that it is a resource */
var first_resource: @AnyResource <- create AnyResource()
ãããŠããããsecond_resource
ãšããæ°ããå€æ°ã«ä»£å
¥ããããšããŸãã代å
¥ãè¡ã£ãåŸãfirst_resource
ã¯ç¡å¹ãšãªããŸããããã¯ããªãœãŒã¹ãæ°ããå€æ°ã«ç§»åããããã§ãã
var second_resource <- first_resource
/* first_resource is now invalid. Nothing can be done with it */
ãªãœãŒã¹ãéåžžã®ä»£å
¥ããããšã¯èš±å¯ãããŠããŸãããéåžžã®ä»£å
¥ã¯å€ãã³ããŒããã ãã ããã§ãããªãœãŒã¹ã¯åæã«1ã€ã®å Žæã«ããååšã§ããªãããã移åã¯ç§»åæŒç®å<-
ã䜿çšããŠã³ãŒãå
ã§æ瀺ããŠç€ºãããå¿
èŠããããŸãã
Create Hello Transaction
ã§ã¯æ¬¡ã«ãcreateHelloAsset()
é¢æ°ãå®è¡ããHelloAsset
ãªãœãŒã¹ãã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžã«ä¿åãããã©ã³ã¶ã¯ã·ã§ã³åŠçãè¡ããŸãã
ACTION
Create Hello
ãšããååã®ãã©ã³ã¶ã¯ã·ã§ã³ãéããŸãã
Create Hello
ã«ã¯ä»¥äžã®ã³ãŒããå«ããŸãã
/* create_hello.cdc
This transaction calls the createHelloAsset() function from the contract
to create a resource, then saves the resource
in the signer's account storage using the "save" method. */
import HelloWorld from 0x06
transaction {
/* `auth(SaveValue) &Account` signifies an account object
that has the `SaveValue` authorization entitlement, which means
that this transaction can't do anything with the &Account object
besides saving values to storage.
You will learn more about entitlements later */
prepare(acct: auth(SaveValue) &Account) {
/* Here we create a resource and move it to the variable newHello,
then we save it in the signer's account storage */
let newHello <- HelloWorld.createHelloAsset()
acct.storage.save(<-newHello, to: /storage/HelloAssetTutorial)
}
/* In execute, we log a string to confirm that the transaction executed successfully. */
execute {
log("Saved Hello Resource to account.")
}
}
ãã®ãã©ã³ã¶ã¯ã·ã§ã³ã®åŠçã¯ä»¥äžã®éãã§ãã
-
HelloWorld
ã®å®çŸ©ãã¢ã«ãŠã³ã0x06
ããã€ã³ããŒãããŸãã -
createHelloAsset()
é¢æ°ã䜿çšããŠãªãœãŒã¹ãäœæãããããnewHello
ã«ç§»åããŸãã - äœæãããªãœãŒã¹ãããã©ã³ã¶ã¯ã·ã§ã³ã«çœ²åããã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžïŒ
/storage/HelloAssetTutorial
ã®ãã¹ïŒã«save
ããŸãã - ã
Saved Hello Resource to account.
ããšããããã¹ããã³ã³ãœãŒã«ã«ãã°åºåããŸãã
ããã¯ãprepare
ãã§ãŒãºã䜿çšããæåã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ããprepare
ãã§ãŒãºãšã¯ãã¢ã«ãŠã³ãåç
§ïŒ&AccountïŒãä»ããŠçœ²åãããã¢ã«ãŠã³ãã«ã¢ã¯ã»ã¹ãåºæ¥ããå¯äžã®å Žæã§ããã¢ã«ãŠã³ãåç
§ã¯ãã¢ã«ãŠã³ããšã®ããåãã«äœ¿çšåºæ¥ãããŸããŸãªã¡ãœããã«ã¢ã¯ã»ã¹ããããšãåºæ¥ãŸããäžäŸãšããŠãã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžããããŸããäžèšã®äŸã§ã¯ããã©ã³ã¶ã¯ã·ã§ã³ã¯ auth(SaveValue) &Account
ãšãããã®ã䜿çšããŠãããããã¯SaveValue
ãšããèªèšŒæš©é(authorization entitlement)ãæå³ããŠããŸãããã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ã&Account
ã¢ã«ãŠã³ãåç
§ãªããžã§ã¯ãã«å¯ŸããŠãã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžã«å€ãä¿åããããšã¯åºæ¥ããã©ããã以å€ã®æäœã¯ã§ããªããšããããšãæå³ããŠããŸããæš©é(Entitlement)ã«ã€ããŠã¯ãåŸã®ãã¥ãŒããªã¢ã«ã§è©³ãã説æããŸãã詳现ã«ã€ããŠã¯ãæš©éã«é¢ããããã¥ã¡ã³ã(the entitlements documentation)ãåç
§ããŠãã ããã
ãŸããå
šã¢ã«ãŠã³ãæš©é(Entitlement)ã«é¢ããå¯èœãªéãã®ããã¥ã¡ã³ãããèšèªãªãã¡ã¬ã³ã¹ã®Accountã»ã¯ã·ã§ã³ã§èŠãããšãåºæ¥ãŸãããã®ãã¥ãŒããªã¢ã«ã§ã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžã®/storage/
ãžã®ä¿åãããããããŒããè¡ãã¢ã«ãŠã³ãã®é¢æ°ã䜿çšããŠãããŸãã
ã¢ã«ãŠã³ãã¯ãªããžã§ã¯ãããã¹(paths)ã«ä¿åããŸãããã¹ã¯åºæ¬çã«ã¢ã«ãŠã³ãã®ãã¡ã€ã«ã·ã¹ãã ãè¡šãããŠãŒã¶ãŒå®çŸ©ã®ãã¹ã䜿ã£ãŠãã©ãã«ã§ããªããžã§ã¯ããä¿åããããšãã§ããŸããå€ãã®å Žåãã¹ããŒãã³ã³ãã©ã¯ãã¯ãã®ã¹ããŒãã³ã³ãã©ã¯ãã®ãªããžã§ã¯ããã©ãã«ä¿åããããããŠãŒã¶ãŒã«æ瀺ããŠããŸããããã«ãã£ãŠãã³ãŒãã¯æšæºçãªæ¹æ³ã§ãããã®ãªããžã§ã¯ãã«ã¢ã¯ã»ã¹ããæ¹æ³ãç¥ãããšãåºæ¥ãŸãã
executeãã§ãŒãºã«ãããã¢ã«ãŠã³ãã¹ãã¬ãŒãžãžã®ã¢ã¯ã»ã¹ãçŠæ¢ããŠããããšãããã³æš©é(Entitlement)ã®äœ¿çšã«ãã£ãŠãä»»æã®ãã©ã³ã¶ã¯ã·ã§ã³ã眲åè ã¢ã«ãŠã³ãã®ã©ã®ã¢ã»ãããé å/ãã¹(areas/path)ãå€æŽã§ããããéçã«æ€èšŒå¯èœã«ããŸãã ãã©ã³ã¶ã¯ã·ã§ã³ã®éä¿¡ãè¡ããã©ãŠã¶ãŠã©ã¬ãããã¢ããªã±ãŒã·ã§ã³ã¯ãããã䜿çšããŠããã©ã³ã¶ã¯ã·ã§ã³ãå€æŽããã§ãããå 容ããŠãŒã¶ãŒã«è¡šç€ºããããšãã§ããŸããããã«ããããŠã©ã¬ããããŠãŒã¶ãŒã®å®è¡ãããã©ã³ã¶ã¯ã·ã§ã³ã«é¢ããæ å ±ããŠãŒã¶ãŒã«æäŸããã¢ããªã±ãŒã·ã§ã³ããŠã©ã¬ããããæªæã®ããå±éºãªãã©ã³ã¶ã¯ã·ã§ã³ãéä¿¡ãããããšã¯ãªããšãã確信ãäžããããšãã§ããŸãã
ãã©ã³ã¶ã¯ã·ã§ã³ã«ã€ããŠãããå°ã詳ããèŠãŠãããŸããããHelloAsset
ãªãœãŒã¹ãäœæããããã«ãã¹ããŒãã³ã³ãã©ã¯ãã®createHelloAsset()
é¢æ°ã«ã¢ã¯ã»ã¹ããããã«ãã£ãŠäœæããããªãœãŒã¹ãnewHello
å€æ°ã«ç§»åãããŸãã
let newHello <- HelloWorld.createHelloAsset()
次ã«ããªãœãŒã¹ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžã«ä¿åããŸããFlowã§ã¢ã«ãŠã³ãã¹ãã¬ãŒãžãšããåãããã«ã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã䜿çšããŸãããªãœãŒã¹ãä¿åããã«ã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã®save()ã¡ãœããã䜿çšããŠãã¢ã«ãŠã³ãã®/storage/HelloAssetTutorial
ãã¹ã«ãªãœãŒã¹ãä¿åããŸãã
acct.storage.save(<-newHello, to: /storage/HelloAssetTutorial)
save
ã«å¯Ÿããæåã®ãã©ã¡ãŒã¿ã¯ãä¿åããããªããžã§ã¯ãã§ãããto
ãã©ã¡ãŒã¿ã¯ãªããžã§ã¯ããä¿åããããã¹ã§ãããã¹ã¯storageãã¹ã§ãªããã°ãªãããto
ãã©ã¡ãŒã¿ã§ã¯ãã¡ã€ã³ã¯/storage/
ã®ã¿ãèš±å¯ãããŸãã
æå®ãããã¹ã«ãã§ã«ãªããžã§ã¯ããä¿åãããŠããå Žåãããã°ã©ã ã¯äžæ¢(abort)ããŸããCadenceã®åã·ã¹ãã ã«ããããªãœãŒã¹ã誀ã£ãŠå€±ãããããšã¯æ±ºããŠãªãããšãèŠããŠãããŠãã ããããªãœãŒã¹ããã£ãŒã«ããé åããã£ã¯ã·ã§ããªããŸãã¯ã¹ãã¬ãŒãžã«ç§»åããå Žåããã®å Žæã«ãã§ã«ãªãœãŒã¹ãå«ãŸããŠããå¯èœæ§ããããŸããCadenceã§ã¯ãæ¢åã®ãªãœãŒã¹ãåŠçããããã«éçºè ã匷å¶ããäžæžãã«ãã£ãŠèª€ã£ãŠå€±ãããããšããªãããã«ããŠããŸãã
ãŸããããªããèå¥åãšããŠãã¹åããã§ã€ã¹ããæããããžã§ã¯ãã«åºæã§ãããã®ãéžã¶ããšãéåžžã«éèŠã§ããçŸåšãã¢ã«ãŠã³ãã®ä¿åãã¹ã¯ã°ããŒãã«ã§ããããããããžã§ã¯ããåãä¿åãã¹ã䜿çšããå¯èœæ§ãããããã¹ã競åããå¯èœæ§ããããŸããããã¯åä»ãªåé¡ãšãªãå¯èœæ§ãããããããã®ãããªåé¡ãé¿ããããã«åºæã®ãã¹åãéžæããŠãã ããã(è£è¶³: Cadenceã§ã¯ãªãœãŒã¹å®£èšèªäœãäžã€ã®åã§ããããåèªäœã¯éè€ããªããšèãããã)
æåŸã«ãexecuteãã§ãŒãºã§ã¯ã"Saved Hello Resource to account."
ãšããæèšãã³ã³ãœãŒã«ã«ãã°åºåããŸãã
log("Saved Hello Resource to account.")
ACTION
0x06
ã¢ã«ãŠã³ããå¯äžã®çœ²åè
ãšããŠéžæããŸããSend
ãã¿ã³ãã¯ãªãã¯ããŠããã©ã³ã¶ã¯ã·ã§ã³ãéä¿¡ããŸãã
以äžã®ãããªè¡šç€ºããããã¯ãã§ãã
"Saved Hello Resource to account."
ACTION
newHello
ãã¹ãã¬ãŒãžã«ä¿å(save)ããã³ãŒãè¡ã®åé€ãè©ŠããŠãããã§ãã
newHello
ã®ãšã©ãŒã衚瀺ãããã¯ãã§ãããã®ãšã©ãŒã«ã¯ãloss of resource
ããšè¡šç€ºãããŸããããã¯ããªãœãŒã¹ãé©åã«æ±ãããŠããªãããšãæå³ããŸããããã°ã©ã ã§ãã®ãšã©ãŒã衚瀺ãããå Žåãæ瀺çã«ä¿åãŸãã¯ç Žæ£ãããŠããªããªãœãŒã¹ãã©ããã«ããããšãæå³ããããã°ã©ã ãäžæ£ã§ããããšãæå³ããŸãã
ãã©ã³ã¶ã¯ã·ã§ã³ã®ãã§ãã¯ãé©åã«ãªãããã«ããããã«ãã³ãŒãè¡ãåè¿œå ããŸãã
ãã®å Žåãéžæããã¢ã«ãŠã³ãã§äœããä¿åããã®ã¯åããŠãªã®ã§ãã¹ãã¬ãŒãžã®/storage/HelloAssetTutorial
ã®å Žæã¯ç©ºã§ããããšãåãããŸããå®éã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ã誀ã£ãŠäžæžããããããšããŠãã©ã³ã¶ã¯ã·ã§ã³ãäžæ¢ããªãããã«ãä¿åããå Žæã®ãã¹ã§ãå¿
èŠãªãã§ãã¯ãšã¢ã¯ã·ã§ã³ãå®è¡ããã§ãããã
ãã©ã³ã¶ã¯ã·ã§ã³ãå®è¡ããã®ã§ãã¢ã«ãŠã³ã0x06
ã®ã¹ãã¬ãŒãžã«ã¯æ°ãã«äœæãããHelloWorld.HelloAsset
ãªãœãŒã¹ãæ ŒçŽãããŠããã¯ãã§ããããã確èªããã«ã¯ãå·Šäžã®ã¢ã«ãŠã³ã0x06
ãã¯ãªãã¯ããŸããããã«ãããã¢ã«ãŠã³ãå
ã®ããŸããŸãªã³ã³ãã©ã¯ããšãªããžã§ã¯ãã®ãã¥ãŒãéããŸããHelloWorld
ã³ã³ãã©ã¯ããšHelloAsset
ãªãœãŒã¹ã®ãšã³ããªã衚瀺ãããã¯ãã§ãã
Deployed Contracts:
[
{
"contract": "HelloWorld",
"height": 6
}
]
Account Storage:
{
"Private": null,
"Public": {},
"Storage": {
"HelloAssetTutorial": {
"Fields": [
39
],
"ResourceType": {
"Fields": [
{
"Identifier": "uuid",
"Type": {}
}
],
"Initializers": null,
"Location": {
"Address": "0x0000000000000005",
"Name": "HelloWorld",
"Type": "AddressLocation"
},
"QualifiedIdentifier": "HelloWorld.HelloAsset"
}
}
}
}
FlowToken
ãªããžã§ã¯ãã衚瀺ãããŠããã®ããããã§ãããããã¹ãŠã®ã¢ã«ãŠã³ãã¯ãFlowTokenã¢ã»ããã䜿çšããèœåãèªåçã«åæåãããŠããŸã(è£è¶³: FlowTokenã¯ãã©ã³ã¶ã¯ã·ã§ã³ãã£ãŒã«å¿
èŠãªã®ã§å
šãŠã®ã¢ã«ãŠã³ãã¯åæåæã«ãããä¿æããŸã)ãçŸæç¹ã§ã¯ããããã«ã€ããŠå¿é
ããå¿
èŠã¯ãããŸããã
Load Hello Transaction
次ã«ããã©ã³ã¶ã¯ã·ã§ã³ã§HelloAsset
ãªãœãŒã¹ã®hello()
ã¡ãœãããå®è¡ããŸãã
ACTION
Load Hello
ãšããååãã€ãããã©ã³ã¶ã¯ã·ã§ã³ãéããŸãã
Load Hello
ã«ã¯ã以äžã®ã³ãŒããå«ãŸããŠããã¯ãã§ãã
import HelloWorld from 0x06
/* This transaction calls the "hello" method on the HelloAsset object
that is stored in the account's storage by removing that object
from storage, calling the method, and then saving it back to the same storage path */
transaction {
/* In this prepare block, we have to load a value from storage
in addition to saving it, so we also need the `LoadValue` entitlement
which additionally allows loading values from storage */
prepare(acct: auth(LoadValue, SaveValue) &Account) {
/* Load the resource from storage, specifying the type to load it as
and the path where it is stored */
let helloResource <- acct.storage.load<@HelloWorld.HelloAsset>(from: /storage/HelloAssetTutorial)
?? panic("The signer does not have the HelloAsset resource stored at /storage/HelloAssetTutorial. Run the `Create Hello` Transaction again to store the resource")
/* log the hello world message */
log(helloResource.hello())
/* Put the resource back in storage at the same spot */
acct.storage.save(<-helloResource, to: /storage/HelloAssetTutorial)
}
}
ãã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ä»¥äžã®åŠçãè¡ããŸãã
-
HelloWorld
å®çŸ©ã0x06
ã¢ã«ãŠã³ãããã€ã³ããŒãããŸãã -
HelloAsset
ãªããžã§ã¯ãã移åæŒç®åãšã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã®load
é¢æ°ãçšããŠãã¹ãã¬ãŒãžããhelloResource
ã«ç§»åããŸãã -
helloResource
ã«ä¿åãããHelloAsset
ãªãœãŒã¹ã®hello()
é¢æ°ãåŒã³åºããçµæããã°ã«èšé²ããŸãã - ãªãœãŒã¹ããç§ãã¡ã移åãããåã«ãã£ãã¢ã«ãŠã³ãã®ãã¹
/storage/HelloAssetTutorial
ã«ä¿å(save)ãçŽããŸã
ç§éã¯prepare
ãã§ãŒãºãåã³äœ¿çšããæž¡ãããã¢ã«ãŠã³ãã®åç
§ã䜿ã£ãŠãªãœãŒã¹ãloadããŸãã
ãã©ã³ã¶ã¯ã·ã§ã³ã«ã€ããŠãããã«è©³ããèŠãŠãããŸãããã
HelloAsset
ãªãœãŒã¹ãã¹ãã¬ãŒãžããããŒãããŸãã
ã¹ãã¬ãŒãžãããªããžã§ã¯ããåãé€ãã«ã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã®load
ã¡ãœããã䜿çšããŸãã
let helloResource <- acct.storage.load<@HelloWorld.HelloAsset>(from: /storage/HelloAssetTutorial)
æå®ããåã®ãªããžã§ã¯ãããã¹ã«ååšããå Žåã¯ããã®é¢æ°ã¯ãã®ãªããžã§ã¯ããè¿ããŸãããããŠãã¢ã«ãŠã³ãã¹ãã¬ãŒãžã«ã¯ãæå®ããããã¹ä»¥äžã®ãªããžã§ã¯ãã¯å«ãŸããªããªããŸãã
ããŒããããªããžã§ã¯ãã®åã®ãåãã©ã¡ãŒã¿ã¯<>
ã«å«ãŸããŠããŸãã ããã§ãç§éã¯åºæ¬çã«@HelloWorld.HelloAsset
ã®ãªãœãŒã¹ãªããžã§ã¯ãããã®ãã¹ããããŒããããšããããšãè¿°ã¹ãŠããŸãã ãã©ã¡ãŒã¿ã«ããåã®åŒæ°ã¯æ瀺çã«æå®ããå¿
èŠããããŸãã (ãªãœãŒã¹ã§ããããšãæå³ãã@
èšå·ã«æ³šç®ããŠãã ãã)
from
ãã¹ã¯ã¹ãã¬ãŒãžãã¹ã§ãªããã°ãªããŸããããã®ããã/storage/
ãã¡ã€ã³ã®ã¿ãèš±å¯ãããŸãã
æå®ããåã®ãªããžã§ã¯ããæå®ãããã¹ã«ååšããªãå Žåããã®é¢æ°ã¯äœãè¿ããªããããªãã¡nil
ãè¿ããŸããããã¯ãªãã·ã§ãã«åãšããã
ãªãã·ã§ãã«åãšã¯ãå€ã®ååšãŸãã¯éååšãè¡šãããšãã§ããå€ã§ãããªãã·ã§ãã«åã«ã¯2ã€ã®ã±ãŒã¹ããããŸããæå®ããåã®å€ããããããŸãã¯äœãååšããªãïŒnil
ïŒãã®ã©ã¡ããã§ãããªãã·ã§ãã«åã¯ã?
æ¥å°ŸèŸãçšããŠå®£èšããŸãã
let newResource: HelloAsset? /* could either have a value of type `HelloAsset`
or it could have a value of `nil`, which represents nothing */
ãªãã·ã§ãã«åã«ãããéçºè
ã¯nil
ã®å Žåãããé©åã«åŠçããããšãã§ããããã«ãªããŸããããã§ç§éã¯ãload
ã§ååŸããhelloResource
ãªããžã§ã¯ããnil
ã§ããå¯èœæ§ãã¯ã£ãããšèæ
®ããå¿
èŠããããŸãïŒãªããªããload
ã¯ããŒããããã®ããªãå Žåã¯nil
ãè¿ãããã§ãïŒã
nil çµåæŒç®å (??
) ã䜿çšããŠããªãã·ã§ãã«ããunwrapãããŸããããã¯åºæ¬çã«ç§éã¯ãload
ã¡ãœãããnil
ãè¿ããå Žåã®å¯Ÿå¿ãåŠçããããšãæå³ããŸããããnil
ãè¿ã£ãŠããæã¯ã??
ã®åŸã®ã³ãŒããããã¯ãå®è¡ãããŸãã ããã§ã¯ãpanic
ãå®è¡ããŠããŸãŸããããã¯ãšã©ãŒã¡ãã»ãŒãžãšãšãã«ãã©ã³ã¶ã¯ã·ã§ã³ã®å®è¡ãäžæããŸãã
ãªãã·ã§ãã«åã®è©³çŽ°ãšäœ¿çšæ¹æ³ã«ã€ããŠã¯ããOptionals In Cadenceããåç §ããŠãã ããã
ã³ãŒãå ã§ããäœãåé¡ãçããæã«ãåžžã«ãã®è©³çŽ°ãªãšã©ãŒã¡ãã»ãŒãžãéçºè ã«æäŸããããšã¯ããŠãŒã¶ãŒãéçºè ã«ãšã£ãŠäœãä¿®æ£ãããã¹ãããæçœã«ããäžã§æ¥µããŠéèŠã§ãã
ãšã©ãŒã¡ãã»ãŒãžã«ã¯ãå¯èœã§ããã°ä»¥äžã®å 容ãå«ããã¹ãã§ãã
- ã¹ããŒãã³ã³ãã©ã¯ãããç±æ¥ããå Žåã¯ãã³ã³ãã©ã¯ãåãšé¢æ°å
- èµ·ããŠãããªãã©ã« ãšã©ãŒ(the literal error)ã®èª¬æ
- ãšã©ãŒã®åå ãšãªã£ãŠããå¯èœæ§ãããçç±ã®é«ã¬ãã«ãªèª¬æ
- ãšã©ãŒã«çŽçµããŠããå¯èœæ§ãããã¡ã¿ããŒã¿ãŸãã¯å€æ°å€
- å¯èœãªãã°ä¿®æ£æ¹æ³ã®ææ¡
ç§ãã¡ã®ãšã©ãŒã¡ãã»ãŒãžãèŠãŠãããã°åãããšãããç§ãã¡ã¯åé¡ç¹ãæ£ç¢ºã«èª¬æããŠããŸããã€ãŸãããªãœãŒã¹ããããã¹ãä¿åãã¹ã«ä¿åãããŠããªãããšã説æããŠããŸãããããŠããšã©ãŒãä¿®æ£ããããã®è§£æ±ºçãææ¡ããŠããŸããããã¯ããCreate Helloããã©ã³ã¶ã¯ã·ã§ã³ãå®è¡ããŠãªãœãŒã¹ãä¿åããããšã§ãã
Flow NFTã®GitHubãªããžããªã®ã¹ããŒãã³ã³ãã©ã¯ãããã©ã³ã¶ã¯ã·ã§ã³ã®ãšã©ãŒã¡ãã»ãŒãžãåèã«ã詳现ãã€ãã«ããã«ãªãšã©ãŒã¡ãã»ãŒãžã®äŸããã§ãã¯ããŠã¿ãŠãã ããã
hello()
é¢æ°ãåŒã³åºã
次ã«ãhello()
é¢æ°ãåŒã³åºããåºåããã°ã«åºããŸãã
log(helloResource.hello())
ãªãœãŒã¹ã眲åè ã®ã¢ã«ãŠã³ãã«åä¿åãã
次ã«ç§éã¯ãsave
ãåã³äœ¿çšããŠããªããžã§ã¯ããå
ã®ã¹ãã¬ãŒãžã®å Žæã«æ»ããŸãã
acct.storage.save(<-helloResource, to: /storage/HelloAssetTutorial)
ACTION
0x06
ã¢ã«ãŠã³ããå¯äžã®çœ²åè
ãšããŠéžæããŸããSend
ãã¿ã³ãã¯ãªãã¯ããŠããã©ã³ã¶ã¯ã·ã§ã³ãéä¿¡ããŸãã
以äžã®ãããªè¡šç€ºããããã¯ãã§ãã
"Hello, World!"
Reviewing the Resource Contract
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã䜿çšããŠãã©ã³ã¶ã¯ã·ã§ã³ã§ãªãœãŒã¹ãšã®ããåããè¡ããªãããCadenceã®ãªãœãŒã¹ã®çŽ¹ä»ãè¡ããŸããã
ããªãã¯ãã¹ãŠã®ã¹ã³ãŒãããã¢ã¯ã»ã¹å¯èœãªã¹ããŒãã³ã³ãã©ã¯ããå®è£
ããŸãããã¹ããŒãã³ã³ãã©ã¯ãã«ã¯hello()
ãšãããHello, World!
ãæååãè¿ãé¢æ°ãå®è£
ããããªãœãŒã¹ã宣èšãããŠããããŸãããªãœãŒã¹ãäœæã§ããé¢æ°ã宣èšããŸããã
次ã«ãããªãã¯ãã®ã³ã³ãã©ã¯ããã¢ã«ãŠã³ãã«ãããã€ããã¹ããŒãã³ã³ãã©ã¯ãã®äžã§ãªãœãŒã¹ãçæãããã©ã³ã¶ã¯ã·ã§ã³ãäœæãããã®ãªãœãŒã¹ããã©ã³ã¶ã¯ã·ã§ã³ã®çœ²åè
ãšããŠãã0x06
ã¢ã«ãŠã³ãã®äžã«ä¿åããŸããã
æåŸã«ããã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããŠHelloAsset
ãªãœãŒã¹ãã¢ã«ãŠã³ãã¹ãã¬ãŒãžãã移åãããhello
ã¡ãœãããåŒã³åºããã¢ã«ãŠã³ãã¹ãã¬ãŒãžã«åã³æ»ããŸããã
ä»ããã§ããªãã¯ãã®ãã¥ãŒããªã¢ã«ãå®äºããŸããã®ã§ãã·ã³ãã«ãªCadenceããã°ã©ã ãæžãããã®ä»¥äžã®åºæ¬çç¥èã身ã«ä»ããŸããã
- ã¹ããŒãã³ã³ãã©ã¯ãã®äžã«ãªãœãŒã¹ãå®è£ ãã
- ã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIãš
<-
移åæŒç®åã䜿çšããŠããªãœãŒã¹ãä¿åã移åããããŠããŒãããããš - ãã©ã³ã¶ã¯ã·ã§ã³ã®
prepare
ãã§ãŒãºã䜿çšããŠãã¢ã«ãŠã³ãã¹ãã¬ãŒãžãããªãœãŒã¹ãããŒãããïŒåãåºãïŒããš
ç°ãªããªãœãŒã¹ãäœæããããã«ã¹ããŒãã³ã³ãã©ã¯ãã奜ããªããã«å€æŽããŠãã ãããèªç±ã«å©çšå¯èœãªã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIãå®éšããŠãã ããããããŠèªç±ã«ããªãã®ã¹ããŒãã³ã³ãã©ã¯ãã®éãé¢æ°ãå®è¡ããæ°ãããã©ã³ã¶ã¯ã·ã§ã³ãäœæããŠãã ããããªãœãŒã¹ã§ã§ããããšã«ã€ããŠããã«è©³ããç¥ãããå Žåã¯ããªãœãŒã¹ãªãã¡ã¬ã³ã¹ããŒãžãã芧ãã ããã
翻蚳å
Flow Blockchainã®Cadence version1.0ããã¥ã¡ã³ã (3. Resource Contract Tutorial)