From 3f9af3bd18361d47def7a01b4beb6ce1b382fac3 Mon Sep 17 00:00:00 2001 From: Kayanski Date: Wed, 8 Nov 2023 12:16:18 +0100 Subject: [PATCH] Added cw-orch --- .../project/contracts/counter/Cargo.toml | 5 +++ .../contracts/counter/src/interface.rs | 45 +++++++++++++++++++ .../project/contracts/counter/src/lib.rs | 3 ++ .../project/contracts/counter/src/msg.rs | 2 + 4 files changed, 55 insertions(+) create mode 100644 templates/project/contracts/counter/src/interface.rs diff --git a/templates/project/contracts/counter/Cargo.toml b/templates/project/contracts/counter/Cargo.toml index fb83804..36b9bed 100644 --- a/templates/project/contracts/counter/Cargo.toml +++ b/templates/project/contracts/counter/Cargo.toml @@ -31,6 +31,8 @@ overflow-checks = true backtraces = ["cosmwasm-std/backtraces"] # use library feature to disable all instantiate/execute/query exports library = [] +# use interface feature to expose structure for contract orchestration +interface = ["dep:cw-orch"] [package.metadata.scripts] optimize = """docker run --rm -v "$(pwd)":/code \ @@ -49,5 +51,8 @@ schemars = "0.8.8" serde = { version = "1.0.137", default-features = false, features = ["derive"] } thiserror = { version = "1.0.31" } +cw-orch = { version = "0.18.1", optional = true } + [dev-dependencies] cw-multi-test = "0.13.2" +counter = {path = ".", features=["interface"]} \ No newline at end of file diff --git a/templates/project/contracts/counter/src/interface.rs b/templates/project/contracts/counter/src/interface.rs new file mode 100644 index 0000000..5bcda66 --- /dev/null +++ b/templates/project/contracts/counter/src/interface.rs @@ -0,0 +1,45 @@ +use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; +use cosmwasm_std::Empty; +use cw_orch::{interface, prelude::*}; + +#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, Empty)] +pub struct Counter; + +impl Uploadable for Counter { + /// Return the path to the wasm file corresponding to the contract + fn wasm(&self) -> WasmPath { + artifacts_dir_from_workspace!() + .find_wasm_path("{{crate_name}}") + .unwrap() + } + /// Returns a CosmWasm contract wrapper + fn wrapper(&self) -> Box> { + Box::new(ContractWrapper::new_with_empty( + crate::contract::execute, + crate::contract::instantiate, + crate::contract::query, + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::msg::{ExecuteMsgFns, QueryMsgFns}; + use cw_orch::anyhow; + + #[test] + fn contract_logic() -> anyhow::Result<()> { + let mock = Mock::new(&Addr::unchecked("sender")); + let contract = Counter::new("project-name", mock); + contract.upload()?; + + contract.instantiate(&InstantiateMsg { count: 7 }, None, None)?; + assert_eq!(contract.get_count()?.count, 7); + + contract.increment()?; + assert_eq!(contract.get_count()?.count, 8); + + Ok(()) + } +} diff --git a/templates/project/contracts/counter/src/lib.rs b/templates/project/contracts/counter/src/lib.rs index d6185c4..233ef7e 100644 --- a/templates/project/contracts/counter/src/lib.rs +++ b/templates/project/contracts/counter/src/lib.rs @@ -6,3 +6,6 @@ pub mod msg; pub mod state; pub use crate::error::ContractError; + +#[cfg(feature = "interface")] +pub mod interface; diff --git a/templates/project/contracts/counter/src/msg.rs b/templates/project/contracts/counter/src/msg.rs index 0edfa32..f62c93f 100644 --- a/templates/project/contracts/counter/src/msg.rs +++ b/templates/project/contracts/counter/src/msg.rs @@ -6,6 +6,7 @@ pub struct InstantiateMsg { } #[cw_serde] +#[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))] pub enum ExecuteMsg { Increment {}, Reset { count: i32 }, @@ -13,6 +14,7 @@ pub enum ExecuteMsg { #[cw_serde] #[derive(QueryResponses)] +#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))] pub enum QueryMsg { // GetCount returns the current count as a json-encoded number #[returns(GetCountResponse)]