cw_ica_controller/
helpers.rs1use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use cosmwasm_std::{
8 instantiate2_address, to_json_binary, Addr, Api, CosmosMsg, Env, QuerierWrapper, StdError,
9 StdResult, WasmMsg,
10};
11
12use crate::types::{msg, state};
13
14pub use cw_ica_controller_derive::ica_callback_execute; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
19pub struct CwIcaControllerContract(pub Addr);
20
21#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
24pub struct CwIcaControllerCode(pub u64);
25
26pub struct CwIcaControllerContractQuerier<'a> {
31 querier: &'a QuerierWrapper<'a>,
32 addr: String,
33}
34
35impl CwIcaControllerContract {
36 #[must_use]
38 pub const fn new(addr: Addr) -> Self {
39 Self(addr)
40 }
41
42 #[must_use]
44 pub fn addr(&self) -> Addr {
45 self.0.clone()
46 }
47
48 pub fn execute(&self, msg: impl Into<msg::ExecuteMsg>) -> StdResult<CosmosMsg> {
54 let msg = to_json_binary(&msg.into())?;
55 Ok(WasmMsg::Execute {
56 contract_addr: self.addr().into(),
57 msg,
58 funds: vec![],
59 }
60 .into())
61 }
62
63 #[must_use]
65 pub fn query<'a>(&self, querier: &'a QuerierWrapper) -> CwIcaControllerContractQuerier<'a> {
66 CwIcaControllerContractQuerier::new(querier, self.addr().into_string())
67 }
68
69 pub fn update_admin(&self, admin: impl Into<String>) -> CosmosMsg {
71 WasmMsg::UpdateAdmin {
72 contract_addr: self.addr().into(),
73 admin: admin.into(),
74 }
75 .into()
76 }
77
78 #[must_use]
80 pub fn clear_admin(&self) -> CosmosMsg {
81 WasmMsg::ClearAdmin {
82 contract_addr: self.addr().into(),
83 }
84 .into()
85 }
86
87 pub fn migrate(
93 &self,
94 msg: impl Into<msg::MigrateMsg>,
95 new_code_id: u64,
96 ) -> StdResult<CosmosMsg> {
97 let msg = to_json_binary(&msg.into())?;
98 Ok(WasmMsg::Migrate {
99 contract_addr: self.addr().into(),
100 new_code_id,
101 msg,
102 }
103 .into())
104 }
105}
106
107impl CwIcaControllerCode {
108 #[must_use]
110 pub const fn new(code_id: u64) -> Self {
111 Self(code_id)
112 }
113
114 #[must_use]
116 pub const fn code_id(&self) -> u64 {
117 self.0
118 }
119
120 pub fn instantiate(
126 &self,
127 msg: impl Into<msg::InstantiateMsg>,
128 label: impl Into<String>,
129 admin: Option<impl Into<String>>,
130 ) -> StdResult<CosmosMsg> {
131 let msg = to_json_binary(&msg.into())?;
132 Ok(WasmMsg::Instantiate {
133 code_id: self.code_id(),
134 msg,
135 funds: vec![],
136 label: label.into(),
137 admin: admin.map(Into::into),
138 }
139 .into())
140 }
141
142 #[allow(clippy::too_many_arguments)]
153 pub fn instantiate2(
154 &self,
155 api: &dyn Api,
156 querier: &QuerierWrapper,
157 env: &Env,
158 msg: impl Into<msg::InstantiateMsg>,
159 label: impl Into<String>,
160 admin: Option<impl Into<String>>,
161 salt: impl Into<String>,
162 ) -> StdResult<(CosmosMsg, Addr)> {
163 let salt = salt.into();
164 let code_info = querier.query_wasm_code_info(self.code_id())?;
165 let creator_cannonical = api.addr_canonicalize(env.contract.address.as_str())?;
166
167 let contract_addr = api.addr_humanize(
168 &instantiate2_address(
169 code_info.checksum.as_slice(),
170 &creator_cannonical,
171 salt.as_bytes(),
172 )
173 .map_err(|e| StdError::generic_err(e.to_string()))?,
174 )?;
175
176 let instantiate_msg = WasmMsg::Instantiate2 {
177 code_id: self.code_id(),
178 msg: to_json_binary(&msg.into())?,
179 funds: vec![],
180 label: label.into(),
181 admin: admin.map(Into::into),
182 salt: salt.as_bytes().into(),
183 };
184
185 Ok((instantiate_msg.into(), contract_addr))
186 }
187}
188
189impl<'a> CwIcaControllerContractQuerier<'a> {
190 #[must_use]
192 pub const fn new(querier: &'a QuerierWrapper<'a>, addr: String) -> Self {
193 Self { querier, addr }
194 }
195
196 pub fn get_channel(&self) -> StdResult<state::ChannelState> {
202 self.querier
203 .query_wasm_smart(&self.addr, &msg::QueryMsg::GetChannel {})
204 }
205
206 pub fn get_contract_state(&self) -> StdResult<state::ContractState> {
212 self.querier
213 .query_wasm_smart(&self.addr, &msg::QueryMsg::GetContractState {})
214 }
215
216 pub fn ownership(&self) -> StdResult<cw_ownable::Ownership<String>> {
221 self.querier
222 .query_wasm_smart(&self.addr, &msg::QueryMsg::Ownership {})
223 }
224}