Skip to main content

Module Initializers

In Move, the init function plays a critical role during the module's lifecycle, executing only once at the moment of module publication. To ensure proper usage, the init function must conform to specific criteria:

  • Function Name: The function must be named init.
  • Parameter List: The last parameter must be of type &mut TxContext or &TxContext.
  • Return Values: The function must not return any values.
  • Visibility: The function should be private.
  • Optional Parameter: The parameter list may optionally begin by accepting the module's one-time witness by value.

Here are some examples of valid init functions:

  • fun init(ctx: &TxContext)
  • fun init(ctx: &mut TxContext)
  • fun init(otw: EXAMPLE, ctx: &TxContext)
  • fun init(otw: EXAMPLE, ctx: &mut TxContext)
warning

It's important to note that init functions are executed only once when the package is initially published. They do not run during subsequent package upgrades. If an init function is added during an upgrade (whether in a new or existing module), it will not be executed.

Below is an example of a valid init function within a Move module:

module examples::one_timer {
/// The unique capability created during the module's initialization.
public struct CreatorCapability has key {
id: UID
}

/// This function runs only once upon the module's publication.
/// It ensures that certain actions, like granting the module's author a unique
/// `CreatorCapability`, happen just once.
fun init(ctx: &mut TxContext) {
transfer::transfer(CreatorCapability {
id: object::new(ctx),
}, tx_context::sender(ctx))
}
}

Question 1/4

When does the `init` function in a Move module execute?