Entry points

Entry_points

Use the entry_points macro to generate entry points of your contract.

💡

Use entry_points macro on top of contract macro. The contract macro erases all the Sylvia attributes, which may cause the entry_points macro to fail. The entry_points macro also depends on the messages generated by the contract macro.

Attributes

List of attributes supported by entry_points macro:

Usage

#[cfg(not(feature = "library"))]
use sylvia::entry_points;

pub struct Contract;

#[cw_serde]
pub struct SomeResponse;

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
impl Contract {
    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())
    }
}

The entry_points macro scans for sv::msg attributes. By default it generates: instantiate, exec, and query entry points.

Custom types

You can construct your entry points to work with some specific custom types with the sv::custom.

Generic types

CosmWasm entry points cannot be generic. We are thus forced to provide concrete types to be used in place of generic types used in the contract.

use cw_storage_plus::Item;

#[cfg(not(feature = "library"))]
use sylvia::cw_std::Empty;
#[cfg(not(feature = "library"))]
use sylvia::entry_points;

pub struct Contract<ExecParamT, FieldT> {
    field: Item<FieldT>,
    _phantom: PhantomData<ExecParamT>,
}

#[cw_serde]
pub struct SomeResponse;

#[cfg_attr(not(feature = "library"), entry_points(generics<Empty, Empty>))]
#[contract]
impl<ExecParamT, FieldT> Contract<ExecParamT, FieldT>
where
    ExecParamT: CustomMsg + 'static,
    FieldT: 'static,
{
    pub const fn new() -> Self {
        Self {
            field: Item::new("field"),
            _phantom: PhantomData,
        }
    }

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

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

We do that by adding parentheses after the entry_points macro and passing generics attribute with concrete types passed in brackets.

💡

Remember to pass the types in the order reflecting the order of generics defined on the contract.