-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathgeneric_transfer_with_paths.cdc
62 lines (51 loc) · 2.8 KB
/
generic_transfer_with_paths.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
53
54
55
56
57
58
59
60
61
62
import "NonFungibleToken"
#interaction (
version: "1.0.0",
title: "Generic NFT Transfer with Paths",
description: "Transfer any Non-Fungible Token by providing the paths for the source Collection and destination Collection",
language: "en-US",
)
/// Can pass in any storage path and receiver path instead of just the default.
/// This lets you choose the token you want to send as well the capability you want to send it to.
///
/// Any token path can be passed as an argument here, so wallets should
/// should check argument values to make sure the intended token path is passed in
///
/// @param to: The address to transfer the token to
/// @param id: The id of the token to transfer
/// @param senderPathIdentifier: The string identifier of the storage path
/// where the token should be withdrawn from
/// @param receiverPathIdentifier: The string identifier of the public path
/// where the token should be deposited to
///
transaction(to: Address, id: UInt64, senderPathIdentifier: String, receiverPathIdentifier: String) {
// The NFT resource to be transferred
let tempNFT: @{NonFungibleToken.NFT}
prepare(signer: auth(BorrowValue) &Account) {
let storagePath = StoragePath(identifier: senderPathIdentifier)
?? panic("Could not construct a storage path from the provided path identifier string")
// borrow a reference to the signer's NFT collection
let withdrawRef = signer.storage.borrow<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(
from: storagePath
) ?? panic("The signer does not store a NonFungibleToken Collection object at the path "
.concat(storagePath.toString())
.concat("The signer must initialize their account with this collection first!"))
self.tempNFT <- withdrawRef.withdraw(withdrawID: id)
}
execute {
let publicPath = PublicPath(identifier: receiverPathIdentifier)
?? panic("Could not construct a public path from the provided path identifier string \""
.concat(receiverPathIdentifier)
.concat("\"."))
// get the recipients public account object
let recipient = getAccount(to)
// borrow a public reference to the receivers collection
let receiverRef = recipient.capabilities.borrow<&{NonFungibleToken.Receiver}>(publicPath)
?? panic("The recipient does not have a NonFungibleToken Receiver at "
.concat(publicPath.toString())
.concat(" that is capable of receiving a NFT.")
.concat("The recipient must initialize their account with this collection and receiver first!"))
// Deposit the NFT to the receiver
receiverRef.deposit(token: <-self.tempNFT)
}
}