Add Admin Resource to Account
Add Admin Resource to Account
01 Apr 2022
Contributed by Flow Blockchain
When you want to give someone else access to the admin resource.
Smart Contract Example
1
2
3
4
5
6
7
8
9
10
11
access(all) contract Recipe {
// Admin is a special authorization resource that
// allows the owner to perform important NFT
// functions
access(all) resource Admin {
access(all) fun createNewAdmin(): @Admin {
return <-create Admin()
}
}
}
The Recipe contract is designed to manage administrative permissions using a special resource called Admin. This resource gives its owner the ability to perform important NFT-related actions in a secure and controlled way.
The Admin resource includes a function called createNewAdmin. This function allows the creation of new Admin resources, which can be given to other users. These users can then store the Admin resource in their account and gain administrative permissions.
By keeping these permissions within the Admin resource, the contract ensures that critical operations are carefully managed. This makes the system secure and flexible, especially useful for NFT platforms or other applications that need clear and safe access controls.
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
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.")
}
execute {
log("Transaction complete.")
}
}
This transaction is designed to add an Admin resource to an account, granting the account administrative privileges within the ExampleNFT contract. It first checks if the Admin resource already exists in the account's storage to avoid creating duplicate resources.
If the Admin resource does not exist, the transaction calls the createAdmin function from the ExampleNFT contract to create a new Admin resource. This resource is then moved into the account’s storage at the specified path /storage/nftAdminResource.
100%