-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtransfer_tokens.cdc
41 lines (32 loc) · 1.7 KB
/
transfer_tokens.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
// This transaction is a template for a transaction that
// could be used by anyone to send tokens to another account
// that has been set up to receive tokens.
//
// The withdraw amount and the account from getAccount
// would be the parameters to the transaction
import "FungibleToken"
import "FlowToken"
transaction(amount: UFix64, to: Address) {
// The Vault resource that holds the tokens that are being transferred
let sentVault: @{FungibleToken.Vault}
prepare(signer: auth(BorrowValue) &Account) {
// Get a reference to the signer's stored vault
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("The signer does not store a FlowToken Vault object at the path "
.concat("/storage/flowTokenVault. ")
.concat("The signer must initialize their account with this vault first!"))
// Withdraw tokens from the signer's stored vault
self.sentVault <- vaultRef.withdraw(amount: amount)
}
execute {
// Get a reference to the recipient's Receiver
let receiverRef = getAccount(to)
.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
?? panic("Could not borrow a Receiver reference to the FlowToken Vault in account "
.concat(to.toString()).concat(" at path /public/flowTokenReceiver")
.concat(". Make sure you are sending to an address that has ")
.concat("a FlowToken Vault set up properly at the specified path."))
// Deposit the withdrawn tokens in the recipient's receiver
receiverRef.deposit(from: <-self.sentVault)
}
}