初始化函数(Init Function)
初始化函数(init
)是一个只在模块发布时被执行一次的函数,函数签名总是一致和且只有一个参数:
fun init(ctx: &mut TxContext) { /* ... */ }
English Version
Init function is a special function that gets executed only once - when the associated module is published. It always has the same signature and only one argument:
fun init(ctx: &mut TxContext) { /* ... */ }
示例:
module examples::one_timer {
use sui::transfer;
use sui::object::{Self, UID};
use sui::tx_context::{Self, TxContext};
/// `CreatorCapability`对象将只在模块发布时创建一次
/// The one of a kind - created in the module initializer.
struct CreatorCapability has key {
id: UID
}
/// `init`函数只会执行一次,在这个例子中只有模块的发布者拥有`CreatorCapability`
/// This function is only called once on module publish.
/// Use it to make sure something has happened only once, like
/// here - only module author will own a version of a
/// `CreatorCapability` struct.
fun init(ctx: &mut TxContext) {
transfer::transfer(CreatorCapability {
id: object::new(ctx),
}, tx_context::sender(ctx))
}
}