#[init]
Expand description

Derive the appropriate export for an annotated init function.

This macro requires the following items to be present

  • contract="<name>" where <name> is the name of the smart contract and the generated function is exported as this name prefixed with init_. The name should be unique in the module, as a contract can only have one init-function.

The annotated function must be of a specific type, which depends on the enabled attributes. Without any of the optional attributes the function must have a signature of

#[init(contract = "my_contract")]
fn some_init<S: HasStateApi>(ctx: &impl HasInitContext, state_builder: &mut StateBuilder<S>,) -> InitResult<MyState> {...}

Where HasInitContext, InitResult, and StateBuilder are exposed from concordium-std and MyState is a user-defined type.

Optional attributes

payable: Make function accept an amount of CCD

Without setting the payable attribute, the generated function will reject any non-zero amount of CCD supplied with the transaction. This means we are required to explicitly mark our functions as payable, if they are to accept CCD.

Setting the payable attribute changes the required signature to include an extra argument of type Amount, allowing the function to access the amount of CCD supplied with the transaction.

Example

#[init(contract = "my_contract", payable)]
fn some_init<S: HasStateApi>(ctx: &impl HasInitContext, state_builder: StateBuilder<S>, amount: Amount) -> InitResult<MyState> {...}

enable_logger: Function can access event logging

Setting the enable_logger attribute changes the required signature to include an extra argument &mut impl HasLogger, allowing the function to log events.

Example

#[init(contract = "my_contract", enable_logger)]
fn some_init<S: HasStateApi>(ctx: &impl HasInitContext, state_builder: StateBuilder<S>, logger: &mut impl HasLogger) -> InitResult<MyState> {...}

low_level: Manually deal with the low-level state.

Setting the low_level attribute disables the generated code for serializing the contract state.

If low_level is set, the &mut StateBuilder<S> in the signature is replaced by &impl mut HasStateApi found in concordium-std, which gives access to manipulating the low-level contract state directly. This means there is no need to return the contract state and the return type becomes InitResult<()>.

Example

#[init(contract = "my_contract", low_level)]
fn some_init(ctx: &impl HasInitContext, state: &mut impl HasStateApi) -> InitResult<()> {...}

parameter="<Param>": Generate schema for parameter

To make schema generation to include the parameter for this function, add the attribute parameter and set it equal to a string literal containing the name of the type used for the parameter. The parameter type must implement the SchemaType trait, which for most cases can be derived automatically.

Example

#[derive(SchemaType)]
struct MyParam { ... }

#[init(contract = "my_contract", parameter = "MyParam")]

crypto_primitives: Function can access cryptographic primitives

Setting the crypto_primitives attribute changes the required signature to include an extra argument &impl HasCryptoPrimitives, which provides cryptographic primitives such as verifying signatures and hashing data.

Example

#[init(contract = "my_contract", crypto_primitives)]
fn some_init<S: HasStateApi>(
    ctx: &impl HasInitContext,
    state_build: StateBuilder<S>,
    crypto_primitives: &impl HasCryptoPrimitives,
) -> InitResult<MyState> {...}