1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
use crate::AbstractAccount;
pub use abstract_core::version_control::{ExecuteMsgFns as VCExecFns, QueryMsgFns as VCQueryFns};
use abstract_core::{
objects::{
module::{Module, ModuleInfo, ModuleVersion},
module_reference::ModuleReference,
AccountId,
},
version_control::*,
VERSION_CONTROL,
};
use cosmwasm_std::Addr;
use cw_orch::contract::Contract;
use cw_orch::interface;
#[cfg(feature = "daemon")]
use cw_orch::prelude::Daemon;
use cw_orch::prelude::*;
use semver::Version;
#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
pub struct VersionControl<Chain>;
impl<Chain: CwEnv> Uploadable for VersionControl<Chain> {
#[cfg(feature = "integration")]
fn wrapper(&self) -> <Mock as ::cw_orch::environment::TxHandler>::ContractSource {
Box::new(
ContractWrapper::new_with_empty(
::version_control::contract::execute,
::version_control::contract::instantiate,
::version_control::contract::query,
)
.with_migrate(::version_control::contract::migrate),
)
}
fn wasm(&self) -> WasmPath {
artifacts_dir_from_workspace!()
.find_wasm_path("version_control")
.unwrap()
}
}
impl<Chain: CwEnv> VersionControl<Chain>
where
TxResponse<Chain>: IndexResponse,
{
pub fn new(name: &str, chain: Chain) -> Self {
Self(cw_orch::contract::Contract::new(name, chain))
}
pub fn load(chain: Chain, address: &Addr) -> Self {
Self(cw_orch::contract::Contract::new(VERSION_CONTROL, chain).with_address(Some(address)))
}
/// Query a single module
pub fn module(&self, info: ModuleInfo) -> Result<Module, crate::AbstractInterfaceError> {
let ModulesResponse { mut modules } = self.modules(vec![info])?;
Ok(modules.swap_remove(0))
}
pub fn register_base(
&self,
account: &AbstractAccount<Chain>,
version: &str,
) -> Result<(), crate::AbstractInterfaceError> {
let manager = account.manager.as_instance();
let manager_module = (
ModuleInfo::from_id(&manager.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::AccountBase(manager.code_id()?),
);
self.propose_modules(vec![manager_module])?;
log::info!("Module {} registered", manager.id);
let proxy = account.proxy.as_instance();
let proxy_module = (
ModuleInfo::from_id(&proxy.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::AccountBase(proxy.code_id()?),
);
self.propose_modules(vec![proxy_module])?;
log::info!("Module {} registered", proxy.id);
Ok(())
}
/// Register account modules
pub fn register_account_mods(
&self,
apps: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), crate::AbstractInterfaceError> {
let to_register = self.contracts_into_module_entries(apps, version, |c| {
ModuleReference::AccountBase(c.code_id().unwrap())
})?;
self.propose_modules(to_register)?;
Ok(())
}
/// Register native modules
pub fn register_natives(
&self,
natives: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), crate::AbstractInterfaceError> {
let to_register = self.contracts_into_module_entries(natives, version, |c| {
ModuleReference::Native(c.address().unwrap())
})?;
self.propose_modules(to_register)?;
Ok(())
}
pub fn register_apps(
&self,
apps: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), crate::AbstractInterfaceError> {
let to_register = self.contracts_into_module_entries(apps, version, |c| {
ModuleReference::App(c.code_id().unwrap())
})?;
self.propose_modules(to_register)?;
Ok(())
}
pub fn register_adapters(
&self,
adapters: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), crate::AbstractInterfaceError> {
let to_register = self.contracts_into_module_entries(adapters, version, |c| {
ModuleReference::Adapter(c.address().unwrap())
})?;
self.propose_modules(to_register)?;
Ok(())
}
pub fn register_standalones(
&self,
standalones: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), crate::AbstractInterfaceError> {
let to_register = self.contracts_into_module_entries(standalones, version, |c| {
ModuleReference::Standalone(c.code_id().unwrap())
})?;
self.propose_modules(to_register)?;
Ok(())
}
fn contracts_into_module_entries<RefFn>(
&self,
modules: Vec<&Contract<Chain>>,
version: &Version,
ref_fn: RefFn,
) -> Result<Vec<(ModuleInfo, ModuleReference)>, crate::AbstractInterfaceError>
where
RefFn: Fn(&&Contract<Chain>) -> ModuleReference,
{
let modules_to_register: Result<
Vec<(ModuleInfo, ModuleReference)>,
crate::AbstractInterfaceError,
> = modules
.iter()
.map(|contract| {
Ok((
ModuleInfo::from_id(&contract.id, ModuleVersion::Version(version.to_string()))?,
ref_fn(contract),
))
})
.collect();
modules_to_register
}
pub fn get_account(
&self,
account_id: AccountId,
) -> Result<AccountBase, crate::AbstractInterfaceError> {
let resp: AccountBaseResponse = self.query(&QueryMsg::AccountBase { account_id })?;
Ok(resp.account_base)
}
/// Retrieves an Adapter's address from version control given the module **id** and **version**.
pub fn get_adapter_addr(
&self,
id: &str,
version: ModuleVersion,
) -> Result<Addr, crate::AbstractInterfaceError> {
let module: Module = self.module(ModuleInfo::from_id(id, version)?)?;
Ok(module.reference.unwrap_adapter()?)
}
/// Retrieves an APP's code id from version control given the module **id** and **version**.
pub fn get_app_code(
&self,
id: &str,
version: ModuleVersion,
) -> Result<u64, crate::AbstractInterfaceError> {
let module: Module = self.module(ModuleInfo::from_id(id, version)?)?;
Ok(module.reference.unwrap_app()?)
}
}
#[cfg(feature = "daemon")]
impl VersionControl<Daemon> {
// pub fn update_code_ids(&self, new_version: Version) -> anyhow::Result<()> {
// let code_ids = self.get_chain().state().get_all_code_ids()?;
// for (contract_id, code_id) in code_ids {
// if NATIVE_CONTRACTS.contains(&contract_id.as_str()) {
// continue;
// }
// // Get latest code id
// let resp: Result<QueryCodeIdResponse, crate::AbstractBootError> = self.query(&QueryMsg::CodeId {
// module: ModuleInfo {
// name: contract_id.clone(),
// version: None,
// },
// });
// log::debug!("{:?}", resp);
// if new_version.pre.is_empty() {
// match resp {
// Ok(resp) => {
// let registered_code_id = resp.code_id.u64();
// // If equal, continue
// if registered_code_id == code_id {
// continue;
// } else {
// let latest_version = resp.info.version;
// version = latest_version.parse().unwrap();
// // bump patch
// version.patch += 1;
// }
// }
// Err(_) => (),
// };
// }
// self.execute(
// &ExecuteMsg::AddCodeId {
// module: contract_id.to_string(),
// version: version.to_string(),
// code_id,
// },
// None,
// )?;
// }
// Ok(())
// }
// pub fn update_adapters(&self) -> anyhow::Result<()> {
// for contract_name in chain_state.keys() {
// if !API_CONTRACTS.contains(&contract_name.as_str()) {
// continue;
// }
// // Get local addr
// let address: String = chain_state[contract_name].as_str().unwrap().into();
// // Get latest addr
// let resp: Result<QueryApiAddressResponse, crate::AbstractBootError> =
// self.query(&QueryMsg::ApiAddress {
// module: ModuleInfo {
// name: contract_name.clone(),
// version: None,
// },
// });
// log::debug!("{:?}", resp);
// let mut version = self.deployment_version.clone();
// match resp {
// Ok(resp) => {
// let registered_addr = resp.address.to_string();
// // If equal, continue
// if registered_addr == address {
// continue;
// } else {
// let latest_version = resp.info.version;
// version = latest_version.parse().unwrap();
// // bump patch
// version.patch += 1;
// }
// }
// Err(_) => (),
// };
// self.execute(
// &ExecuteMsg::AddApi {
// module: contract_name.to_string(),
// version: version.to_string(),
// address,
// },
// None,
// )?;
// }
// Ok(())
// }
}