Initialize a Contract

Overview

Only when a module is being published for the first time (and not during an upgrade), will the VM search for and execute an init_module(account: &signer) function.

The signer of the account that is publishing the module is passed into the init_module function of the contract.

This function must be private and not return any value.


Aptogotchi Example

Source Code


// This function is only callable during publishing of the module
fun init_module(account: &signer) {
let constructor_ref = object::create_named_object(
account,
APP_OBJECT_SEED,
);
let extend_ref = object::generate_extend_ref(&constructor_ref);
let app_signer = &object::generate_signer(&constructor_ref);
move_to(account, MintAptogotchiEvents {
mint_aptogotchi_events: account::new_event_handle<MintAptogotchiEvent>(account),
});
move_to(app_signer, CollectionCapability {
extend_ref,
});
create_aptogotchi_collection(app_signer);
}