use cosmwasm_std::Coin;
use cw4_group::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
ContractError,
};
use osmosis_test_tube::{
osmosis_std::types::cosmwasm::wasm::v1::MsgExecuteContractResponse, Account, Module,
OsmosisTestApp, RunnerError, RunnerExecuteResult, SigningAccount, Wasm,
};
use serde::de::DeserializeOwned;
use std::fmt::Debug;
use std::path::PathBuf;
#[derive(Debug)]
pub struct Cw4Group<'a> {
pub app: &'a OsmosisTestApp,
pub code_id: u64,
pub contract_addr: String,
}
impl<'a> Cw4Group<'a> {
pub fn new(
app: &'a OsmosisTestApp,
instantiate_msg: &InstantiateMsg,
signer: &SigningAccount,
) -> Result<Self, RunnerError> {
let wasm = Wasm::new(app);
let code_id = wasm
.store_code(&Self::get_wasm_byte_code(), None, signer)?
.data
.code_id;
let contract_addr = wasm
.instantiate(
code_id,
&instantiate_msg,
Some(&signer.address()),
None,
&[],
signer,
)?
.data
.address;
Ok(Self {
app,
code_id,
contract_addr,
})
}
pub fn new_with_values(
app: &'a OsmosisTestApp,
code_id: u64,
contract_addr: String,
) -> Result<Self, RunnerError> {
Ok(Self {
app,
code_id,
contract_addr,
})
}
pub fn upload(app: &OsmosisTestApp, signer: &SigningAccount) -> Result<u64, RunnerError> {
let wasm = Wasm::new(app);
let code_id = wasm
.store_code(&Self::get_wasm_byte_code(), None, signer)?
.data
.code_id;
Ok(code_id)
}
pub fn execute(
&self,
execute_msg: &ExecuteMsg,
funds: &[Coin],
signer: &SigningAccount,
) -> RunnerExecuteResult<MsgExecuteContractResponse> {
let wasm = Wasm::new(self.app);
wasm.execute(&self.contract_addr, execute_msg, funds, signer)
}
pub fn query<T>(&self, query_msg: &QueryMsg) -> Result<T, RunnerError>
where
T: DeserializeOwned,
{
let wasm = Wasm::new(self.app);
wasm.query(&self.contract_addr, query_msg)
}
fn get_wasm_byte_code() -> Vec<u8> {
let manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let byte_code = std::fs::read(
manifest_path
.join("..")
.join("..")
.join("artifacts")
.join("cw4_group.wasm"),
);
match byte_code {
Ok(byte_code) => byte_code,
Err(_) => std::fs::read(
manifest_path
.join("..")
.join("..")
.join("artifacts")
.join("cw4_group-aarch64.wasm"),
)
.unwrap(),
}
}
pub fn execute_error(err: ContractError) -> RunnerError {
RunnerError::ExecuteError {
msg: format!(
"failed to execute message; message index: 0: {}: execute wasm contract failed",
err
),
}
}
}