From 0b883d0a76b2c61bfca4efe7219cf29c9940e673 Mon Sep 17 00:00:00 2001 From: Lars Lubkoll <11710767+lubkoll@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:05:50 +0200 Subject: [PATCH] Add cw-orch --- .github/workflows/babylon_vault.yml | 2 +- smart-contracts/Cargo.lock | 1 + .../contracts/babylon-vault/Cargo.toml | 1 + .../contracts/babylon-vault/src/contract.rs | 8 ----- .../contracts/babylon-vault/src/lib.rs | 3 ++ .../contracts/babylon-vault/src/msg.rs | 8 +++-- .../babylon-vault/src/tests/instantiate.rs | 21 ++++++++++++ .../contracts/babylon-vault/src/tests/mod.rs | 2 ++ .../babylon-vault/src/tests/setup.rs | 34 +++++++++++++++++++ 9 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs create mode 100644 smart-contracts/contracts/babylon-vault/src/tests/mod.rs create mode 100644 smart-contracts/contracts/babylon-vault/src/tests/setup.rs diff --git a/.github/workflows/babylon_vault.yml b/.github/workflows/babylon_vault.yml index 0b35060aa..4c508b739 100644 --- a/.github/workflows/babylon_vault.yml +++ b/.github/workflows/babylon_vault.yml @@ -12,7 +12,7 @@ on: push: branches: - main - - feat/babylon + - feat/babylon-* paths: - 'smart-contracts/contracts/babylon-vault/Cargo.toml' - 'smart-contracts/contracts/babylon-vault/**.rs' diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 2816a2f67..7385f1b4a 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -1404,6 +1404,7 @@ version = "0.0.1" dependencies = [ "cosmwasm-schema 1.5.4", "cosmwasm-std", + "cw-orch 0.23.0", "cw-storage-plus 1.2.0", "cw2 1.1.2", "thiserror", diff --git a/smart-contracts/contracts/babylon-vault/Cargo.toml b/smart-contracts/contracts/babylon-vault/Cargo.toml index 11290aad4..9813b9344 100644 --- a/smart-contracts/contracts/babylon-vault/Cargo.toml +++ b/smart-contracts/contracts/babylon-vault/Cargo.toml @@ -29,3 +29,4 @@ cosmwasm-schema = { workspace = true } cw-storage-plus = { workspace = true } thiserror = { workspace = true } cw2 = { workspace = true } +cw-orch = { workspace = true } diff --git a/smart-contracts/contracts/babylon-vault/src/contract.rs b/smart-contracts/contracts/babylon-vault/src/contract.rs index 83a0e3b87..55981168f 100644 --- a/smart-contracts/contracts/babylon-vault/src/contract.rs +++ b/smart-contracts/contracts/babylon-vault/src/contract.rs @@ -30,11 +30,3 @@ pub fn execute(_deps: DepsMut, _env: Env, _info: MessageInfo, _msg: ExecuteMsg) pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> VaultResult { Ok(Binary::default()) } - -#[cfg(test)] -mod tests { - #[test] - fn dummy_test() { - assert!(true); - } -} diff --git a/smart-contracts/contracts/babylon-vault/src/lib.rs b/smart-contracts/contracts/babylon-vault/src/lib.rs index a0cd84f22..575c7c3ce 100644 --- a/smart-contracts/contracts/babylon-vault/src/lib.rs +++ b/smart-contracts/contracts/babylon-vault/src/lib.rs @@ -2,4 +2,7 @@ pub mod contract; mod error; pub mod msg; +#[cfg(test)] +mod tests; + pub use crate::error::VaultError; diff --git a/smart-contracts/contracts/babylon-vault/src/msg.rs b/smart-contracts/contracts/babylon-vault/src/msg.rs index 72bbb479d..9e04dba9f 100644 --- a/smart-contracts/contracts/babylon-vault/src/msg.rs +++ b/smart-contracts/contracts/babylon-vault/src/msg.rs @@ -5,13 +5,17 @@ use cosmwasm_std::{Timestamp, Uint128}; pub struct InstantiateMsg {} #[cw_serde] +#[derive(cw_orch::ExecuteFns)] pub enum ExecuteMsg { // permission-less methods + #[cw_orch(payable)] Deposit {}, Withdraw {}, Claim {}, // owner methods - RegisterLst { denom: String }, + RegisterLst { + denom: String, + }, } #[cw_serde] @@ -21,7 +25,7 @@ pub struct Claim { } #[cw_serde] -#[derive(QueryResponses)] +#[derive(cw_orch::QueryFns, QueryResponses)] pub enum QueryMsg { #[returns(Vec)] Pending { address: String }, diff --git a/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs b/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs new file mode 100644 index 000000000..cc07ff296 --- /dev/null +++ b/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs @@ -0,0 +1,21 @@ +use crate::msg::InstantiateMsg; +use crate::tests::setup::create_test_vault; +use cosmwasm_std::Event; +use cw_orch::contract::interface_traits::CwOrchInstantiate; + +#[test] +fn test_instantiate() { + let env = create_test_vault(); + let vault = env.vault; + + let result = vault.instantiate(&InstantiateMsg {}, None, None); + assert!(result.is_ok()); + let response = result.unwrap(); + assert_eq!(response.events.len(), 1); + assert_eq!( + response.events[0], + Event::new("instantiate") + .add_attribute("_contract_address", "contract0") + .add_attribute("code_id", "1") + ); +} diff --git a/smart-contracts/contracts/babylon-vault/src/tests/mod.rs b/smart-contracts/contracts/babylon-vault/src/tests/mod.rs new file mode 100644 index 000000000..c7c48b2c4 --- /dev/null +++ b/smart-contracts/contracts/babylon-vault/src/tests/mod.rs @@ -0,0 +1,2 @@ +pub mod instantiate; +pub mod setup; diff --git a/smart-contracts/contracts/babylon-vault/src/tests/setup.rs b/smart-contracts/contracts/babylon-vault/src/tests/setup.rs new file mode 100644 index 000000000..dcd4c4b19 --- /dev/null +++ b/smart-contracts/contracts/babylon-vault/src/tests/setup.rs @@ -0,0 +1,34 @@ +use crate::{ + contract::{execute, instantiate, query}, + msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, +}; +use cosmwasm_std::{testing::MockApi, Addr, Empty}; +use cw_orch::interface; +use cw_orch::mock::Mock; +use cw_orch::prelude::*; +use cw_orch::{contract::interface_traits::CwOrchUpload, mock::MockBase}; + +pub const SENDER: &str = "sender"; + +#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, Empty)] +pub struct Vault; + +impl Uploadable for Vault { + fn wrapper() -> Box> { + Box::new(ContractWrapper::new_with_empty(execute, instantiate, query)) + } +} + +pub struct TestEnv { + pub chain: MockBase, + pub vault: Vault>, +} + +pub fn create_test_vault() -> TestEnv { + let sender = Addr::unchecked(SENDER); + let chain = Mock::new(&sender); + + let vault: Vault = Vault::new("vault", chain.clone()); + vault.upload().unwrap(); + TestEnv { chain, vault } +}