sv::msg attribute

Use the sv::msg attribute to mark methods as specific message types.

Macros

List of macros supporting the sv::msg attribute:

💡

interface macro supports the sv::msg attribute only with exec, query or sudo value.

Usage

pub struct CounterContract;

#[cw_serde]
pub struct SomeResponse;

#[cfg_attr(feature = "library", sylvia::entry_points)]
#[contract]
impl CounterContract {
    pub const fn new() -> Self {
        Self
    }

    #[sv::msg(instantiate)]
    fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> {
        Ok(Response::new())
    }

    #[sv::msg(exec)]
    fn some_exec(&self, ctx: ExecCtx) -> StdResult<Response> {
        Ok(Response::new())
    }

    #[sv::msg(query)]
    fn some_query(&self, ctx: QueryCtx) -> StdResult<SomeResponse> {
        Ok(SomeResponse)
    }

    #[sv::msg(sudo)]
    fn some_sudo(&self, ctx: SudoCtx) -> StdResult<Response> {
        Ok(Response::new())
    }

    #[sv::msg(migrate)]
    fn some_migrate(&self, ctx: MigrateCtx) -> StdResult<Response> {
        Ok(Response::new())
    }

    #[sv::msg(reply)]
    fn some_reply(&self, ctx: ReplyCtx, reply: Reply) -> StdResult<Response> {
        Ok(Response::new())
    }
}

Notice that each message type has its context type, like InstantiateCtx (opens in a new tab). You are required to use the appropriate type. Otherwise, the dispatch will fail to compile.

Each message type except for query expects the result type generic over Response (opens in a new tab).

Aliasing

It is possible to use aliases for types in message signatures. In case of query return type it requires however additional action. Due to QueryResponses (opens in a new tab) macro derive on generated query message, we have to explicitly provide the return type, as the macro wouldn't be able to deduce that from the alias.

#[sv::msg(query, resp=SomeResponse)]
fn some_query(&self, ctx: QueryCtx) -> SomeResult {
    Ok(SomeResponse)
}