use crate::Abstract;
use abstract_core::manager::ModuleInstallConfig;
use abstract_core::objects::module::{ModuleInfo, ModuleVersion};
use cosmwasm_std::to_json_binary;
use cw_orch::deploy::Deploy;
use cw_orch::prelude::CwOrchError::StdErr;
use cw_orch::prelude::*;
use semver::Version;
use serde::Serialize;
pub trait RegisteredModule {
type InitMsg: Serialize;
fn module_id<'a>() -> &'a str;
fn module_version<'a>() -> &'a str;
}
pub trait DependencyCreation {
type DependenciesConfig;
#[allow(unused_variables)]
fn dependency_install_configs(
configuration: Self::DependenciesConfig,
) -> Result<Vec<ModuleInstallConfig>, crate::AbstractInterfaceError> {
Ok(vec![])
}
}
pub trait InstallConfig: RegisteredModule {
fn module_info() -> Result<ModuleInfo, crate::AbstractInterfaceError> {
ModuleInfo::from_id(Self::module_id(), Self::module_version().into()).map_err(Into::into)
}
fn install_config(
init_msg: &Self::InitMsg,
) -> Result<ModuleInstallConfig, crate::AbstractInterfaceError> {
Ok(ModuleInstallConfig::new(
Self::module_info()?,
Some(to_json_binary(init_msg)?),
))
}
}
impl<T> InstallConfig for T where T: RegisteredModule {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeployStrategy {
Error,
Try,
Force,
}
pub trait AdapterDeployer<Chain: CwEnv, CustomInitMsg: Serialize>: ContractInstance<Chain>
+ CwOrchInstantiate<Chain, InstantiateMsg = abstract_core::adapter::InstantiateMsg<CustomInitMsg>>
+ Uploadable
+ Sized
{
fn deploy(
&self,
version: Version,
custom_init_msg: CustomInitMsg,
strategy: DeployStrategy,
) -> Result<(), crate::AbstractInterfaceError> {
let abstr = Abstract::load_from(self.get_chain().to_owned())?;
let version_check = || {
abstr
.version_control
.get_adapter_addr(&self.id(), ModuleVersion::from(version.to_string()))
};
match strategy {
DeployStrategy::Error => {
if version_check().is_ok() {
return Err(StdErr(format!(
"Adapter {} already exists with version {}",
self.id(),
version
))
.into());
}
}
DeployStrategy::Try => {
if version_check().is_ok() {
return Ok(());
}
}
DeployStrategy::Force => {}
}
if self.upload_if_needed()?.is_some() {
let init_msg = abstract_core::adapter::InstantiateMsg {
module: custom_init_msg,
base: abstract_core::adapter::BaseInstantiateMsg {
ans_host_address: abstr.ans_host.address()?.into(),
version_control_address: abstr.version_control.address()?.into(),
},
};
self.instantiate(&init_msg, None, None)?;
abstr
.version_control
.register_adapters(vec![(self.as_instance(), version.to_string())])?;
}
Ok(())
}
}
pub trait AppDeployer<Chain: CwEnv>: Sized + Uploadable + ContractInstance<Chain> {
fn deploy(
&self,
version: Version,
strategy: DeployStrategy,
) -> Result<(), crate::AbstractInterfaceError> {
let abstr = Abstract::<Chain>::load_from(self.get_chain().to_owned())?;
let version_check = || {
abstr
.version_control
.get_app_code(&self.id(), ModuleVersion::from(version.to_string()))
};
match strategy {
DeployStrategy::Error => {
if version_check().is_ok() {
return Err(StdErr(format!(
"App {} already exists with version {}",
self.id(),
version
))
.into());
}
}
DeployStrategy::Try => {
if version_check().is_ok() {
return Ok(());
}
}
_ => {}
}
if self.upload_if_needed()?.is_some() {
abstr
.version_control
.register_apps(vec![(self.as_instance(), version.to_string())])?;
}
Ok(())
}
}