sv::custom attribute

Use sv::custom if you want to use a custom message and/or query in your contract.

Macros

List of macros supporting the sv::custom attribute:

Usage

use sylvia::cw_std::{CustomMsg, CustomQuery};
 
#[cw_serde]
pub struct SomeMsg {}
#[cw_serde]
pub struct SomeQuery {}
 
impl CustomMsg for SomeMsg {}
impl CustomQuery for SomeQuery {}
 
pub struct Contract;
 
#[cw_serde]
pub struct SomeResponse;
 
#[cfg_attr(feature = "library", sylvia::entry_points)]
#[contract]
#[sv::custom(msg=SomeMsg, query=SomeQuery)]
impl Contract {
    pub const fn new() -> Self {
        Self
    }
 
    #[sv::msg(instantiate)]
    fn instantiate(&self, ctx: InstantiateCtx<SomeQuery>) -> StdResult<Response<SomeMsg>> {
        Ok(Response::new())
    }
}

sv::custom accepts one or two parameters:

  • msg - informs Sylvia macros about the custom message used in the contract
  • query - informs Sylvia macros about the custom query used in the contract
💡

Types passed as parameters have to implement CustomMsg (opens in a new tab) and CustomQuery (opens in a new tab) respectively.

sv::custom works the same in the case of the interface macro.

use sylvia::cw_std::{CustomMsg, CustomQuery};
 
#[cw_serde]
pub struct SomeMsg {}
#[cw_serde]
pub struct SomeQuery {}
 
impl CustomMsg for SomeMsg {}
impl CustomQuery for SomeQuery {}
 
#[cw_serde]
pub struct InterfaceQueryResp {}
 
#[interface]
#[sv::custom(msg=SomeMsg, query=SomeQuery)]
pub trait SomeInterface {
    type Error: From<StdError>;
 
    #[sv::msg(exec)]
    fn interface_exec(&self, ctx: ExecCtx<SomeQuery>) -> StdResult<Response<SomeMsg>>;
 
    #[sv::msg(query)]
    fn interface_query(&self, ctx: QueryCtx<SomeQuery>) -> StdResult<InterfaceQueryResp>;
}
💡

It's also possible to define custom types for the interface using associated types. We cover it in the interface macro section.