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
use boot_core::{
interface::{BootExecute, BootQuery, ContractInstance},
prelude::boot_contract,
BootEnvironment, BootError, Contract, Daemon, IndexResponse, TxResponse,
};
use cosmwasm_std::Addr;
use semver::Version;
use abstract_os::{
objects::{
module::{ModuleInfo, ModuleVersion},
module_reference::ModuleReference,
},
version_control::*,
VERSION_CONTROL,
};
use crate::deployment::{self, OS};
pub use abstract_os::version_control::{ExecuteMsgFns as VCExecFns, QueryMsgFns as VCQueryFns};
#[boot_contract(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
pub struct VersionControl<Chain>;
impl<Chain: BootEnvironment> VersionControl<Chain>
where
TxResponse<Chain>: IndexResponse,
{
pub fn new(name: &str, chain: Chain) -> Self {
let mut contract = Contract::new(name, chain);
contract = contract.with_wasm_path("version_control");
Self(contract)
}
pub fn load(chain: Chain, address: &Addr) -> Self {
Self(Contract::new(VERSION_CONTROL, chain).with_address(Some(address)))
}
pub fn register_core(&self, os: &OS<Chain>, version: &str) -> Result<(), BootError> {
let manager = os.manager.as_instance();
self.execute(
&ExecuteMsg::AddModules {
modules: vec![(
ModuleInfo::from_id(&manager.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::Core(manager.code_id()?),
)],
},
None,
)?;
log::info!("Module {} registered", manager.id);
let proxy = os.proxy.as_instance();
self.execute(
&ExecuteMsg::AddModules {
modules: vec![(
ModuleInfo::from_id(&proxy.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::Core(proxy.code_id()?),
)],
},
None,
)?;
log::info!("Module {} registered", proxy.id);
Ok(())
}
pub fn register_apps(
&self,
modules: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), BootError> {
let apps_to_register: Result<Vec<(ModuleInfo, ModuleReference)>, BootError> = modules
.iter()
.map(|app| {
Ok((
ModuleInfo::from_id(&app.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::App(app.code_id()?),
))
})
.collect();
self.execute(
&ExecuteMsg::AddModules {
modules: apps_to_register?,
},
None,
)?;
Ok(())
}
pub fn register_apis(
&self,
apis: Vec<&Contract<Chain>>,
version: &Version,
) -> Result<(), BootError> {
let apis_to_register: Result<Vec<(ModuleInfo, ModuleReference)>, BootError> = apis
.iter()
.map(|ex| {
Ok((
ModuleInfo::from_id(&ex.id, ModuleVersion::Version(version.to_string()))?,
ModuleReference::Api(ex.address()?),
))
})
.collect();
self.execute(
&ExecuteMsg::AddModules {
modules: apis_to_register?,
},
None,
)?;
Ok(())
}
pub fn register_native(
&self,
deployment: &deployment::Deployment<Chain>,
) -> Result<(), BootError> {
let modules: Result<Vec<(ModuleInfo, ModuleReference)>, BootError> = deployment
.contracts()
.iter()
.map(|contr| {
Ok((
ModuleInfo::from_id(
&contr.id,
ModuleVersion::Version(deployment.version.to_string()),
)?,
ModuleReference::Native(contr.address()?),
))
})
.collect();
self.execute(&ExecuteMsg::AddModules { modules: modules? }, None)?;
Ok(())
}
pub fn get_os_core(&self, os_id: u32) -> Result<Core, BootError> {
let resp: OsCoreResponse = self.query(&QueryMsg::OsCore { os_id })?;
Ok(resp.os_core)
}
/// Retrieves an API's address from version control given the module **id** and **version**.
pub fn get_api_addr(&self, id: &str, version: ModuleVersion) -> Result<Addr, BootError> {
let resp: ModuleResponse = self.query(&QueryMsg::Module {
module: ModuleInfo::from_id(id, version)?,
})?;
Ok(resp.module.reference.unwrap_addr()?)
}
}
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, BootError> = 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_apis(&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, BootError> =
// 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(())
// }
}