Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mock state env id #313

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions cw-orch-daemon/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)]
Expand Down
13 changes: 12 additions & 1 deletion cw-orch-daemon/src/sync/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
}
}
}
3 changes: 1 addition & 2 deletions cw-orch/examples/async_daemon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cosmwasm_std::Addr;
use counter_contract::CounterContract;
use cw_orch_daemon::DaemonAsync;
use cw_orch_mock::Mock;
Expand Down Expand Up @@ -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());

Expand Down
20 changes: 18 additions & 2 deletions cw-orch/src/osmosis_test_tube/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -184,7 +186,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"))
}
}

Expand Down Expand Up @@ -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)?,
Expand Down
47 changes: 7 additions & 40 deletions packages/cw-orch-core/src/build.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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!()
}
}
13 changes: 6 additions & 7 deletions packages/cw-orch-core/src/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions packages/cw-orch-core/src/contract/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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>,
Expand Down
16 changes: 14 additions & 2 deletions packages/cw-orch-core/src/environment/cosmwasm_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion packages/cw-orch-core/src/environment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
16 changes: 0 additions & 16 deletions packages/cw-orch-core/src/environment/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand All @@ -102,10 +94,6 @@ 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()
}
}

impl<S: StateInterface> StateInterface for Arc<S> {
Expand All @@ -132,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()
}
}
24 changes: 22 additions & 2 deletions packages/cw-orch-mock/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -168,7 +168,7 @@ impl Mock<MockState> {
Mock::new_custom(sender, MockState::new())
}

pub fn with_chain_id(sender: impl Into<String>, chain_id: &str) -> Self {
pub fn new_with_chain_id(sender: impl Into<String>, chain_id: &str) -> Self {
let chain = Mock::new_custom(sender, MockState::new());
chain
.app
Expand All @@ -177,6 +177,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> {
Expand Down Expand Up @@ -347,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)
Expand Down
Loading