Modules

Modules are libraries that define struct types along with functions that operate on these types.

A minimal module example which only stores an admin of the module.

It allows anyone to read the current admin address, and only current admin to update it to new admin address.


module aptogotchi::main {
struct Config has copy, drop {
admin: address
}
// Init function runs once when the module is published for the first time.
fun init_module(caller: &signer) {
let caller_addr = address_of(caller);
// move_to is a function to move the struct to the global storage
move_to(caller, Config { admin: caller_addr });
}
#[view]
public fun get_admin(): address acquires Config {
// @aptogotchi is the address of the module
let config = borrow_global<Config>(@aptogotchi);
config.admin
}
}