Init Function

Init is a special function which gets called when a module is published. It is guaranteed that it will never be called again. It can take the &mut TxContext as the last argument and a One-Time-Witness (optional) as the first argument.

fun init(ctx: &mut TxContext) { /* ... */ }

For example:

module examples::init_function {
    /// The one of a kind - created in the module initializer.
    public struct CreatorCap has key {
        id: UID
    }

    /// This function is only called once on module publish.
    /// Use it to make sure something has happened only once, like
    /// here - only module author will own a version of a
    /// `CreatorCap` struct.
    fun init(ctx: &mut TxContext) {
        transfer::transfer(CreatorCap {
            id: object::new(ctx),
        }, ctx.sender())
    }
}

Learn more