問題一覧に戻る
中級Move開発
問題 23: モジュール初期化関数
Moveにおけるモジュール初期化関数について学びます。init関数は、モジュールが公開されたときに一度だけ呼び出される特別な関数です。モジュール公開者に初期オブジェクトを作成して転送するためによく使用されます。
module my_first_package::example;
// Part 1: These imports are provided by default
// use sui::object::{Self, UID};
// use sui::transfer;
// use sui::tx_context::{Self, TxContext};
// Part 2: struct definitions
public struct Sword has key, store {
id: UID,
magic: u64,
strength: u64,
}
public struct Forge has key {
id: UID,
swords_created: u64,
}
// Part 3: Module initializer to be executed when this module is published
// モジュール公開時に一度だけ呼ばれる特別な関数
fun (ctx: &mut TxContext) {
let admin = Forge {
// オブジェクトの新しい一意の識別子を作成
id: ::(ctx),
swords_created: 0,
};
// Transfer the forge object to the module/package publisher
// 公開者に所有権を転送
::transfer(admin, ctx.());
}