Attribute Macro sylvia::contract

source ·
#[contract]
Expand description

Macro generating messages from contract impl block.

Example usage




#[cw_derive::contract(module=msg)]
impl Cw4Group {
    #[msg(instantiate, name="Instantiate")]
    fn instantiate(&self, ctx: (DepsMut, Env, MessageInfo), admin: Option<String>)
        -> Result<Response, Error>;
}

This would generate output like:

pub mod msg {

    #[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, schemars::JsonSchema)]
    #[serde(rename_all = "snake_case")]
    pub struct Instantiate {
        admin: Option<String>,
    }

    impl Instantiate {
        fn dispatch(contract: &Cw4Group, ctx: (DepsMut, Env, MessageInfo), admin: Option<String>)
            -> Result<Response, Error>
        {
            contract.instantiate(ctx, admin)
        }
    }
}

Parameters

contract attribute takes optional parameters:

  • module - defines module name, where all generated messages would be encapsulated; no additional module would be created if not provided

Attributes

Messages structures are generated basing on specific implemented methods attributed with #[msg(msg_type, ...). Msg attribute takes as its first argument type of message it is supposed to handle:

  • instantiate - this is instantiation message handler. There should be always exactly one
  • exec - this is execute message variant
  • query - this is query message variant
  • migrate - this is migrate message variant handler for this kind of message. In case of query it is possible to pass second argument which is it’s ResponseType. This is required in case of aliased results wrapping their ResponseType. Example for member query
    #[msg(query, resp=MemberQueryResponse)]
    fn member(&self, ctx: (Deps, Env), addr: String, at_height: Option<u64>) -> Result<MemberQueryResponse, Error>