From b01acfccfca863a4126b3e2bf52597efc7927f38 Mon Sep 17 00:00:00 2001 From: Kayanski <kowalski.kowalskin@gmail.com> Date: Fri, 19 Jan 2024 08:47:59 +0100 Subject: [PATCH 1/4] Added mock state env id --- cw-orch/src/osmosis_test_tube/core.rs | 2 +- packages/cw-orch-mock/src/core.rs | 11 +++++++++-- packages/cw-orch-mock/src/state.rs | 24 ++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/cw-orch/src/osmosis_test_tube/core.rs b/cw-orch/src/osmosis_test_tube/core.rs index 15a5e979c..55e9e4d6a 100644 --- a/cw-orch/src/osmosis_test_tube/core.rs +++ b/cw-orch/src/osmosis_test_tube/core.rs @@ -184,7 +184,7 @@ impl OsmosisTestTube<MockState> { /// Unlike for mocks, the accounts are created by the struct and not provided by the client /// Make sure to use only valid bech32 osmosis addresses, not mock pub fn new(init_coins: Vec<Coin>) -> Self { - Self::new_custom(init_coins, MockState::new()) + Self::new_custom(init_coins, MockState::new_with_chain_id("osmosis-1")) } } diff --git a/packages/cw-orch-mock/src/core.rs b/packages/cw-orch-mock/src/core.rs index 152081c6b..0d4158058 100644 --- a/packages/cw-orch-mock/src/core.rs +++ b/packages/cw-orch-mock/src/core.rs @@ -131,8 +131,8 @@ impl Mock<MockState> { Mock::new_custom(sender, MockState::new()) } - pub fn with_chain_id(sender: &Addr, chain_id: &str) -> Self { - let chain = Mock::new_custom(sender, MockState::new()); + pub fn new_with_chain_id(sender: &Addr, chain_id: &str) -> Self { + let chain = Mock::new_custom(sender, MockState::new_with_chain_id(chain_id)); chain .app .borrow_mut() @@ -140,6 +140,13 @@ impl Mock<MockState> { chain } + + pub fn with_chain_id(&mut self, chain_id: &str) { + self.state.borrow_mut().set_chain_id(chain_id); + self.app + .borrow_mut() + .update_block(|b| b.chain_id = chain_id.to_string()); + } } impl<S: StateInterface> Mock<S> { diff --git a/packages/cw-orch-mock/src/state.rs b/packages/cw-orch-mock/src/state.rs index 262b29c81..ef5d3a2e3 100644 --- a/packages/cw-orch-mock/src/state.rs +++ b/packages/cw-orch-mock/src/state.rs @@ -13,6 +13,8 @@ pub struct MockState { pub code_ids: HashMap<String, u64>, /// Deployed contract addresses pub addresses: HashMap<String, Addr>, + /// Chain id of the mocked chain + pub chain_id: String, } impl MockState { @@ -21,8 +23,26 @@ impl MockState { Self { addresses: HashMap::new(), code_ids: HashMap::new(), + chain_id: mock_env().block.chain_id, } } + /// Creates a new empty mock state + pub fn new_with_chain_id(chain_id: &str) -> Self { + Self { + addresses: HashMap::new(), + code_ids: HashMap::new(), + chain_id: chain_id.to_string(), + } + } + + pub fn with_chain_id(mut self, chain_id: &str) -> Self { + self.chain_id = chain_id.to_string(); + self + } + + pub fn set_chain_id(&mut self, chain_id: &str) { + self.chain_id = chain_id.to_string(); + } } impl Default for MockState { @@ -66,8 +86,8 @@ impl StateInterface for MockState { } fn deploy_details(&self) -> DeployDetails { - let chain_id: String = mock_env().block.chain_id; - let chain_name: String = chain_id.rsplitn(2, '-').collect::<Vec<_>>()[1].to_string(); + let chain_id = self.chain_id.clone(); + let chain_name = chain_id.rsplitn(2, '-').collect::<Vec<_>>()[1].to_string(); DeployDetails { chain_id, chain_name, From df57fd8181326c38ec17f6b596e8ff37a47472b2 Mon Sep 17 00:00:00 2001 From: Kayanski <kowalski.kowalskin@gmail.com> Date: Fri, 19 Jan 2024 09:01:02 +0100 Subject: [PATCH 2/4] Format --- cw-orch-daemon/src/log.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cw-orch-daemon/src/log.rs b/cw-orch-daemon/src/log.rs index 9bbf5f942..cceb990b5 100644 --- a/cw-orch-daemon/src/log.rs +++ b/cw-orch-daemon/src/log.rs @@ -8,7 +8,7 @@ static LOGS_DISABLED: Once = Once::new(); // Prints a warning if log is disabled for the application pub fn print_if_log_disabled() -> Result<(), DaemonError> { - LOGS_DISABLED.call_once(|| { + LOGS_DISABLED.call_once(|| { // Here we check for logging capabilities. if !log::log_enabled!(log::Level::Info) && !CwOrchEnvVars::load().map(|env|env.disable_logs_message).unwrap_or(false){ println!( From a6c9f41e125b48c67b6409b2ed5b1349d9a9243a Mon Sep 17 00:00:00 2001 From: Kayanski <kowalski.kowalskin@gmail.com> Date: Wed, 24 Jan 2024 12:03:53 +0100 Subject: [PATCH 3/4] Added environment info trait instead of deploy_details --- cw-orch-daemon/src/state.rs | 10 +--- cw-orch-daemon/src/sync/core.rs | 13 ++++- cw-orch/examples/async_daemon.rs | 3 +- cw-orch/src/osmosis_test_tube/core.rs | 18 ++++++- packages/cw-orch-core/src/build.rs | 47 +++---------------- packages/cw-orch-core/src/contract/deploy.rs | 13 +++-- packages/cw-orch-core/src/contract/paths.rs | 4 +- .../src/environment/cosmwasm_environment.rs | 16 ++++++- packages/cw-orch-core/src/environment/mod.rs | 4 +- .../cw-orch-core/src/environment/state.rs | 12 ----- packages/cw-orch-mock/src/core.rs | 15 +++++- packages/cw-orch-mock/src/state.rs | 15 +----- 12 files changed, 78 insertions(+), 92 deletions(-) diff --git a/cw-orch-daemon/src/state.rs b/cw-orch-daemon/src/state.rs index d6d6928f4..8e507d6ba 100644 --- a/cw-orch-daemon/src/state.rs +++ b/cw-orch-daemon/src/state.rs @@ -4,7 +4,7 @@ use crate::{channel::GrpcChannel, networks::ChainKind}; use cosmwasm_std::Addr; use cw_orch_core::{ env::default_state_folder, - environment::{DeployDetails, StateInterface}, + environment::StateInterface, log::{connectivity_target, local_target}, CwEnvError, CwOrchEnvVars, }; @@ -229,14 +229,6 @@ impl StateInterface for DaemonState { } Ok(store) } - - fn deploy_details(&self) -> DeployDetails { - DeployDetails { - chain_id: self.chain_data.chain_id.to_string(), - chain_name: self.chain_data.chain_name.clone(), - deployment_id: self.deployment_id.clone(), - } - } } #[cfg(test)] diff --git a/cw-orch-daemon/src/sync/core.rs b/cw-orch-daemon/src/sync/core.rs index 1d416d194..77d3b0b8c 100644 --- a/cw-orch-daemon/src/sync/core.rs +++ b/cw-orch-daemon/src/sync/core.rs @@ -10,7 +10,7 @@ use cosmrs::tendermint::Time; use cosmwasm_std::{Addr, Coin}; use cw_orch_core::{ contract::{interface_traits::Uploadable, WasmPath}, - environment::{BankQuerier, ChainState, TxHandler}, + environment::{BankQuerier, ChainState, EnvironmentInfo, EnvironmentQuerier, TxHandler}, }; use cw_orch_traits::stargate::Stargate; use serde::{de::DeserializeOwned, Serialize}; @@ -251,3 +251,14 @@ impl Stargate for Daemon { ) } } + +impl EnvironmentQuerier for Daemon { + fn env_info(&self) -> EnvironmentInfo { + let state = &self.daemon.sender.daemon_state; + EnvironmentInfo { + chain_id: state.chain_data.chain_id.to_string(), + chain_name: state.chain_data.chain_name.clone(), + deployment_id: state.deployment_id.clone(), + } + } +} diff --git a/cw-orch/examples/async_daemon.rs b/cw-orch/examples/async_daemon.rs index 67ee7deee..d8c6c8f45 100644 --- a/cw-orch/examples/async_daemon.rs +++ b/cw-orch/examples/async_daemon.rs @@ -1,4 +1,3 @@ -use cosmwasm_std::Addr; use counter_contract::CounterContract; use cw_orch_daemon::DaemonAsync; use cw_orch_mock::Mock; @@ -26,7 +25,7 @@ pub async fn main() -> anyhow::Result<()> { .await?; // Uploading a contract is very simple - let counter = CounterContract::new(Mock::new(&Addr::unchecked("sender"))); + let counter = CounterContract::new(Mock::new("sender")); let upload_res = daemon.upload(&counter).await; assert!(upload_res.is_ok()); diff --git a/cw-orch/src/osmosis_test_tube/core.rs b/cw-orch/src/osmosis_test_tube/core.rs index ad9140bb0..709f55c2f 100644 --- a/cw-orch/src/osmosis_test_tube/core.rs +++ b/cw-orch/src/osmosis_test_tube/core.rs @@ -4,7 +4,9 @@ use crate::contract::WasmPath; use crate::prelude::Uploadable; use cosmwasm_std::{coin, Addr, ContractInfoResponse, StdResult}; -use cw_orch_core::environment::{BankQuerier, BankSetter, WasmCodeQuerier}; +use cw_orch_core::environment::{ + BankQuerier, BankSetter, EnvironmentInfo, EnvironmentQuerier, WasmCodeQuerier, +}; use cw_orch_traits::stargate::Stargate; use cosmwasm_std::{Binary, BlockInfo, Coin, Timestamp, Uint128}; @@ -455,6 +457,20 @@ impl WasmCodeQuerier for OsmosisTestTube { } } +impl EnvironmentQuerier for OsmosisTestTube { + fn env_info(&self) -> EnvironmentInfo { + let block = self.block_info().unwrap(); + let chain_id = block.chain_id; + let chain_name = chain_id.rsplitn(2, '-').collect::<Vec<_>>()[1].to_string(); + + EnvironmentInfo { + chain_id, + chain_name, + deployment_id: "default".to_string(), + } + } +} + fn to_cosmwasm_coin(c: osmosis_std::types::cosmos::base::v1beta1::Coin) -> StdResult<Coin> { Ok(Coin { amount: Uint128::from_str(&c.amount)?, diff --git a/packages/cw-orch-core/src/build.rs b/packages/cw-orch-core/src/build.rs index 28ec6c6a4..2b7495929 100644 --- a/packages/cw-orch-core/src/build.rs +++ b/packages/cw-orch-core/src/build.rs @@ -1,11 +1,11 @@ //! # Build Postfix Format //! Used to specify the build-postfix for contracts in the `Uploadable` trait. -use crate::environment::{ChainState, StateInterface}; +use crate::environment::{EnvironmentInfo, EnvironmentQuerier}; /// Build name used for building the contract. /// See the [Abstract Optimizer](https://github.com/AbstractSDK/rust-optimizer). -pub enum BuildPostfix<'a, T: ChainState = ()> { +pub enum BuildPostfix<'a, T: EnvironmentQuerier = ()> { /// Default, doesn't look for anything but the contract name. None, /// Uses the chain to figure out the chain name. I.e. "uni-6" = "juno-1" -> "juno" post-fix on build. @@ -16,52 +16,19 @@ pub enum BuildPostfix<'a, T: ChainState = ()> { Custom(String), } -impl<T: ChainState> From<BuildPostfix<'_, T>> for String { +impl<T: EnvironmentQuerier> From<BuildPostfix<'_, T>> for String { fn from(value: BuildPostfix<T>) -> Self { match value { BuildPostfix::None => "".to_string(), - BuildPostfix::ChainName(chain) => chain.state().deploy_details().chain_name, - BuildPostfix::ChainID(chain) => chain.state().deploy_details().chain_id, + BuildPostfix::ChainName(chain) => chain.env_info().chain_name, + BuildPostfix::ChainID(chain) => chain.env_info().chain_id, BuildPostfix::Custom(postfix) => postfix, } } } -impl ChainState for () { - type Out = (); - fn state(&self) -> Self::Out {} -} - -impl StateInterface for () { - fn get_address(&self, _contract_id: &str) -> Result<cosmwasm_std::Addr, crate::CwEnvError> { - panic!() - } - - fn set_address(&mut self, _contract_id: &str, _address: &cosmwasm_std::Addr) { - panic!() - } - - fn get_code_id(&self, _contract_id: &str) -> Result<u64, crate::CwEnvError> { - panic!() - } - - fn set_code_id(&mut self, _contract_id: &str, _code_id: u64) { - panic!() - } - - fn get_all_addresses( - &self, - ) -> Result<std::collections::HashMap<String, cosmwasm_std::Addr>, crate::CwEnvError> { - panic!() - } - - fn get_all_code_ids( - &self, - ) -> Result<std::collections::HashMap<String, u64>, crate::CwEnvError> { - panic!() - } - - fn deploy_details(&self) -> crate::environment::DeployDetails { +impl EnvironmentQuerier for () { + fn env_info(&self) -> EnvironmentInfo { panic!() } } diff --git a/packages/cw-orch-core/src/contract/deploy.rs b/packages/cw-orch-core/src/contract/deploy.rs index 882b28079..5ed13eaa9 100644 --- a/packages/cw-orch-core/src/contract/deploy.rs +++ b/packages/cw-orch-core/src/contract/deploy.rs @@ -14,7 +14,6 @@ use std::path::PathBuf; use crate::env::CwOrchEnvVars; use crate::environment::CwEnv; -use crate::environment::StateInterface; use crate::CwEnvError; use super::interface_traits::ContractInstance; @@ -193,14 +192,14 @@ pub trait Deploy<Chain: CwEnv>: Sized { for contract in all_contracts { // We set the code_id and/or address of the contract in question if they are not present already - let deploy_details = contract.get_chain().state().deploy_details(); + let env_info = contract.get_chain().env_info(); // We load the file // We try to get the code_id for the contract if contract.code_id().is_err() { let code_id = state - .get(deploy_details.chain_name.clone()) + .get(env_info.chain_name.clone()) .unwrap_or(&Value::Null) - .get(deploy_details.chain_id.to_string()) + .get(env_info.chain_id.to_string()) .unwrap_or(&Value::Null) .get("code_ids") .unwrap_or(&Value::Null) @@ -216,11 +215,11 @@ pub trait Deploy<Chain: CwEnv>: Sized { if contract.address().is_err() { // Try and get the code id from file let address = state - .get(deploy_details.chain_name.clone()) + .get(env_info.chain_name.clone()) .unwrap_or(&Value::Null) - .get(deploy_details.chain_id.to_string()) + .get(env_info.chain_id.to_string()) .unwrap_or(&Value::Null) - .get(deploy_details.deployment_id) + .get(env_info.deployment_id) .unwrap_or(&Value::Null) .get(contract.id()); diff --git a/packages/cw-orch-core/src/contract/paths.rs b/packages/cw-orch-core/src/contract/paths.rs index 9a655b588..736105560 100644 --- a/packages/cw-orch-core/src/contract/paths.rs +++ b/packages/cw-orch-core/src/contract/paths.rs @@ -58,7 +58,7 @@ mod wasm_path { mod artifacts_dir { use super::WasmPath; use crate::{ - build::BuildPostfix, env::ARTIFACTS_DIR_ENV_NAME, environment::ChainState, + build::BuildPostfix, env::ARTIFACTS_DIR_ENV_NAME, environment::EnvironmentQuerier, error::CwEnvError, log::local_target, CwOrchEnvVars, }; @@ -154,7 +154,7 @@ mod artifacts_dir { /// Find a WASM file in the artifacts directory that contains the given contract name AND build post-fix. /// If a build with the post-fix is not found, the default build will be used. /// If none of the two are found, an error is returned. - pub fn find_wasm_path_with_build_postfix<T: ChainState>( + pub fn find_wasm_path_with_build_postfix<T: EnvironmentQuerier>( &self, name: &str, build_postfix: BuildPostfix<T>, diff --git a/packages/cw-orch-core/src/environment/cosmwasm_environment.rs b/packages/cw-orch-core/src/environment/cosmwasm_environment.rs index 8de7deecb..ba10e80d4 100644 --- a/packages/cw-orch-core/src/environment/cosmwasm_environment.rs +++ b/packages/cw-orch-core/src/environment/cosmwasm_environment.rs @@ -10,8 +10,8 @@ use serde::{de::DeserializeOwned, Serialize}; use std::fmt::Debug; /// Signals a supported execution environment for CosmWasm contracts -pub trait CwEnv: TxHandler + BankQuerier + WasmCodeQuerier + Clone {} -impl<T: TxHandler + BankQuerier + WasmCodeQuerier + Clone> CwEnv for T {} +pub trait CwEnv: TxHandler + BankQuerier + WasmCodeQuerier + EnvironmentQuerier + Clone {} +impl<T: TxHandler + BankQuerier + WasmCodeQuerier + EnvironmentQuerier + Clone> CwEnv for T {} /// Response type for actions on an environment pub type TxResponse<Chain> = <Chain as TxHandler>::Response; @@ -123,3 +123,15 @@ pub trait BankQuerier: TxHandler { /// Query total supply in the bank for a denom fn supply_of(&self, denom: impl Into<String>) -> Result<Coin, <Self as TxHandler>::Error>; } + +#[derive(Clone)] +pub struct EnvironmentInfo { + pub chain_id: String, + pub chain_name: String, + pub deployment_id: String, +} + +pub trait EnvironmentQuerier { + /// Get some details about the environment. + fn env_info(&self) -> EnvironmentInfo; +} diff --git a/packages/cw-orch-core/src/environment/mod.rs b/packages/cw-orch-core/src/environment/mod.rs index 9f731ff8d..3c9cf8569 100644 --- a/packages/cw-orch-core/src/environment/mod.rs +++ b/packages/cw-orch-core/src/environment/mod.rs @@ -3,7 +3,9 @@ mod index_response; mod mut_env; mod state; -pub use cosmwasm_environment::{BankQuerier, CwEnv, TxHandler, TxResponse, WasmCodeQuerier}; +pub use cosmwasm_environment::{ + BankQuerier, CwEnv, EnvironmentInfo, EnvironmentQuerier, TxHandler, TxResponse, WasmCodeQuerier, +}; pub use index_response::IndexResponse; pub use mut_env::{BankSetter, MutCwEnv}; pub use state::{ChainState, DeployDetails, StateInterface}; diff --git a/packages/cw-orch-core/src/environment/state.rs b/packages/cw-orch-core/src/environment/state.rs index f80a91bd6..4ecfcfdfe 100644 --- a/packages/cw-orch-core/src/environment/state.rs +++ b/packages/cw-orch-core/src/environment/state.rs @@ -32,10 +32,6 @@ pub trait StateInterface: Clone { /// Get all codes related to this deployment. fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError>; - - /// Get some details used for deployment on the current chain - /// This is used for - fn deploy_details(&self) -> DeployDetails; } /// Details about the chain and env you are deploying on @@ -72,10 +68,6 @@ impl<S: StateInterface> StateInterface for Rc<RefCell<S>> { fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError> { (**self).borrow().get_all_code_ids() } - - fn deploy_details(&self) -> DeployDetails { - (**self).borrow().deploy_details() - } } impl<S: StateInterface> StateInterface for Rc<S> { @@ -102,8 +94,4 @@ impl<S: StateInterface> StateInterface for Rc<S> { fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError> { (**self).get_all_code_ids() } - - fn deploy_details(&self) -> DeployDetails { - (**self).deploy_details() - } } diff --git a/packages/cw-orch-mock/src/core.rs b/packages/cw-orch-mock/src/core.rs index 3cc473a45..a0359c4a6 100644 --- a/packages/cw-orch-mock/src/core.rs +++ b/packages/cw-orch-mock/src/core.rs @@ -15,7 +15,7 @@ use serde::{de::DeserializeOwned, Serialize}; use cw_orch_core::{ contract::interface_traits::{ContractInstance, Uploadable}, environment::{BankQuerier, BankSetter, ChainState, IndexResponse, StateInterface}, - environment::{TxHandler, WasmCodeQuerier}, + environment::{EnvironmentInfo, EnvironmentQuerier, TxHandler, WasmCodeQuerier}, CwEnvError, }; @@ -354,6 +354,19 @@ impl<S: StateInterface> TxHandler for Mock<S> { } } +impl<S: StateInterface> EnvironmentQuerier for Mock<S> { + fn env_info(&self) -> EnvironmentInfo { + let block_info = self.block_info().unwrap(); + let chain_id = block_info.chain_id.clone(); + let chain_name = chain_id.rsplitn(2, '-').collect::<Vec<_>>()[1].to_string(); + EnvironmentInfo { + chain_id, + chain_name, + deployment_id: "default".to_string(), + } + } +} + impl WasmCodeQuerier for Mock { /// Returns the checksum of provided code_id /// Cw-multi-test implements a checksum based on the code_id (because it wan't access the wasm code) diff --git a/packages/cw-orch-mock/src/state.rs b/packages/cw-orch-mock/src/state.rs index ef5d3a2e3..e25b5db0f 100644 --- a/packages/cw-orch-mock/src/state.rs +++ b/packages/cw-orch-mock/src/state.rs @@ -1,8 +1,5 @@ use cosmwasm_std::{testing::mock_env, Addr}; -use cw_orch_core::{ - environment::{DeployDetails, StateInterface}, - CwEnvError, -}; +use cw_orch_core::{environment::StateInterface, CwEnvError}; use std::collections::HashMap; @@ -84,16 +81,6 @@ impl StateInterface for MockState { fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError> { Ok(self.code_ids.clone()) } - - fn deploy_details(&self) -> DeployDetails { - let chain_id = self.chain_id.clone(); - let chain_name = chain_id.rsplitn(2, '-').collect::<Vec<_>>()[1].to_string(); - DeployDetails { - chain_id, - chain_name, - deployment_id: "default".to_string(), - } - } } #[cfg(test)] From 38e701a86c80494140dbfb79236fe7d2d545b3f3 Mon Sep 17 00:00:00 2001 From: Kayanski <kowalski.kowalskin@gmail.com> Date: Wed, 24 Jan 2024 12:41:43 +0100 Subject: [PATCH 4/4] Removed deploy_details in state template impl --- packages/cw-orch-core/src/environment/state.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/cw-orch-core/src/environment/state.rs b/packages/cw-orch-core/src/environment/state.rs index a7a031c39..876f225f9 100644 --- a/packages/cw-orch-core/src/environment/state.rs +++ b/packages/cw-orch-core/src/environment/state.rs @@ -120,8 +120,4 @@ impl<S: StateInterface> StateInterface for Arc<S> { fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError> { (**self).get_all_code_ids() } - - fn deploy_details(&self) -> DeployDetails { - (**self).deploy_details() - } }