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

First impl of Mock and MockBech32 #325

Merged
merged 10 commits into from
Feb 13, 2024
2 changes: 1 addition & 1 deletion cw-orch-daemon/src/queriers/cosmwasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
Ok(c)
}

fn instantiate2_addr<I: serde::Serialize + std::fmt::Debug>(
fn instantiate2_addr(

Check warning on line 279 in cw-orch-daemon/src/queriers/cosmwasm.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/queriers/cosmwasm.rs#L279

Added line #L279 was not covered by tests
&self,
code_id: u64,
creator: impl Into<String>,
Expand Down
3 changes: 1 addition & 2 deletions cw-orch/src/osmosis_test_tube/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ impl<S: StateInterface> TxHandler for OsmosisTestTube<S> {
impl BankSetter for OsmosisTestTube {
type T = OsmosisTestTubeBankQuerier;

/// It's impossible to set the balance of an address directly in OsmosisTestTub
/// So for this implementation, we use a weird algorithm
/// It's impossible to set the balance of an address directly in OsmosisTestTube
fn set_balance(
&mut self,
_address: impl Into<String>,
Expand Down
2 changes: 1 addition & 1 deletion cw-orch/src/osmosis_test_tube/queriers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
Ok(c)
}

fn instantiate2_addr<I: serde::Serialize + std::fmt::Debug>(
fn instantiate2_addr(

Check warning on line 160 in cw-orch/src/osmosis_test_tube/queriers/wasm.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch/src/osmosis_test_tube/queriers/wasm.rs#L160

Added line #L160 was not covered by tests
&self,
code_id: u64,
creator: impl Into<String>,
Expand Down
2 changes: 1 addition & 1 deletion cw-orch/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use crate::environment::{
};

// Mock for testing
pub use crate::mock::Mock;
pub use crate::mock::{Mock, MockBech32};

// OsmosisTestTube for testing
#[cfg(feature = "osmosis-test-tube")]
Expand Down
2 changes: 1 addition & 1 deletion packages/cw-orch-core/src/contract/interface_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub trait ConditionalUpload<Chain: CwEnv>: CwOrchUpload<Chain> {
.wasm_querier()
.code_id_hash(latest_uploaded_code_id)
.map_err(Into::into)?;
let local_hash = chain.wasm_querier().local_hash(self)?;
let local_hash = Chain::Wasm::local_hash(self)?;
Ok(local_hash == on_chain_hash)
}

Expand Down
3 changes: 1 addition & 2 deletions packages/cw-orch-core/src/environment/queriers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ pub trait WasmQuerier: Querier {

/// Returns the checksum of the WASM file if the env supports it. Will re-upload every time if not supported.
fn local_hash<Chain: TxHandler + QueryHandler, T: Uploadable + ContractInstance<Chain>>(
&self,
contract: &T,
) -> Result<String, CwEnvError> {
contract.wasm().checksum()
}

fn instantiate2_addr<I: Serialize + std::fmt::Debug>(
fn instantiate2_addr(
&self,
code_id: u64,
creator: impl Into<String>,
Expand Down
3 changes: 3 additions & 0 deletions packages/cw-orch-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
str::ParseBoolError,
};

use cosmwasm_std::Instantiate2AddressError;
use thiserror::Error;

/// cw-orchestrator error wrapper using thiserror.
Expand All @@ -29,6 +30,8 @@ pub enum CwEnvError {
ParseIntError(#[from] ParseIntError),
#[error(transparent)]
ParseBoolError(#[from] ParseBoolError),
#[error(transparent)]
Instantiate2AddressError(#[from] Instantiate2AddressError),
#[error("File must be a wasm file")]
NotWasm,
#[error("Could not find wasm file with name {0} in artifacts:{1} dir")]
Expand Down
141 changes: 141 additions & 0 deletions packages/cw-orch-mock/src/bech32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use std::{cell::RefCell, rc::Rc};

use cosmwasm_std::{Addr, Coin, Uint128};
use cw_multi_test::{
addons::{MockAddressGenerator, MockApiBech32},
AppBuilder, WasmKeeper,
};
use cw_orch_core::{
environment::{BankQuerier, BankSetter, DefaultQueriers, StateInterface, TxHandler},
CwEnvError,
};
use cw_utils::NativeBalance;

use crate::{queriers::bank::MockBankQuerier, MockBase, MockBech32, MockState};

impl MockBase<MockApiBech32, MockState> {
/// Create a mock environment with the default mock state.
pub fn new(prefix: &'static str) -> Self {
MockBech32::new_custom(prefix, MockState::new())
}

pub fn new_with_chain_id(prefix: &'static str, chain_id: &str) -> Self {
let chain = MockBech32::new_custom(prefix, MockState::new());
chain
.app
.borrow_mut()
.update_block(|b| b.chain_id = chain_id.to_string());

chain
}

Check warning on line 30 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L22-L30

Added lines #L22 - L30 were not covered by tests
}

impl<S: StateInterface> MockBase<MockApiBech32, S> {
pub fn create_account(&self, account_name: impl Into<String>) -> Addr {
self.app.borrow().api().addr_make(&account_name.into())
}

Check warning on line 36 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L34-L36

Added lines #L34 - L36 were not covered by tests
}

impl Default for MockBase<MockApiBech32, MockState> {
fn default() -> Self {
MockBase::<MockApiBech32, MockState>::new_custom("mock", MockState::new())
}

Check warning on line 42 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L40-L42

Added lines #L40 - L42 were not covered by tests
}

impl<S: StateInterface> MockBase<MockApiBech32, S> {
/// Create a mock environment with a custom mock state.
/// The state is customizable by implementing the `StateInterface` trait on a custom struct and providing it on the custom constructor.
pub fn new_custom(prefix: &'static str, custom_state: S) -> Self {
let state = Rc::new(RefCell::new(custom_state));
let app = Rc::new(RefCell::new(
AppBuilder::new_custom()
.with_api(MockApiBech32::new(prefix))
.with_wasm(WasmKeeper::default().with_address_generator(MockAddressGenerator))
.build(|_, _, _| {}),
));

// We create an address internally
let sender = app.borrow().api().addr_make("sender");

Self { sender, state, app }
}
}

impl<S: StateInterface> MockBech32<S> {
/// Set the bank balance of an address.
pub fn set_balance(
&self,
address: &Addr,
amount: Vec<cosmwasm_std::Coin>,
) -> Result<(), CwEnvError> {
self.app
.borrow_mut()
.init_modules(|router, _, storage| router.bank.init_balance(storage, address, amount))
.map_err(Into::into)
}

Check warning on line 75 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L66-L75

Added lines #L66 - L75 were not covered by tests

/// Adds the bank balance of an address.
pub fn add_balance(
&self,
address: &Addr,
amount: Vec<cosmwasm_std::Coin>,
) -> Result<(), CwEnvError> {
let addr = &address;
let b = self.query_all_balances(addr)?;
let new_amount = NativeBalance(b) + NativeBalance(amount);
self.app
.borrow_mut()
.init_modules(|router, _, storage| {
router
.bank
.init_balance(storage, addr, new_amount.into_vec())
})
.map_err(Into::into)
}

Check warning on line 94 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L78-L94

Added lines #L78 - L94 were not covered by tests

/// Set the balance for multiple coins at once.
pub fn set_balances(
&self,
balances: &[(&Addr, &[cosmwasm_std::Coin])],
) -> Result<(), CwEnvError> {
self.app
.borrow_mut()
.init_modules(|router, _, storage| -> Result<(), CwEnvError> {
for (addr, coins) in balances {
router.bank.init_balance(storage, addr, coins.to_vec())?;

Check warning on line 105 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L97-L105

Added lines #L97 - L105 were not covered by tests
}
Ok(())
})
}

Check warning on line 109 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L107-L109

Added lines #L107 - L109 were not covered by tests

/// Query the (bank) balance of a native token for and address.
/// Returns the amount of the native token.
pub fn query_balance(&self, address: &Addr, denom: &str) -> Result<Uint128, CwEnvError> {
Ok(self
.bank_querier()
.balance(address, Some(denom.to_string()))?
.first()
.map(|c| c.amount)
.unwrap_or_default())
}

Check warning on line 120 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L113-L120

Added lines #L113 - L120 were not covered by tests

/// Fetch all the balances of an address.
pub fn query_all_balances(
&self,
address: &Addr,
) -> Result<Vec<cosmwasm_std::Coin>, CwEnvError> {
self.bank_querier().balance(address, None)
}

Check warning on line 128 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L123-L128

Added lines #L123 - L128 were not covered by tests
}

impl<S: StateInterface> BankSetter for MockBech32<S> {
type T = MockBankQuerier<MockApiBech32>;

fn set_balance(
&mut self,
address: impl Into<String>,
amount: Vec<Coin>,
) -> Result<(), <Self as TxHandler>::Error> {
(*self).set_balance(&Addr::unchecked(address), amount)
}

Check warning on line 140 in packages/cw-orch-mock/src/bech32.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/bech32.rs#L134-L140

Added lines #L134 - L140 were not covered by tests
}
Loading
Loading