/
setup_account_from_address.cdc
33 lines (26 loc) · 1.56 KB
/
setup_account_from_address.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
/// This transaction is what an account would run
/// to set itself up to receive NFTs. This function
/// uses views to know where to set up the collection
/// in storage and to create the empty collection.
import "NonFungibleToken"
import "MetadataViews"
transaction(contractAddress: Address, contractName: String) {
prepare(signer: auth(IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {
// Borrow a reference to the nft contract deployed to the passed account
let resolverRef = getAccount(contractAddress)
.contracts.borrow<&{NonFungibleToken}>(name: contractName)
?? panic("Could not borrow a reference to the non-fungible token contract")
// Use that reference to retrieve the NFTCollectionData view
let collectionData = resolverRef.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
?? panic("Could not resolve the NFTCollectionData view for the given non-fungible token contract")
// Create a new empty collections
let emptyCollection <- collectionData.createEmptyCollection()
// save it to the account
signer.storage.save(<-emptyCollection, to: collectionData.storagePath)
// create a public capability for the collection
let collectionCap = signer.capabilities.storage.issue<&{NonFungibleToken.Collection}>(
collectionData.storagePath
)
signer.capabilities.publish(collectionCap, at: collectionData.publicPath)
}
}