Attribute Macro concordium_std_derive::receive [−][src]
#[receive]
Expand description
Derive the appropriate export for an annotated receive function.
This macro requires the following items to be present
contract = "<contract-name>"
where <contract-name> is the name of the smart contract.name = "<receive-name>"
where <receive-name> is the name of the receive function. The generated function is exported as<contract-name>.<receive-name>
. Contract name and receive name is required to be unique in the module.
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
#[receive(contract = "my_contract", name = "some_receive")] fn contract_receive<A: HasActions>(ctx: &impl HasReceiveContext, state: &mut MyState) -> ReceiveResult<A> {...}
Where the HasAction
, HasReceiveContext
traits and the type
ReceiveResult
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 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
#[receive(contract = "my_contract", name = "some_receive", payable)] fn contract_receive<A: HasActions>(ctx: &impl HasReceiveContext, amount: Amount, state: &mut MyState) -> ReceiveResult<A> {...}
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
#[receive(contract = "my_contract", name = "some_receive", enable_logger)] fn contract_receive<A: HasActions>(ctx: &impl HasReceiveContext, logger: &mut impl HasLogger, state: &mut MyState) -> ReceiveResult<A> {...}
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, instead of the user-defined state type in the
signature, the state argument becomes the type &mut ContractState
found in
concordium-std
, which gives access to manipulating the contract state
bytes directly.
Example
#[receive(contract = "my_contract", name = "some_receive", low_level)] fn contract_receive<A: HasActions>(ctx: &impl HasReceiveContext, state: &mut ContractState) -> ReceiveResult<A> {...}
parameter="<Param>"
: Generate schema for parameter
To make schema generation 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 { ... } #[receive(contract = "my_contract", name = "some_receive", parameter = "MyParam")] fn contract_receive<A: HasActions>(ctx: &impl HasReceiveContext, state: &mut MyState) -> ReceiveResult<A> {...}