Capabilities
A capability is a pattern that allows authorizing actions with an object. For example, the UpgradeCap
authorizes the action of upgrading packages. One of the most common capabilities you might encounter is TreasuryCap
, which is defined in iota::coin
.
module examples::item {
use std::string::String;
/// Type that marks Capability to create new `Item`s.
public struct AdminCap has key { id: UID }
/// Custom NFT-like type.
public struct Item has key, store { id: UID, name: String }
/// Module initializer is called once on module publish.
/// Here we create only one instance of `AdminCap` and send it to the publisher.
fun init(ctx: &mut TxContext) {
transfer::transfer(AdminCap {
id: object::new(ctx)
}, tx_context::sender(ctx))
}
/// The function can not be called if `AdminCap` is not passed as
public fun create_item(_: &AdminCap, name: String, ctx: &mut TxContext): Item {
let item = Item {
id: object::new(ctx),
name,
};
item
}
}