Attribute Macro sylvia::interface

source · []
#[interface]
Expand description

Macro generating messages from contract trait.

Example usage






#[sylvia::interface(module=msg)]
trait Cw4 {
    type Error: From<StdError>;

    #[msg(exec)]
    fn update_admin(&self, ctx: (DepsMut, Env, MessageInfo), admin: Option<String>) -> Result<Response, Self::Error>;

    #[msg(exec)]
    fn update_members(&self, ctx: (DepsMut, Env, MessageInfo), remove: Vec<String>, add: Vec<Member>)
        -> Result<Response, Self::Error>;

    #[msg(query)]
    fn admin(&self, ctx: (Deps, Env)) -> Result<AdminQueryResponse, Error>;

    #[msg(query)]
    fn member(&self, ctx: (Deps, Env), addr: String, at_height: Option<u64>) -> Result<MemberQueryResponse, 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 enum ExecMsg {
        UpdateAdmin { admin: Option<String> },
        UpdateMembers {
            remove: Vec<String>,
            add: Vec<Member>,
        },
        AddHook { addr: String },
        RemoveHook { addr: String },
    }

    impl ExecMsg {
        pub fn dispatch<C: Cw4>(contract: &C, ctx: (DepsMut, Env, MessageInfo))
            -> Result<Response, C::Error>
        {
            // Some dispatching implementation
        }
    }
}

And similar `Query` structure for handling queries.

Parameters

interface 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 interface trait method attributed with #[msg(msg_type, ...). Msg attribute takes as its first argument type of message it is supposed to handle:

  • exec - this is execute message variant
  • query - this is query message variant

For now, #[msg(...)] attribute doesn’t support any additional data on #[interface] elements, but it may be extended in future.