use boot_core::{BootError, Contract, IndexResponse, TxHandler, TxResponse};
use cosmwasm_std::{Addr, Binary, Empty, Uint128};
use cw20::{BalanceResponse, Cw20Coin, MinterResponse};
use cw_multi_test::ContractWrapper;
use crate::CwPlusContract;
use boot_core::Daemon;
use cw20_base::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
pub type Cw20<Chain> = CwPlusContract<Chain, ExecuteMsg, InstantiateMsg, QueryMsg, Empty>;
impl<Chain: TxHandler + Clone> Cw20<Chain>
where
TxResponse<Chain>: IndexResponse,
{
pub fn new(id: &str, chain: &Chain) -> Self {
let crate_path = env!("CARGO_MANIFEST_DIR");
let file_path = &format!("{}{}", crate_path, "/cw-artifacts/cw20_base.wasm");
Self(
Contract::new(id, chain)
.with_mock(Box::new(ContractWrapper::new_with_empty(
cw20_base::contract::execute,
cw20_base::contract::instantiate,
cw20_base::contract::query,
)))
.with_wasm_path(file_path),
)
}
pub fn send(
&self,
msg: Binary,
amount: u128,
contract: String,
) -> Result<TxResponse<Chain>, BootError> {
let msg = ExecuteMsg::Send {
contract,
amount: Uint128::new(amount),
msg,
};
self.execute(&msg, None)
}
pub fn transfer(
&self,
amount: u128,
recipient: String,
) -> Result<TxResponse<Chain>, BootError> {
let msg = ExecuteMsg::Transfer {
recipient,
amount: amount.into(),
};
self.execute(&msg, None)
}
pub fn create_new<T: Into<Uint128>>(
&self,
minter: &Addr,
balance: T,
) -> Result<TxResponse<Chain>, BootError> {
let msg = InstantiateMsg {
decimals: 6,
mint: Some(MinterResponse {
cap: None,
minter: minter.to_string(),
}),
symbol: "TEST".into(),
name: self.0.id.to_string(),
initial_balances: vec![Cw20Coin {
address: minter.to_string(),
amount: balance.into(),
}],
marketing: None,
};
self.instantiate(&msg, Some(minter), None)
}
pub fn balance(&self, address: &Addr) -> Result<Uint128, BootError> {
let bal: BalanceResponse = self.query(&QueryMsg::Balance {
address: address.to_string(),
})?;
Ok(bal.balance)
}
pub fn test_generic(&self, sender: &Addr) -> Result<(), BootError> {
let resp = self.create_new(sender, 420u128)?;
println!("events: {:?}", resp.events());
let new_balance = self.balance(sender)?;
assert_eq!(420u128, new_balance.u128());
self.execute(
&cw20::Cw20ExecuteMsg::Burn {
amount: 96u128.into(),
},
None,
)?;
let token_info: cw20::TokenInfoResponse =
self.query(&cw20_base::msg::QueryMsg::TokenInfo {})?;
println!("token_info: {:?}", token_info);
Ok(())
}
}
impl Cw20<Daemon> {
pub fn upload_required(&self) -> Result<bool, BootError> {
let daemon: Daemon = self.chain();
daemon.is_contract_hash_identical(&self.id)
}
}