Cadence Cookbook

Contribute

Admin Resource

Admin Resource

01 Apr 2022

Contributed by Flow Blockchain

Intermediate

Using an admin resource to have exclusive access to functions in a smart contract.

Smart Contract Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import "ExampleNFT" access(all) contract Recipe { // Declare an Admin resource for managing NFTs access(all) resource Admin { // Mint a new NFT access(all) fun mintNFT(): @ExampleNFT.NFT { return <-ExampleNFT.mintNFT() } // Create a new Admin resource access(all) fun createNewAdmin(): @Admin { return <-create Admin() } } // Function to create an Admin resource access(all) fun createAdmin(): @Admin { return <-create Admin() } }

The Admin portion of the Recipe contract is designed to provide secure and controlled access to administrative operations for managing NFTs in the ExampleNFT contract. It encapsulates privileged functionality within an Admin resource, ensuring that only authorized accounts holding this resource can perform sensitive tasks like minting new NFTs or creating additional Admin resources.

Transaction Example
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 import "NonFungibleToken" import "ExampleNFT" transaction { prepare(signer: auth(Storage) &Account) { // Check if the Admin resource already exists if signer.storage.borrow<&ExampleNFT.Admin>(from: /storage/nftAdminResource) != nil { log("Admin resource already exists in the account.") return } // Create a new Admin resource let adminResource <- ExampleNFT.createAdmin() // Save the Admin resource to the account's storage signer.storage.save(<-adminResource, to: /storage/nftAdminResource) log("Admin resource added to the account.") // Borrow the Admin resource back from storage let adminResourceRef = signer.storage.borrow<&ExampleNFT.Admin>(from: /storage/nftAdminResource) ?? panic("Failed to borrow the Admin resource after saving it to storage.") // Use the Admin resource to mint a new NFT let newNFT <- adminResourceRef.mintNFT() // Borrow the account's NFT collection to store the newly minted NFT let collection = signer.storage.borrow<&ExampleNFT.Collection>(from: ExampleNFT.CollectionStoragePath) ?? panic("NFT collection not found in the account. Please ensure the collection is set up.") // Deposit the newly minted NFT into the collection collection.deposit(token: <-newNFT) log("New NFT minted and added to the account's collection.") } execute { log("Transaction complete.") } }

This transaction demonstrates how to create and utilize an Admin resource for the ExampleNFT contract. It begins by checking if the Admin resource already exists in the account's storage at the path /storage/nftAdminResource.

If the Admin resource does not exist, the transaction calls the createAdmin function from the ExampleNFT contract to generate a new Admin resource.

After saving the Admin resource, the transaction borrows a reference to it from storage to perform further operations. Using this reference, it mints a new NFT by calling the mintNFT function of the Admin resource. The transaction then borrows the account's NFT collection from storage, ensuring it exists at the path specified by ExampleNFT.CollectionStoragePath. Finally, the newly minted NFT is deposited into the account's collection using the deposit function.


ProgressGetting Started With Access Management

Up Next: Add Admin Resource to Account

50%


Related Recipes

01 Apr 2022
Add Admin Resource to Account
Intermediate