Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Init Routes

Init routes run once when an entity instance is created. They supply the arguments that member transforms use for initial state. An entity may have at most one init route.

Syntax

Init routes are declared with the init keyword:

init setup(owner: address, name: String) => [
    // initialization actions (optional)
]

Purpose

The init route is invoked exactly once at instance creation. Its primary role is to provide initial values for members through their transforms:

entity Token {
    routes {
        init deploy(
            name: String,
            symbol: String,
            decimals: u8,
            owner: address
        ) => []
    }

    m_name: String {
        in deploy(name, _, _, _) => name
    }

    m_symbol: String {
        in deploy(_, symbol, _, _) => symbol
    }

    m_decimals: u8 {
        in deploy(_, _, decimals, _) => decimals
    }

    m_owner: address {
        in deploy(_, _, _, owner) => owner
    }

    m_total_supply: U256 {
        in deploy(_, _, _, _) => 0
    }
}

Parameter Passing

Init route parameters are passed at deployment time. The deploying entity (or off-chain client) provides these values as part of the deployment transaction:

// In another entity, deploying a Token (identity + init args):
deploy Token("MyToken", "MTK", 18) with { value: init_value }

Init Actions

While the init route’s action list is often empty ([]), it can contain actions that should execute at deployment:

init setup(owner: address, treasury: address) => [
    Welcome(owner) ~> treasury
]

Member Initialization

Every member that needs an initial value must include a transform referencing the init route. Members without an init transform start at their type’s default value (typically zero or empty).

The parameter pattern in a member transform for an init route uses positional matching with _ for unused parameters:

m_owner: address {
    in deploy(_, _, _, owner) => owner
}

m_paused: bool {
    in deploy(_, _, _, _) => false
}

Complete Example

A vault entity with an init route:

entity Vault {
    const MIN_DEPOSIT: U256 = 100;

    routes {
        init create(
            owner: address,
            guardian: address,
            daily_limit: U256
        ) => []

        deposit()
            where msg::value >= MIN_DEPOSIT : throw 100
        => []

        withdraw(amount: U256)
            where msg::sender == m_owner : throw 101
            && m_balance >= amount : throw 102
        => [
            ~> msg::sender with { value: amount }
        ]
    }

    m_owner: address {
        in create(owner, _, _) => owner
    }

    m_guardian: address {
        in create(_, guardian, _) => guardian
    }

    m_daily_limit: U256 {
        in create(_, _, daily_limit) => daily_limit
    }

    m_balance: U256 {
        in create(_, _, _) => 0
        in deposit() => m_balance + msg::value
        in withdraw(amount) => m_balance - amount
    }
}

Constraints

  • An entity can have at most one init route.
  • Init routes cannot be called after deployment — they execute exactly once.
  • Init routes can include where clauses to validate constructor arguments.
  • On EVM, construction is split into a factory-guarded initialize(); see Deterministic Addresses.