Skip to content

Commit

Permalink
test: Support generic field in contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 7, 2023
1 parent 6ad43ab commit ab90eee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
30 changes: 22 additions & 8 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{Reply, Response, StdResult};
use cw_storage_plus::Item;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{
Expand All @@ -9,33 +10,45 @@ use sylvia::{contract, schemars};
#[cfg(not(feature = "library"))]
use sylvia::entry_points;

pub struct GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>(
std::marker::PhantomData<(
pub struct GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
FieldType,
> {
_field: Item<'static, FieldType>,
_phantom: std::marker::PhantomData<(
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
)>,
);
}

#[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg>))]
#[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg, 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>
GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, 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,
FieldType: 'static,
{
pub const fn new() -> Self {
Self(std::marker::PhantomData)
pub fn new() -> Self {
Self {
_field: Item::new("field"),
_phantom: std::marker::PhantomData,
}
}

#[msg(instantiate)]
Expand Down Expand Up @@ -96,6 +109,7 @@ mod tests {
SvCustomMsg,
super::SvCustomMsg,
super::SvCustomMsg,
String,
_,
> = CodeId::store_code(&app);

Expand Down
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(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=sylvia::types::SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
CustomAndGeneric<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
FieldType,
>
{
type Error = StdError;
Expand Down Expand Up @@ -52,6 +53,7 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
String,
_,
>::store_code(&app);

Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ 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> Cw1
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType> Cw1
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
FieldType,
>
{
type Error = StdError;
Expand Down Expand Up @@ -49,6 +50,7 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
String,
_,
>::store_code(&app);

Expand Down
4 changes: 3 additions & 1 deletion 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>
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, FieldType>
Generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
FieldType,
>
{
type Error = StdError;
Expand Down Expand Up @@ -58,6 +59,7 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
String,
_,
> = CodeId::store_code(&app);

Expand Down

0 comments on commit ab90eee

Please sign in to comment.