From 5582e0b2ecb5a6aec443ced7bb6941bcc6583c9e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 22 Jun 2023 23:27:59 +0200 Subject: [PATCH] Instantiate contract trio in multitest --- Cargo.lock | 1 + contracts/consumer/converter/Cargo.toml | 2 + contracts/consumer/converter/src/multitest.rs | 136 +++++++++++++++++- ...ual_staking.rs => virtual_staking_mock.rs} | 14 ++ 4 files changed, 152 insertions(+), 1 deletion(-) rename contracts/consumer/converter/src/multitest/{virtual_staking.rs => virtual_staking_mock.rs} (92%) diff --git a/Cargo.lock b/Cargo.lock index 3ef4a360..475772cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,6 +586,7 @@ dependencies = [ "cw2", "derivative", "mesh-apis", + "mesh-simple-price-feed", "schemars", "serde", "sylvia", diff --git a/contracts/consumer/converter/Cargo.toml b/contracts/consumer/converter/Cargo.toml index 7e2dce29..5a4bf6b8 100644 --- a/contracts/consumer/converter/Cargo.toml +++ b/contracts/consumer/converter/Cargo.toml @@ -34,6 +34,8 @@ serde = { workspace = true } thiserror = { workspace = true } [dev-dependencies] +mesh-simple-price-feed = { workspace = true, features = ["mt"] } + cw-multi-test = { workspace = true } test-case = { workspace = true } derivative = { workspace = true } diff --git a/contracts/consumer/converter/src/multitest.rs b/contracts/consumer/converter/src/multitest.rs index 9e398580..fa58c792 100644 --- a/contracts/consumer/converter/src/multitest.rs +++ b/contracts/consumer/converter/src/multitest.rs @@ -1 +1,135 @@ -mod virtual_staking; +mod virtual_staking_mock; + +use cosmwasm_std::{Addr, Decimal}; + +use sylvia::multitest::App; + +use crate::contract; +// use crate::error::ContractError; +// use crate::msg; + +const JUNO: &str = "ujuno"; + +#[test] +fn instantiation() { + let app = App::default(); + + let owner = "Sunny"; // Owner of the staking contract (i. e. the vault contract) + let admin = "The man"; + let discount = Decimal::percent(40); // 1 OSMO worth of JUNO should give 0.6 OSMO of stake + let native_per_foreign = Decimal::percent(50); // 1 JUNO is worth 0.5 OSMO + + let price_feed_code = + mesh_simple_price_feed::contract::multitest_utils::CodeId::store_code(&app); + let virtual_staking_code = virtual_staking_mock::multitest_utils::CodeId::store_code(&app); + let converter_code = contract::multitest_utils::CodeId::store_code(&app); + + let price_feed = price_feed_code + .instantiate(native_per_foreign, None) + .with_label("Price Feed") + .call(owner) + .unwrap(); + + let converter = converter_code + .instantiate( + price_feed.contract_addr.to_string(), + discount, + JUNO.to_owned(), + virtual_staking_code.code_id(), + ) + .with_label("Juno Converter") + .with_admin(admin) + .call(owner) + .unwrap(); + + // check the config + let config = converter.config().unwrap(); + assert_eq!(config.price_feed, price_feed.contract_addr.to_string()); + assert_eq!(config.adjustment, Decimal::percent(60)); + assert!(!config.virtual_staking.is_empty()); + + // let's check we passed the admin here properly + let vs_info = app + .app() + .wrap() + .query_wasm_contract_info(&config.virtual_staking) + .unwrap(); + assert_eq!(vs_info.admin, Some(admin.to_string())); + + // let's query virtual staking to find the owner + let virtual_staking_addr = Addr::unchecked(&config.virtual_staking); + let virtual_staking = virtual_staking_mock::multitest_utils::VirtualStakingMockProxy::new( + virtual_staking_addr, + &app, + ); + let vs_config = virtual_staking.config().unwrap(); + assert_eq!(vs_config.converter, converter.contract_addr.to_string()); +} + +/* +#[test] +fn receiving_stake() { + let owner = "vault"; // Owner of the staking contract (i. e. the vault contract) + + let user1 = "user1"; // One who wants to local stake + let user2 = "user2"; // Another one who wants to local stake + + let validator = "validator1"; // Validator to stake on + + // Fund the vault + let app = MtApp::new(|router, _api, storage| { + router + .bank + .init_balance(storage, &Addr::unchecked(owner), coins(300, OSMO)) + .unwrap(); + }); + let app = App::new(app); + + // Contracts setup + let staking_proxy_code = local_staking_proxy::multitest_utils::CodeId::store_code(&app); + let staking_code = contract::multitest_utils::CodeId::store_code(&app); + + let staking = staking_code + .instantiate(OSMO.to_owned(), staking_proxy_code.code_id()) + .with_label("Staking") + .call(owner) + .unwrap(); + + // Check that no proxy exists for user1 yet + let err = staking.proxy_by_owner(user1.to_owned()).unwrap_err(); + assert!(matches!( + err, + ContractError::Std(StdError::GenericErr { .. }) // Addr not found + )); + + // Receive some stake on behalf of user1 for validator + let stake_msg = to_binary(&msg::StakeMsg { + validator: validator.to_owned(), + }) + .unwrap(); + staking + .local_staking_api_proxy() + .receive_stake(user1.to_owned(), stake_msg) + .with_funds(&coins(100, OSMO)) + .call(owner) // called from vault + .unwrap(); + + let proxy1 = staking.proxy_by_owner(user1.to_owned()).unwrap().proxy; + // Reverse query + assert_eq!( + staking.owner_by_proxy(proxy1.clone()).unwrap(), + OwnerByProxyResponse { + owner: user1.to_owned(), + } + ); + + // Check that funds are in the proxy contract + assert_eq!( + app.app() + .wrap() + .query_balance(proxy1.clone(), OSMO) + .unwrap(), + coin(100, OSMO) + ); +} +*/ diff --git a/contracts/consumer/converter/src/multitest/virtual_staking.rs b/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs similarity index 92% rename from contracts/consumer/converter/src/multitest/virtual_staking.rs rename to contracts/consumer/converter/src/multitest/virtual_staking_mock.rs index 42892ac7..380087bb 100644 --- a/contracts/consumer/converter/src/multitest/virtual_staking.rs +++ b/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs @@ -60,6 +60,14 @@ impl VirtualStakingMock<'_> { Ok(Response::new()) } + #[msg(query)] + fn config(&self, ctx: QueryCtx) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + let denom = cfg.denom; + let converter = cfg.converter.into_string(); + Ok(ConfigResponse { denom, converter }) + } + #[msg(query)] fn stake(&self, ctx: QueryCtx, validator: String) -> Result { let stake = self @@ -89,6 +97,12 @@ pub struct AllStakeResponse { pub stakes: Vec<(String, Uint128)>, } +#[cw_serde] +pub struct ConfigResponse { + pub denom: String, + pub converter: String, +} + #[contract] #[messages(virtual_staking_api as VirtualStakingApi)] impl VirtualStakingApi for VirtualStakingMock<'_> {