wasm_test_2/
helpers.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use cosmwasm_std::{
5    to_json_binary, Addr, CosmosMsg, CustomQuery, Querier, QuerierWrapper, StdResult, WasmMsg,
6    WasmQuery,
7};
8
9use crate::msg::{ExecuteMsg, GetCountResponse, QueryMsg};
10
11/// CwTemplateContract is a wrapper around Addr that provides a lot of helpers
12/// for working with this.
13#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
14pub struct CwTemplateContract(pub Addr);
15
16impl CwTemplateContract {
17    pub fn addr(&self) -> Addr {
18        self.0.clone()
19    }
20
21    pub fn call<T: Into<ExecuteMsg>>(&self, msg: T) -> StdResult<CosmosMsg> {
22        let msg = to_json_binary(&msg.into())?;
23        Ok(WasmMsg::Execute {
24            contract_addr: self.addr().into(),
25            msg,
26            funds: vec![],
27        }
28        .into())
29    }
30
31    /// Get Count
32    pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetCountResponse>
33    where
34        Q: Querier,
35        T: Into<String>,
36        CQ: CustomQuery,
37    {
38        let msg = QueryMsg::GetCount {};
39        let query = WasmQuery::Smart {
40            contract_addr: self.addr().into(),
41            msg: to_json_binary(&msg)?,
42        }
43        .into();
44        let res: GetCountResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
45        Ok(res)
46    }
47}