Module Initialization

Solidity

constructor is used to initialize the contract.


struct Storage {
uint64 data;
}
Storage private _storage;
constructor(uint256 data_) {
_storage.data = data_;
}

Move

The init_module function works much like a Solidity constructor. It should have the following signature fun init_module(owner: &signer) to be invoked by VM. If you require a custom initialization function with a different signature, it must be manually defined and called.


struct Storage has key {
data: u64
}
fun init_module(owner: &signer) {
move_to(owner, Storage { data: 0 });
}
entry public fun custom_init_module(owner: &signer, data: u64) {
assert!(is_owner(owner), 1);
assert!(!initialized(), 2);
move_to(owner, Storage { data });
}
fun is_owner(owner: &signer): bool {/* ... */}
fun initialized(): bool {/* ... */}