-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Move generic contracts and interfaces to examples
- Loading branch information
Showing
19 changed files
with
614 additions
and
406 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown --lib" | ||
wasm-debug = "build --target wasm32-unknown-unknown --lib" | ||
unit-test = "test --lib" | ||
integration-test = "test --test integration" | ||
schema = "run --bin schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "generic_contract" | ||
version = { workspace = true } | ||
authors = ["Jan Woźniak <[email protected]>"] | ||
edition = { workspace = true } | ||
description = "Example of generic contract" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/CosmWasm/sylvia" | ||
homepage = "https://cosmwasm.com" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
library = [] | ||
tests = ["library", "cw-multi-test", "anyhow"] | ||
|
||
[dependencies] | ||
anyhow = { version = "1.0", optional = true } | ||
cosmwasm-schema = "1.2" | ||
cosmwasm-std = { version = "1.3", features = ["staking"] } | ||
cw-multi-test = { version = "0.16", optional = true } | ||
cw-storage-plus = "1.0" | ||
cw-utils = "1.0" | ||
serde = { version = "1.0", default-features = false, features = ["derive"] } | ||
sylvia = { path = "../../../sylvia" } | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0" | ||
cw-multi-test = "0.16" | ||
sylvia = { path = "../../../sylvia", features = ["mt"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
#[cfg(not(tarpaulin_include))] | ||
fn main() { | ||
use generic_contract::contract::{ | ||
ContractExecMsg, ContractQueryMsg, ExternalMsg, InstantiateMsg, | ||
}; | ||
|
||
write_api! { | ||
instantiate: InstantiateMsg<ExternalMsg>, | ||
execute: ContractExecMsg<ExternalMsg>, | ||
query: ContractQueryMsg<ExternalMsg>, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
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::{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, | ||
ExecParam, | ||
QueryParam, | ||
MigrateParam, | ||
RetType, | ||
)>, | ||
); | ||
|
||
#[contract] | ||
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType> | ||
GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType> | ||
where | ||
for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de, | ||
for<'exec> ExecParam: CustomMsg + DeserializeOwned + 'exec, | ||
for<'exec> QueryParam: CustomMsg + DeserializeOwned + 'exec, | ||
for<'exec> MigrateParam: CustomMsg + DeserializeOwned + 'exec, | ||
for<'ret> RetType: CustomMsg + DeserializeOwned + 'ret, | ||
{ | ||
pub const fn new() -> Self { | ||
Self(std::marker::PhantomData) | ||
} | ||
|
||
#[msg(instantiate)] | ||
pub fn instantiate(&self, _ctx: InstantiateCtx, _msg: InstantiateParam) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
#[msg(exec)] | ||
pub fn execute(&self, _ctx: ExecCtx, _msg: ExecParam) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
#[msg(query)] | ||
pub fn query(&self, _ctx: QueryCtx, _msg: QueryParam) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
#[msg(migrate)] | ||
pub fn migrate(&self, _ctx: MigrateCtx, _msg: MigrateParam) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[msg(reply)] | ||
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cosmwasm_std::Empty; | ||
use sylvia::multitest::App; | ||
|
||
use crate::contract::ExternalMsg; | ||
|
||
#[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 owner = "owner"; | ||
|
||
let contract = code_id | ||
.instantiate(ExternalMsg {}) | ||
.with_label("GenericContract") | ||
.with_admin(owner) | ||
.call(owner) | ||
.unwrap(); | ||
|
||
contract.execute(ExternalMsg).call(owner).unwrap(); | ||
contract.query(ExternalMsg).unwrap(); | ||
contract | ||
.migrate(ExternalMsg) | ||
.call(owner, code_id.code_id()) | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod contract; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "generic_iface_on_contract" | ||
version = { workspace = true } | ||
authors = ["Jan Woźniak <[email protected]>"] | ||
edition = { workspace = true } | ||
description = "Generic interfaces implemented on non generic contract" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/CosmWasm/sylvia" | ||
homepage = "https://cosmwasm.com" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
library = [] | ||
tests = ["library", "cw-multi-test", "anyhow"] | ||
|
||
[dependencies] | ||
anyhow = { version = "1.0", optional = true } | ||
cosmwasm-schema = "1.2" | ||
cosmwasm-std = { version = "1.3", features = ["staking"] } | ||
cw-multi-test = { version = "0.16", optional = true } | ||
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" | ||
cw-multi-test = "0.16" | ||
sylvia = { path = "../../../sylvia", features = ["mt"] } |
81 changes: 81 additions & 0 deletions
81
examples/contracts/generic_iface_on_contract/src/contract.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use cosmwasm_std::{Response, StdResult}; | ||
use sylvia::types::{InstantiateCtx, SvCustomMsg}; | ||
use sylvia::{contract, schemars}; | ||
|
||
pub struct NonGenericContract; | ||
|
||
#[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))] | ||
/// Required if interface returns generic `Response` | ||
#[sv::custom(msg=SvCustomMsg)] | ||
impl NonGenericContract { | ||
pub const fn new() -> Self { | ||
Self | ||
} | ||
|
||
#[msg(instantiate)] | ||
pub fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response<SvCustomMsg>> { | ||
Ok(Response::new()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cosmwasm_std::{CosmosMsg, Empty}; | ||
use sylvia::{multitest::App, types::SvCustomMsg}; | ||
|
||
use super::NonGenericContract; | ||
use crate::custom_and_generic::test_utils::CustomAndGeneric; | ||
use crate::cw1::test_utils::Cw1; | ||
use crate::generic::test_utils::Generic; | ||
|
||
#[test] | ||
fn mt_helpers() { | ||
let _ = NonGenericContract::new(); | ||
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {}); | ||
let code_id = super::multitest_utils::CodeId::store_code(&app); | ||
|
||
let owner = "owner"; | ||
|
||
let contract = code_id | ||
.instantiate() | ||
.with_label("Cw1Contract") | ||
.call(owner) | ||
.unwrap(); | ||
|
||
// Non custom non generic interface | ||
contract | ||
.cw1_proxy() | ||
.can_execute("sender".to_owned(), CosmosMsg::Custom(Empty {})) | ||
.unwrap(); | ||
contract | ||
.cw1_proxy() | ||
.execute(vec![CosmosMsg::Custom(Empty {})]) | ||
.call(owner) | ||
.unwrap(); | ||
|
||
// Non-Custom generic Interface | ||
contract | ||
.generic_proxy() | ||
.generic_query(SvCustomMsg {}) | ||
.unwrap(); | ||
contract | ||
.generic_proxy() | ||
.generic_exec(vec![CosmosMsg::Custom(SvCustomMsg {})]) | ||
.call(owner) | ||
.unwrap(); | ||
|
||
// Custom generic Interface | ||
contract | ||
.custom_and_generic_proxy() | ||
.custom_generic_query(SvCustomMsg {}) | ||
.unwrap(); | ||
contract | ||
.custom_and_generic_proxy() | ||
.custom_generic_execute(vec![CosmosMsg::Custom(SvCustomMsg {})]) | ||
.call(owner) | ||
.unwrap(); | ||
} | ||
} |
Oops, something went wrong.