Skip to content

Commit

Permalink
test: Impl interfaces on generic_contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Oct 24, 2023
1 parent 63317a5 commit c286aed
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 34 deletions.
3 changes: 3 additions & 0 deletions examples/Cargo.lock

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

3 changes: 3 additions & 0 deletions examples/contracts/generic_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ cw-storage-plus = "1.0"
cw-utils = "1.0"
serde = { version = "1.0", default-features = false, features = ["derive"] }
sylvia = { path = "../../../sylvia" }
cw1 = { path = "../../interfaces/cw1" }
generic = { path = "../../interfaces/generic" }
custom-and-generic = { path = "../../interfaces/custom-and-generic" }

[dev-dependencies]
anyhow = "1.0"
Expand Down
11 changes: 5 additions & 6 deletions examples/contracts/generic_contract/src/bin/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use cosmwasm_schema::write_api;

#[cfg(not(tarpaulin_include))]
fn main() {
use generic_contract::contract::{
ContractExecMsg, ContractQueryMsg, ExternalMsg, InstantiateMsg,
};
use generic_contract::contract::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};
use sylvia::types::SvCustomMsg;

write_api! {
instantiate: InstantiateMsg<ExternalMsg>,
execute: ContractExecMsg<ExternalMsg>,
query: ContractQueryMsg<ExternalMsg>,
instantiate: InstantiateMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg>,
query: ContractQueryMsg<SvCustomMsg>,
}
}
70 changes: 42 additions & 28 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Reply, Response, StdResult};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{CustomMsg, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx};
use sylvia::types::{
CustomMsg, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx, SvCustomMsg,
};
use sylvia::{contract, schemars};

#[cw_serde]
pub struct ExternalMsg;
impl cosmwasm_std::CustomMsg for ExternalMsg {}

pub struct GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>(
std::marker::PhantomData<(
InstantiateParam,
Expand All @@ -20,6 +17,10 @@ pub struct GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam
);

#[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>
where
Expand All @@ -34,65 +35,78 @@ where
}

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

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

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

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

#[allow(dead_code)]
#[msg(reply)]
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}
}

#[cfg(test)]
mod tests {
use cosmwasm_std::Empty;
use sylvia::multitest::App;

use crate::contract::ExternalMsg;
use sylvia::{multitest::App, types::SvCustomMsg};

#[test]
fn generic_contract() {
use super::multitest_utils::CodeId;
let app = App::default();
let code_id: CodeId<
cw_multi_test::BasicApp<Empty, Empty>,
ExternalMsg,
ExternalMsg,
ExternalMsg,
super::ExternalMsg,
super::ExternalMsg,
> = CodeId::store_code(&app);
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let code_id = CodeId::<
_,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
sylvia::types::SvCustomMsg,
>::store_code(&app);

let owner = "owner";

let contract = code_id
.instantiate(ExternalMsg {})
.instantiate(SvCustomMsg {})
.with_label("GenericContract")
.with_admin(owner)
.call(owner)
.unwrap();

contract.execute(ExternalMsg).call(owner).unwrap();
contract.query(ExternalMsg).unwrap();
contract.contract_execute(SvCustomMsg).call(owner).unwrap();
contract.contract_query(SvCustomMsg).unwrap();
contract
.migrate(ExternalMsg)
.migrate(SvCustomMsg)
.call(owner, code_id.code_id())
.unwrap();
}
Expand Down
77 changes: 77 additions & 0 deletions examples/contracts/generic_contract/src/custom_and_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use custom_and_generic::CustomAndGeneric;
use sylvia::contract;
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>
CustomAndGeneric<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg>
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
>
{
type Error = StdError;

#[msg(exec)]
fn custom_generic_execute(
&self,
_ctx: ExecCtx,
_msgs: Vec<CosmosMsg<sylvia::types::SvCustomMsg>>,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}

#[msg(query)]
fn custom_generic_query(
&self,
_ctx: QueryCtx,
_msg: sylvia::types::SvCustomMsg,
) -> StdResult<SvCustomMsg> {
Ok(SvCustomMsg {})
}
}

#[cfg(test)]
mod tests {
use super::test_utils::CustomAndGeneric;
use crate::contract::multitest_utils::CodeId;
use sylvia::{multitest::App, types::SvCustomMsg};

#[test]
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let code_id = CodeId::<
_,
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
>::store_code(&app);

let owner = "owner";

let contract = code_id
.instantiate(SvCustomMsg {})
.with_label("GenericContract")
.with_admin(owner)
.call(owner)
.unwrap();

contract
.custom_and_generic_proxy()
.custom_generic_execute(vec![])
.call(owner)
.unwrap();
contract
.custom_and_generic_proxy()
.custom_generic_query(SvCustomMsg {})
.unwrap();
}
}
70 changes: 70 additions & 0 deletions examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::contract;
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
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
>
{
type Error = StdError;

#[msg(exec)]
fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
}

#[msg(query)]
fn can_execute(
&self,
_ctx: QueryCtx,
_sender: String,
_msg: CosmosMsg,
) -> StdResult<CanExecuteResp> {
Ok(CanExecuteResp::default())
}
}

#[cfg(test)]
mod tests {
use super::test_utils::Cw1;
use crate::contract::multitest_utils::CodeId;
use cosmwasm_std::{CosmosMsg, Empty};
use sylvia::{multitest::App, types::SvCustomMsg};

#[test]
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let code_id = CodeId::<
_,
SvCustomMsg,
sylvia::types::SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
sylvia::types::SvCustomMsg,
>::store_code(&app);

let owner = "owner";

let contract = code_id
.instantiate(SvCustomMsg {})
.with_label("GenericContract")
.with_admin(owner)
.call(owner)
.unwrap();

contract.cw1_proxy().execute(vec![]).call(owner).unwrap();
contract
.cw1_proxy()
.can_execute("sender".to_owned(), CosmosMsg::Custom(Empty {}))
.unwrap();
}
}
Loading

0 comments on commit c286aed

Please sign in to comment.