The init function runs once when a module is published:
public struct MY_COIN has drop {}fun init(witness: MY_COIN, ctx: &mut TxContext) { let (treasury, metadata) = coin::create_currency( witness, 6, b"MY_COIN", b"", b"", option::none(), ctx, ); transfer::public_freeze_object(metadata); transfer::public_transfer(treasury, ctx.sender())}
The init function receives a one-time witness (a struct with the same name as the module) that can be used for privileged operations like creating currencies.
module my_package::bank { friend my_package::admin; public(package) fun privileged_operation() { // Only callable by friends }}module my_package::admin { use my_package::bank; public fun admin_action() { bank::privileged_operation(); }}
// Make functions as private as possiblefun internal_helper() { /* ... */ } // Privatepublic(package) fun package_api() { /* ... */ } // Packagepublic fun public_api() { /* ... */ } // Public only when needed
Document modules
/// Core game mechanics module/// /// Handles player actions, game state, and rewardsmodule my_package::game { // ...}