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 20, 2023
1 parent d1c69d8 commit 56021f3
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 77 deletions.
51 changes: 34 additions & 17 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ 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,
SvCustomQuery,
};
use sylvia::{contract, schemars};

Expand All @@ -16,6 +17,7 @@ pub struct GenericContract<
QueryParam,
MigrateParam,
RetType,
CtxType,
FieldType,
> {
_field: Item<'static, FieldType>,
Expand All @@ -25,23 +27,33 @@ pub struct GenericContract<
QueryParam,
MigrateParam,
RetType,
CtxType,
)>,
}

#[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>))]
#[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<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as Generic: custom(msg, query))]
#[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomQuery, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, CtxQuery, FieldType>
GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
CtxQuery,
FieldType,
>
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,
CtxQuery: CustomQuery + 'static,
FieldType: 'static,
{
pub const fn new() -> Self {
Expand All @@ -54,7 +66,7 @@ where
#[msg(instantiate)]
pub fn instantiate(
&self,
_ctx: InstantiateCtx,
_ctx: InstantiateCtx<SvCustomQuery>,
_msg: InstantiateParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
Expand All @@ -63,7 +75,7 @@ where
#[msg(exec)]
pub fn contract_execute(
&self,
_ctx: ExecCtx,
_ctx: ExecCtx<SvCustomQuery>,
_msg: ExecParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
Expand All @@ -72,24 +84,28 @@ where
#[msg(query)]
pub fn contract_query(
&self,
_ctx: QueryCtx,
_ctx: QueryCtx<SvCustomQuery>,
_msg: QueryParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
) -> StdResult<String> {
Ok(String::default())
}

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

#[allow(dead_code)]
#[msg(reply)]
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response<SvCustomMsg>> {
fn reply(
&self,
_ctx: ReplyCtx<SvCustomQuery>,
_reply: Reply,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}
}
Expand All @@ -98,17 +114,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
28 changes: 19 additions & 9 deletions examples/contracts/generic_contract/src/custom_and_generic.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use custom_and_generic::CustomAndGeneric;
use sylvia::contract;
use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg};
use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg, SvCustomQuery};

#[contract(module = crate::contract)]
#[messages(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=sylvia::types::SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
CustomAndGeneric<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, CtxQuery, FieldType>
CustomAndGeneric<
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomQuery,
sylvia::types::SvCustomMsg,
>
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
CtxQuery,
FieldType,
>
{
Expand All @@ -22,7 +28,7 @@ impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
#[msg(exec)]
fn custom_generic_execute(
&self,
_ctx: ExecCtx,
_ctx: ExecCtx<SvCustomQuery>,
_msgs: Vec<CosmosMsg<sylvia::types::SvCustomMsg>>,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
Expand All @@ -31,7 +37,7 @@ impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
#[msg(query)]
fn custom_generic_query(
&self,
_ctx: QueryCtx,
_ctx: QueryCtx<SvCustomQuery>,
_msg: sylvia::types::SvCustomMsg,
) -> StdResult<SvCustomMsg> {
Ok(SvCustomMsg {})
Expand All @@ -42,17 +48,21 @@ impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
mod tests {
use super::sv::test_utils::CustomAndGeneric;
use crate::contract::sv::multitest_utils::CodeId;
use sylvia::{multitest::App, types::SvCustomMsg};
use sylvia::{
multitest::App,
types::{SvCustomMsg, SvCustomQuery},
};

#[test]
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id = CodeId::<
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
String,
_,
>::store_code(&app);
Expand Down
15 changes: 10 additions & 5 deletions examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use sylvia::types::{ExecCtx, QueryCtx};

#[contract(module = crate::contract)]
#[messages(cw1 as Cw1)]
#[sv::custom(msg=sylvia::types::SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType> Cw1
#[sv::custom(msg=sylvia::types::SvCustomMsg, query=sylvia::types::SvCustomQuery)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, CtxQuery, FieldType> Cw1
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
CtxQuery,
FieldType,
>
{
Expand All @@ -39,17 +40,21 @@ mod tests {
use super::sv::test_utils::Cw1;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::{CosmosMsg, Empty};
use sylvia::{multitest::App, types::SvCustomMsg};
use sylvia::{
multitest::App,
types::{SvCustomMsg, SvCustomQuery},
};

#[test]
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id = CodeId::<
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
String,
_,
>::store_code(&app);
Expand Down
8 changes: 5 additions & 3 deletions examples/contracts/generic_contract/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg};
#[contract(module = crate::contract)]
#[messages(generic as Generic)]
#[sv::custom(msg=SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, CtxQuery, FieldType>
Generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
CtxQuery,
FieldType,
>
{
Expand Down Expand Up @@ -48,17 +49,18 @@ mod tests {
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::CosmosMsg;
use sylvia::multitest::App;
use sylvia::types::SvCustomMsg;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

#[test]
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id: CodeId<
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomQuery,
String,
_,
> = CodeId::store_code(&app);
Expand Down
18 changes: 11 additions & 7 deletions examples/contracts/generic_iface_on_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{Response, StdResult};
use sylvia::types::{InstantiateCtx, SvCustomMsg};
use sylvia::types::{InstantiateCtx, SvCustomMsg, SvCustomQuery};
use sylvia::{contract, schemars};

#[cfg(not(feature = "library"))]
Expand All @@ -9,25 +9,29 @@ pub struct NonGenericContract;

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
#[messages(generic<SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg> as Generic: custom(msg))]
#[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[messages(cw1 as Cw1: custom(msg))]
#[messages(generic<SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg> as Generic: custom(msg, query))]
#[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomQuery, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[messages(cw1 as Cw1: custom(msg, query))]
/// Required if interface returns generic `Response`
#[sv::custom(msg=SvCustomMsg)]
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
impl NonGenericContract {
pub const fn new() -> Self {
Self
}

#[msg(instantiate)]
pub fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response<SvCustomMsg>> {
pub fn instantiate(
&self,
_ctx: InstantiateCtx<SvCustomQuery>,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}
}

#[cfg(test)]
mod tests {
use cosmwasm_std::{CosmosMsg, Empty};
use sylvia::types::SvCustomQuery;
use sylvia::{multitest::App, types::SvCustomMsg};

use super::NonGenericContract;
Expand All @@ -38,7 +42,7 @@ mod tests {
#[test]
fn mt_helpers() {
let _ = NonGenericContract::new();
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id = super::sv::multitest_utils::CodeId::store_code(&app);

let owner = "owner";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use custom_and_generic::CustomAndGeneric;
use sylvia::contract;
use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg};
use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg, SvCustomQuery};

#[contract(module = crate::contract)]
#[messages(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=sylvia::types::SvCustomMsg)]
impl CustomAndGeneric<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
#[sv::custom(msg=sylvia::types::SvCustomMsg, query=SvCustomQuery)]
impl CustomAndGeneric<SvCustomMsg, SvCustomMsg, SvCustomQuery, sylvia::types::SvCustomMsg>
for crate::contract::NonGenericContract
{
type Error = StdError;

#[msg(exec)]
fn custom_generic_execute(
&self,
_ctx: ExecCtx,
_ctx: ExecCtx<SvCustomQuery>,
_msgs: Vec<CosmosMsg<sylvia::types::SvCustomMsg>>,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
Expand All @@ -23,7 +23,7 @@ impl CustomAndGeneric<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
#[msg(query)]
fn custom_generic_query(
&self,
_ctx: QueryCtx,
_ctx: QueryCtx<SvCustomQuery>,
_msg: sylvia::types::SvCustomMsg,
) -> StdResult<SvCustomMsg> {
Ok(SvCustomMsg {})
Expand Down
Loading

0 comments on commit 56021f3

Please sign in to comment.