Skip to main content

Init

The init function is a special function that executes only once - when you publish the associated module. It always has the same signature and only one argument:

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

The initializer function must have the following properties for it to execute at publication:

- Function name must be `init`
- The parameter list must end with either a `&mut TxContext` or a `&TxContext` type
- No return values
- Private visibility
- Optionally, the parameter list starts by accepting the module's one-time witness by value

For example, the following init functions are all valid:

- `fun init(ctx: &TxContext)`
- `fun init(ctx: &mut TxContext)`
- `fun init(otw: EXAMPLE, ctx: &TxContext)`
- `fun init(otw: EXAMPLE, ctx: &mut TxContext)`

The following example demonstrates a valid init function in a module:

module examples::one_timer {
use sui::transfer;
use sui::object::{Self, UID};
use sui::tx_context::{Self, TxContext};

/// The one of a kind - created in the module initializer.
struct CreatorCapability 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
/// `CreatorCapability` struct.
fun init(ctx: &mut TxContext) {
transfer::transfer(CreatorCapability {
id: object::new(ctx),
}, tx_context::sender(ctx))
}
}