Skip to content

Commit

Permalink
fix: Allow generic return type in contract queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 21, 2023
1 parent ef1da32 commit d3d7a3c
Show file tree
Hide file tree
Showing 20 changed files with 730 additions and 461 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/contracts/generic_contract/src/bin/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {

write_api! {
instantiate: InstantiateMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg>,
query: ContractQueryMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg, SvCustomMsg>,
query: ContractQueryMsg<SvCustomMsg, SvCustomMsg>,
}
}
94 changes: 46 additions & 48 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,43 @@ use cw_storage_plus::Item;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{
CustomMsg, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx, SvCustomMsg,
CustomMsg, CustomQuery, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx, SvCustomMsg,
};
use sylvia::{contract, schemars};

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

pub struct GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
FieldType,
> {
_field: Item<'static, FieldType>,
pub struct GenericContract<InstantiateT, ExecT, QueryT, MigrateT, CustomMsgT, CustomQueryT, FieldT>
{
_field: Item<'static, FieldT>,
_phantom: std::marker::PhantomData<(
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
InstantiateT,
ExecT,
QueryT,
MigrateT,
CustomMsgT,
CustomQueryT,
)>,
}

#[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg, String>))]
// #[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg, SvCustomQuery, String>))]
// #[sylvia::entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg, SvCustomQuery, String>)]
#[contract]
#[messages(cw1 as Cw1: custom(msg))]
#[messages(generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as Generic: custom(msg))]
#[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[sv::custom(msg=SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
// #[messages(cw1 as Cw1: custom(msg, query))]
#[messages(generic<ExecT, QueryT, SvCustomMsg> as Generic: custom(msg, query))]
// #[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomQuery, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[sv::custom(msg=CustomMsgT, query=CustomQueryT)]
impl<InstantiateT, ExecT, QueryT, MigrateT, CustomMsgT, CustomQueryT, FieldT>
GenericContract<InstantiateT, ExecT, QueryT, MigrateT, CustomMsgT, CustomQueryT, FieldT>
where
for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de,
ExecParam: CustomMsg + DeserializeOwned + 'static,
QueryParam: CustomMsg + DeserializeOwned + 'static,
MigrateParam: CustomMsg + DeserializeOwned + 'static,
RetType: CustomMsg + DeserializeOwned + 'static,
FieldType: 'static,
for<'msg_de> InstantiateT: CustomMsg + Deserialize<'msg_de> + 'msg_de,
ExecT: CustomMsg + DeserializeOwned + 'static,
QueryT: CustomMsg + DeserializeOwned + 'static,
MigrateT: CustomMsg + DeserializeOwned + 'static,
CustomMsgT: CustomMsg + DeserializeOwned + 'static,
CustomQueryT: CustomQuery + 'static,
FieldT: 'static,
{
pub const fn new() -> Self {
Self {
Expand All @@ -54,42 +51,42 @@ where
#[msg(instantiate)]
pub fn instantiate(
&self,
_ctx: InstantiateCtx,
_msg: InstantiateParam,
) -> StdResult<Response<SvCustomMsg>> {
_ctx: InstantiateCtx<CustomQueryT>,
_msg: InstantiateT,
) -> StdResult<Response<CustomMsgT>> {
Ok(Response::new())
}

#[msg(exec)]
pub fn contract_execute(
&self,
_ctx: ExecCtx,
_msg: ExecParam,
) -> StdResult<Response<SvCustomMsg>> {
_ctx: ExecCtx<CustomQueryT>,
_msg: ExecT,
) -> StdResult<Response<CustomMsgT>> {
Ok(Response::new())
}

#[msg(query)]
pub fn contract_query(
&self,
_ctx: QueryCtx,
_msg: QueryParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
pub fn contract_query(&self, _ctx: QueryCtx<CustomQueryT>, _msg: QueryT) -> StdResult<String> {
Ok(String::default())
}

#[msg(migrate)]
pub fn migrate(
&self,
_ctx: MigrateCtx,
_msg: MigrateParam,
) -> StdResult<Response<SvCustomMsg>> {
_ctx: MigrateCtx<CustomQueryT>,
_msg: MigrateT,
) -> StdResult<Response<CustomMsgT>> {
Ok(Response::new())
}

#[allow(dead_code)]
#[msg(reply)]
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response<SvCustomMsg>> {
fn reply(
&self,
_ctx: ReplyCtx<CustomQueryT>,
_reply: Reply,
) -> StdResult<Response<CustomMsgT>> {
Ok(Response::new())
}
}
Expand All @@ -98,17 +95,18 @@ where
mod tests {
use super::sv::multitest_utils::CodeId;
use sylvia::multitest::App;
use sylvia::types::SvCustomMsg;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

#[test]
fn generic_contract() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id: CodeId<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
super::SvCustomMsg,
super::SvCustomMsg,
SvCustomQuery,
String,
_,
> = CodeId::store_code(&app);
Expand Down
Loading

0 comments on commit d3d7a3c

Please sign in to comment.