Skip to main content

能力 (Capabilities)

能力是一种允许使用对象进行授权操作的模式。例如,UpgradeCap 授权了升级包的操作。你可能会遇到的最常见的能力之一是 TreasuryCap,它在 sui::coin 中定义。

module examples::item {
use sui::transfer;
use sui::object::{Self, UID};
use std::string::{Self, String};
use sui::tx_context::{Self, TxContext};

/// Type that marks Capability to create new `Item`s.
struct AdminCap has key { id: UID }

/// Custom NFT-like type.
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 entry function can not be called if `AdminCap` is not passed as