1use cosmwasm_schema::QueryResponses;
2use cosmwasm_std::{Addr, Coin, Empty, QueryRequest};
3use polytone::callbacks::CallbackMessage;
4
5use self::state::IbcInfrastructure;
6use crate::{
7 ibc::CallbackInfo,
8 ibc_host::HostAction,
9 manager::ModuleInstallConfig,
10 objects::{account::AccountId, chain_name::ChainName, AssetEntry},
11};
12
13pub mod state {
14
15 use cosmwasm_std::Addr;
16 use cw_storage_plus::{Item, Map};
17
18 use crate::objects::{
19 account::{AccountSequence, AccountTrace},
20 ans_host::AnsHost,
21 chain_name::ChainName,
22 version_control::VersionControlContract,
23 };
24
25 #[cosmwasm_schema::cw_serde]
26 pub struct Config {
27 pub version_control: VersionControlContract,
28 pub ans_host: AnsHost,
29 }
30
31 #[cosmwasm_schema::cw_serde]
33 pub struct IbcInfrastructure {
34 pub polytone_note: Addr,
36 pub remote_abstract_host: String,
38 pub remote_proxy: Option<String>,
40 }
41
42 pub const IBC_INFRA: Map<&ChainName, IbcInfrastructure> = Map::new("ibci");
45 pub const REVERSE_POLYTONE_NOTE: Map<&Addr, ChainName> = Map::new("revpn");
46
47 pub const CONFIG: Item<Config> = Item::new("config");
48 pub const ACCOUNTS: Map<(&AccountTrace, AccountSequence, &ChainName), String> =
52 Map::new("accs");
53
54 pub const ACKS: Item<Vec<String>> = Item::new("tmpc");
56}
57
58#[cosmwasm_schema::cw_serde]
60pub struct InstantiateMsg {
61 pub ans_host_address: String,
62 pub version_control_address: String,
63}
64
65#[cosmwasm_schema::cw_serde]
66pub struct MigrateMsg {}
67
68#[cosmwasm_schema::cw_serde]
69#[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))]
70pub enum ExecuteMsg {
71 UpdateOwnership(cw_ownable::Action),
73 RegisterInfrastructure {
76 chain: String,
78 note: String,
80 host: String,
82 },
83 UpdateConfig {
85 ans_host: Option<String>,
86 version_control: Option<String>,
87 },
88 SendFunds {
92 host_chain: String,
93 funds: Vec<Coin>,
94 },
95 Register {
98 host_chain: String,
99 base_asset: Option<AssetEntry>,
100 namespace: Option<String>,
101 install_modules: Vec<ModuleInstallConfig>,
102 },
103 RemoteAction {
104 host_chain: String,
107 action: HostAction,
109 callback_info: Option<CallbackInfo>,
111 },
112 RemoteQueries {
115 host_chain: String,
118 queries: Vec<QueryRequest<Empty>>,
120 callback_info: CallbackInfo,
122 },
123 RemoveHost {
124 host_chain: String,
125 },
126
127 Callback(CallbackMessage),
130}
131
132#[cosmwasm_schema::cw_serde]
134pub enum IbcClientCallback {
135 UserRemoteAction(CallbackInfo),
136 CreateAccount { account_id: AccountId },
137 WhoAmI {},
138}
139
140#[cosmwasm_schema::cw_serde]
141#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))]
142#[derive(QueryResponses)]
143pub enum QueryMsg {
144 #[returns(cw_ownable::Ownership<Addr> )]
147 Ownership {},
148
149 #[returns(ConfigResponse)]
152 Config {},
153
154 #[returns(HostResponse)]
157 Host { chain_name: String },
158
159 #[returns(ListAccountsResponse)]
162 ListAccounts {
163 start: Option<(AccountId, String)>,
164 limit: Option<u32>,
165 },
166
167 #[returns(AccountResponse)]
170 Account {
171 chain: String,
172 account_id: AccountId,
173 },
174 #[returns(ListRemoteHostsResponse)]
177 ListRemoteHosts {},
178
179 #[returns(ListRemoteProxiesResponse)]
182 ListRemoteProxies {},
183
184 #[returns(ListRemoteProxiesResponse)]
187 ListRemoteProxiesByAccountId { account_id: AccountId },
188
189 #[returns(ListIbcInfrastructureResponse)]
192 ListIbcInfrastructures {},
193}
194
195#[cosmwasm_schema::cw_serde]
196pub struct ConfigResponse {
197 pub ans_host: String,
198 pub version_control_address: String,
199}
200
201#[cosmwasm_schema::cw_serde]
202pub struct ListAccountsResponse {
203 pub accounts: Vec<(AccountId, ChainName, String)>,
204}
205
206#[cosmwasm_schema::cw_serde]
207pub struct ListRemoteHostsResponse {
208 pub hosts: Vec<(ChainName, String)>,
209}
210
211#[cosmwasm_schema::cw_serde]
212pub struct ListRemoteProxiesResponse {
213 pub proxies: Vec<(ChainName, Option<String>)>,
214}
215
216#[cosmwasm_schema::cw_serde]
217pub struct ListIbcInfrastructureResponse {
218 pub counterparts: Vec<(ChainName, IbcInfrastructure)>,
219}
220
221#[cosmwasm_schema::cw_serde]
222pub struct HostResponse {
223 pub remote_host: String,
224 pub remote_polytone_proxy: Option<String>,
225}
226
227#[cosmwasm_schema::cw_serde]
228pub struct AccountResponse {
229 pub remote_proxy_addr: String,
230}
231
232#[cosmwasm_schema::cw_serde]
233pub struct RemoteProxyResponse {
234 pub channel_id: String,
236 pub proxy_address: String,
238}
239
240#[cfg(test)]
241mod tests {
242 use cosmwasm_std::{to_json_binary, CosmosMsg, Empty};
243 use polytone::callbacks::Callback;
244 use speculoos::prelude::*;
245
246 use crate::ibc::IbcResponseMsg;
247
248 #[test]
251 fn test_response_msg_to_callback_msg() {
252 let receiver = "receiver".to_string();
253 let callback_id = "15".to_string();
254 let callback_msg = to_json_binary("15").unwrap();
255
256 let result = Callback::FatalError("ibc execution error".to_string());
257
258 let response_msg = IbcResponseMsg {
259 id: callback_id,
260 msg: Some(callback_msg),
261 result,
262 };
263
264 let actual: CosmosMsg<Empty> = response_msg.into_cosmos_msg(receiver.clone()).unwrap();
265
266 assert_that!(actual).matches(|e| {
267 matches!(
268 e,
269 CosmosMsg::Wasm(cosmwasm_std::WasmMsg::Execute {
270 contract_addr: _receiver,
271 msg: _,
273 funds: _
274 })
275 )
276 });
277 }
278}