use crate::{
ibc::CallbackInfo,
ibc_host::HostAction,
manager::ModuleInstallConfig,
objects::{account::AccountId, chain_name::ChainName, AssetEntry},
};
use cosmwasm_schema::QueryResponses;
use cosmwasm_std::{Addr, Coin, Empty, QueryRequest};
use polytone::callbacks::CallbackMessage;
use self::state::IbcInfrastructure;
pub mod state {
use crate::objects::{
account::{AccountSequence, AccountTrace},
ans_host::AnsHost,
chain_name::ChainName,
version_control::VersionControlContract,
};
use cosmwasm_std::Addr;
use cw_storage_plus::{Item, Map};
#[cosmwasm_schema::cw_serde]
pub struct Config {
pub version_control: VersionControlContract,
pub ans_host: AnsHost,
}
#[cosmwasm_schema::cw_serde]
pub struct IbcInfrastructure {
pub polytone_note: Addr,
pub remote_abstract_host: String,
pub remote_proxy: Option<String>,
}
pub const IBC_INFRA: Map<&ChainName, IbcInfrastructure> = Map::new("ibci");
pub const REVERSE_POLYTONE_NOTE: Map<&Addr, ChainName> = Map::new("revpn");
pub const CONFIG: Item<Config> = Item::new("config");
pub const ACCOUNTS: Map<(&AccountTrace, AccountSequence, &ChainName), String> =
Map::new("accs");
pub const ACKS: Item<Vec<String>> = Item::new("tmpc");
}
#[cosmwasm_schema::cw_serde]
pub struct InstantiateMsg {
pub ans_host_address: String,
pub version_control_address: String,
}
#[cosmwasm_schema::cw_serde]
pub struct MigrateMsg {}
#[cosmwasm_schema::cw_serde]
#[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))]
pub enum ExecuteMsg {
UpdateOwnership(cw_ownable::Action),
RegisterInfrastructure {
chain: String,
note: String,
host: String,
},
UpdateConfig {
ans_host: Option<String>,
version_control: Option<String>,
},
SendFunds {
host_chain: String,
funds: Vec<Coin>,
},
Register {
host_chain: String,
base_asset: Option<AssetEntry>,
namespace: Option<String>,
install_modules: Vec<ModuleInstallConfig>,
},
RemoteAction {
host_chain: String,
action: HostAction,
callback_info: Option<CallbackInfo>,
},
RemoteQueries {
host_chain: String,
queries: Vec<QueryRequest<Empty>>,
callback_info: CallbackInfo,
},
RemoveHost {
host_chain: String,
},
Callback(CallbackMessage),
}
#[cosmwasm_schema::cw_serde]
pub enum IbcClientCallback {
UserRemoteAction(CallbackInfo),
CreateAccount { account_id: AccountId },
WhoAmI {},
}
#[cosmwasm_schema::cw_serde]
#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(cw_ownable::Ownership<Addr> )]
Ownership {},
#[returns(HostResponse)]
Config {},
#[returns(HostResponse)]
Host { chain_name: String },
#[returns(ListAccountsResponse)]
ListAccounts {
start: Option<(AccountId, String)>,
limit: Option<u32>,
},
#[returns(AccountResponse)]
Account {
chain: String,
account_id: AccountId,
},
#[returns(ListRemoteHostsResponse)]
ListRemoteHosts {},
#[returns(ListRemoteProxiesResponse)]
ListRemoteProxies {},
#[returns(ListRemoteProxiesResponse)]
ListRemoteProxiesByAccountId { account_id: AccountId },
#[returns(ListIbcInfrastructureResponse)]
ListIbcInfrastructures {},
}
#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {
pub ans_host: String,
pub version_control_address: String,
}
#[cosmwasm_schema::cw_serde]
pub struct ListAccountsResponse {
pub accounts: Vec<(AccountId, ChainName, String)>,
}
#[cosmwasm_schema::cw_serde]
pub struct ListRemoteHostsResponse {
pub hosts: Vec<(ChainName, String)>,
}
#[cosmwasm_schema::cw_serde]
pub struct ListRemoteProxiesResponse {
pub proxies: Vec<(ChainName, Option<String>)>,
}
#[cosmwasm_schema::cw_serde]
pub struct ListIbcInfrastructureResponse {
pub counterparts: Vec<(ChainName, IbcInfrastructure)>,
}
#[cosmwasm_schema::cw_serde]
pub struct HostResponse {
pub remote_host: String,
pub remote_polytone_proxy: Option<String>,
}
#[cosmwasm_schema::cw_serde]
pub struct AccountResponse {
pub remote_proxy_addr: String,
}
#[cosmwasm_schema::cw_serde]
pub struct RemoteProxyResponse {
pub channel_id: String,
pub proxy_address: String,
}
#[cfg(test)]
mod tests {
use crate::ibc::IbcResponseMsg;
use cosmwasm_std::{to_json_binary, CosmosMsg, Empty};
use polytone::callbacks::Callback;
use speculoos::prelude::*;
#[test]
fn test_response_msg_to_callback_msg() {
let receiver = "receiver".to_string();
let callback_id = "15".to_string();
let callback_msg = to_json_binary("15").unwrap();
let result = Callback::FatalError("ibc execution error".to_string());
let response_msg = IbcResponseMsg {
id: callback_id,
msg: Some(callback_msg),
result,
};
let actual: CosmosMsg<Empty> = response_msg.into_cosmos_msg(receiver.clone()).unwrap();
assert_that!(actual).matches(|e| {
matches!(
e,
CosmosMsg::Wasm(cosmwasm_std::WasmMsg::Execute {
contract_addr: _receiver,
msg: _,
funds: _
})
)
});
}
}