@@ -5,51 +5,52 @@ import "ExampleToken"
5
5
// that owns a Vault
6
6
transaction {
7
7
8
- // Temporary Vault object that holds the balance being transferred
9
8
var temporaryVault : @ExampleToken .Vault
9
+ var receiver : Capability <&{ExampleToken .Receiver }>
10
+ var receiverRef : &{ExampleToken .Receiver }
11
+ let mintingRef : &ExampleToken .VaultMinter
10
12
11
13
prepare (acct : auth (Storage, Capabilities) &Account) {
12
- // Step 1: Check if a Vault exists, else create one
14
+ // Check if a Vault exists, else create one
13
15
if acct.storage.borrow< &ExampleToken.Vault> (from : / storage/ MainVault) == nil {
14
16
let newVault <- ExampleToken.createEmptyVault ()
15
17
acct.storage.save< @ExampleToken.Vault> (<- newVault, to: / storage/ MainVault)
16
18
log (" New Vault created and saved to storage" )
17
19
}
18
20
19
- // Step 2: Mint 50 tokens into the Vault using VaultMinter
20
- let minterRef = acct.storage.borrow< &ExampleToken.VaultMinter> (
21
+ // Mint 50 tokens into the Vault using VaultMinter
22
+ let minter = acct.storage.borrow< &ExampleToken.VaultMinter> (
21
23
from : / storage/ CadenceFungibleTokenTutorialMinter
22
- ) ?? panic (" Could not borrow a reference to the VaultMinter" )
23
-
24
- let recipientCap = acct.capabilities.storage.issue< auth (ExampleToken.Receiver) &ExampleToken.Vault> (
24
+ ) ?? panic (" Could not borrow a reference to the minter" )
25
+ self .mintingRef = minter
26
+
27
+ // Issue a Receiver capability for the caller's Vault
28
+ let receiverCap = acct.capabilities.storage.issue< &{ExampleToken.Receiver}> (
25
29
/ storage/ MainVault
26
30
)
27
- minterRef.mintTokens (amount : 50 .0 , recipient : recipientCap)
31
+ self .receiver = receiverCap
32
+
33
+ self .mintingRef.mintTokens (amount : 30 .0 , recipient : self .receiver)
28
34
log (" Minted 50 tokens into the Vault" )
29
35
30
- // Step 3: Borrow the Vault with Withdraw entitlement
36
+ // Borrow the Vault with Withdraw entitlement
31
37
let vaultRef = acct.storage.borrow< auth (ExampleToken.Withdraw) &ExampleToken.Vault> (
32
38
from : / storage/ MainVault
33
39
) ?? panic (" Could not borrow a reference to the sender's Vault" )
34
40
35
- // Step 4: Withdraw 10 tokens into the temporary Vault
41
+ // Withdraw 10 tokens into the temporary Vault
36
42
self .temporaryVault <- vaultRef.withdraw (amount : 10 .0 )
37
- log (" Withdrawn 10 tokens into temporary Vault" )
38
- }
39
-
40
43
41
- execute {
42
- // Get the recipient's public account object
43
- let recipient = getAccount (0x01 )
44
+ log (" Withdrawn 10 tokens into temporary Vault" )
44
45
45
- // Borrow the recipient's Receiver capability for their Vault
46
- let receiverRef = recipient.capabilities.get< &ExampleToken.Vault> (
47
- / public/ MainReceiver
48
- ).borrow () ?? panic (" Could not borrow a Receiver reference from the recipient's Vault" )
46
+ self .receiverRef = acct.storage.borrow< &{ExampleToken.Receiver}> (
47
+ from : / storage/ MainVault
48
+ ) ?? panic (" Could not borrow a Receiver reference to the account's Vault" )
49
49
50
- // Deposit the tokens into the recipient's Vault
51
- receiverRef.deposit (from : <- self .temporaryVault)
50
+ }
52
51
53
- log (" Transfer succeeded!" )
52
+ execute {
53
+ self .receiverRef.deposit (from : <- self .temporaryVault)
54
+ log (" Tokens successfully deposited!" )
54
55
}
55
56
}
0 commit comments