-
Notifications
You must be signed in to change notification settings - Fork 53
/
safe_transfer_tokens.cdc
52 lines (40 loc) · 2.49 KB
/
safe_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
42
43
44
45
46
47
48
49
50
51
52
import "FungibleToken"
import "FungibleTokenSwitchboard"
import "ExampleToken"
import "FungibleTokenMetadataViews"
/// This transaction is a template for a transaction that could be used by anyone to send tokens to another account
/// through a switchboard using the deposit method but before depositing we will explicitly check whether receiving
/// capability is borrowable or not and if yes then it will deposit the vault to the receiver capability.
///
transaction(to: Address, amount: UFix64) {
// The reference to the vault from the payer's account
let vaultRef: auth(FungibleToken.Withdraw) &ExampleToken.Vault
prepare(signer: auth(BorrowValue) &Account) {
let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not resolve FTVaultData view. The ExampleToken"
.concat(" contract needs to implement the FTVaultData Metadata view in order to execute this transaction"))
// Get a reference to the signer's stored vault
self.vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &ExampleToken.Vault>(from: vaultData.storagePath)
?? panic("The signer does not store a ExampleToken Vault object at the path "
.concat(vaultData.storagePath.toString())
.concat(". The signer must initialize their account with this object first!"))
}
execute {
// Get the recipient's public account object
let recipient = getAccount(to)
let sentVault <- self.vaultRef.withdraw(amount: amount)
// Get a reference to the recipient's SwitchboardPublic
let switchboardRef = recipient.capabilities.borrow<&{FungibleTokenSwitchboard.SwitchboardPublic}>(
FungibleTokenSwitchboard.PublicPath)
?? panic("The signer does not store a FungibleToken Switchboard capability at the path "
.concat(FungibleTokenSwitchboard.PublicPath.toString())
.concat(". The signer must initialize their account with this object first!"))
// Validate the receiving capability by using safeBorrowByType
if let receivingRef = switchboardRef.safeBorrowByType(type: Type<@ExampleToken.Vault>()) {
switchboardRef.deposit(from: <-sentVault)
} else {
// Return funds to signer's account if receiver is not configured to receive the funds
self.vaultRef.deposit(from: <-sentVault)
}
}
}