Attribute Macro concordium_std::init[][src]

#[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(ctx: &impl HasInitContext) -> InitResult<MyState> {...}

Where the trait HasInitContext and the type InitResult are exposed from concordium-std and MyState is the user-defined type for the contract state.

Optional attributes

payable: Make function accept an amount of GTU

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

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

Example

#[init(contract = "my_contract", payable)]
fn some_init(ctx: &impl HasInitContext, 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(ctx: &impl HasInitContext, logger: &mut impl HasLogger) -> InitResult<MyState> {...}

low_level: Manually deal with writing state bytes

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

If low_level is set, the signature must contain an extra argument of type &mut ContractState found in concordium-std, which gives access to manipulating the contract state bytes 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 ContractState) -> 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")]