use crate::CwEnv;
use crate::{
error::BootError, index_response::IndexResponse, state::StateInterface, tx_handler::TxResponse,
};
use cosmwasm_std::{Addr, Coin, CustomQuery, Empty};
use cw_multi_test::Contract as TestContract;
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
#[derive(Clone)]
pub struct Contract<Chain: CwEnv> {
pub id: String,
pub(crate) source: ContractCodeReference,
pub(crate) chain: Chain,
}
#[derive(Default)]
pub struct ContractCodeReference<ExecT = Empty, QueryT = Empty>
where
ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
QueryT: CustomQuery + DeserializeOwned + 'static,
{
pub wasm_code_path: Option<String>,
pub contract_endpoints: Option<Box<dyn TestContract<ExecT, QueryT>>>,
}
impl Clone for ContractCodeReference {
fn clone(&self) -> Self {
Self {
wasm_code_path: self.wasm_code_path.clone(),
contract_endpoints: None,
}
}
}
impl<Chain: CwEnv + Clone> Contract<Chain> {
pub fn new(id: impl ToString, chain: Chain) -> Self {
Contract {
id: id.to_string(),
chain,
source: ContractCodeReference::default(),
}
}
pub fn get_chain(&self) -> &Chain {
&self.chain
}
pub fn with_wasm_path(mut self, path: impl ToString) -> Self {
self.source.wasm_code_path = Some(path.to_string());
self
}
pub fn with_mock(mut self, mock_contract: Box<dyn TestContract<Empty, Empty>>) -> Self {
self.source.contract_endpoints = Some(mock_contract);
self
}
pub fn set_mock(&mut self, mock_contract: Box<dyn TestContract<Empty, Empty>>) {
self.source.contract_endpoints = Some(mock_contract);
}
pub fn with_address(self, address: Option<&Addr>) -> Self {
if let Some(address) = address {
self.set_address(address)
}
self
}
pub fn execute<E: Serialize + Debug>(
&self,
msg: &E,
coins: Option<&[Coin]>,
) -> Result<TxResponse<Chain>, BootError> {
log::info!("Executing {:#?} on {}", msg, self.id);
let resp = self
.chain
.execute(msg, coins.unwrap_or(&[]), &self.address()?);
log::debug!("execute response: {:?}", resp);
resp.map_err(Into::into)
}
pub fn instantiate<I: Serialize + Debug>(
&self,
msg: &I,
admin: Option<&Addr>,
coins: Option<&[Coin]>,
) -> Result<TxResponse<Chain>, BootError> {
log::info!("Instantiating {} with msg {:#?}", self.id, msg);
let resp = self
.chain
.instantiate(
self.code_id()?,
msg,
Some(&self.id),
admin,
coins.unwrap_or(&[]),
)
.map_err(Into::into)?;
let contract_address = resp.instantiated_contract_address()?;
self.set_address(&contract_address);
log::info!("Instantiated {} with address {}", self.id, contract_address);
log::debug!("Instantiate response: {:?}", resp);
Ok(resp)
}
pub fn upload(&mut self) -> Result<TxResponse<Chain>, BootError> {
log::info!("Uploading {}", self.id);
let resp = self.chain.upload(&mut self.source).map_err(Into::into)?;
let code_id = resp.uploaded_code_id()?;
self.set_code_id(code_id);
log::info!("uploaded {} with code id {}", self.id, code_id);
log::debug!("Upload response: {:?}", resp);
Ok(resp)
}
pub fn query<Q: Serialize + Debug, T: Serialize + DeserializeOwned + Debug>(
&self,
query_msg: &Q,
) -> Result<T, BootError> {
log::info!("Querying {:#?} on {}", query_msg, self.id);
let resp = self
.chain
.query(query_msg, &self.address()?)
.map_err(Into::into)?;
log::debug!("Query response: {:?}", resp);
Ok(resp)
}
pub fn migrate<M: Serialize + Debug>(
&self,
migrate_msg: &M,
new_code_id: u64,
) -> Result<TxResponse<Chain>, BootError> {
log::info!("Migrating {:?} to code_id {}", self.id, new_code_id);
self.chain
.migrate(migrate_msg, new_code_id, &self.address()?)
.map_err(Into::into)
}
pub fn address(&self) -> Result<Addr, BootError> {
self.chain.state().get_address(&self.id)
}
pub fn code_id(&self) -> Result<u64, BootError> {
self.chain.state().get_code_id(&self.id)
}
pub fn set_address(&self, address: &Addr) {
self.chain.state().set_address(&self.id, address)
}
pub fn set_code_id(&self, code_id: u64) {
self.chain.state().set_code_id(&self.id, code_id)
}
}