Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/console/src/factory/mission_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::types::ledger::Fee;
use candid::{Nat, Principal};
use junobuild_shared::constants::shared::CREATE_MISSION_CONTROL_CYCLES;
use junobuild_shared::ic::api::id;
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
use junobuild_shared::mgmt::ic::create_canister_install_code;
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
use junobuild_shared::mgmt::types::cmc::SubnetId;
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
use junobuild_shared::types::interface::CreateMissionControlArgs;
Expand Down Expand Up @@ -69,15 +69,15 @@ async fn create_mission_control_wasm(
};

let mission_control_id = if let Some(subnet_id) = subnet_id {
create_canister_install_code_with_cmc(
create_and_install_canister_with_cmc(
&create_settings_arg,
&wasm_arg,
CREATE_MISSION_CONTROL_CYCLES,
&subnet_id,
)
.await
} else {
create_canister_install_code(
create_and_install_canister_with_ic_mgmt(
&create_settings_arg,
&wasm_arg,
CREATE_MISSION_CONTROL_CYCLES,
Expand Down
13 changes: 9 additions & 4 deletions src/console/src/factory/orbiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::types::state::{Segment, SegmentKey, StorableSegmentKind};
use candid::{Nat, Principal};
use junobuild_shared::constants::shared::CREATE_ORBITER_CYCLES;
use junobuild_shared::ic::api::id;
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
use junobuild_shared::mgmt::ic::create_canister_install_code;
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
use junobuild_shared::mgmt::types::cmc::SubnetId;
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
use junobuild_shared::types::interface::CreateOrbiterArgs;
Expand Down Expand Up @@ -57,15 +57,20 @@ async fn create_orbiter_wasm(
};

let result = if let Some(subnet_id) = subnet_id {
create_canister_install_code_with_cmc(
create_and_install_canister_with_cmc(
&create_settings_arg,
&wasm_arg,
CREATE_ORBITER_CYCLES,
&subnet_id,
)
.await
} else {
create_canister_install_code(&create_settings_arg, &wasm_arg, CREATE_ORBITER_CYCLES).await
create_and_install_canister_with_ic_mgmt(
&create_settings_arg,
&wasm_arg,
CREATE_ORBITER_CYCLES,
)
.await
};

match result {
Expand Down
13 changes: 9 additions & 4 deletions src/console/src/factory/satellite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::types::state::{Segment, SegmentKey, StorableSegmentKind};
use candid::{Nat, Principal};
use junobuild_shared::constants::shared::CREATE_SATELLITE_CYCLES;
use junobuild_shared::ic::api::id;
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
use junobuild_shared::mgmt::ic::create_canister_install_code;
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
use junobuild_shared::mgmt::types::cmc::SubnetId;
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
use junobuild_shared::types::interface::{CreateSatelliteArgs, InitStorageArgs};
Expand Down Expand Up @@ -63,15 +63,20 @@ async fn create_satellite_wasm(
};

let result = if let Some(subnet_id) = subnet_id {
create_canister_install_code_with_cmc(
create_and_install_canister_with_cmc(
&create_settings_arg,
&wasm_arg,
CREATE_SATELLITE_CYCLES,
&subnet_id,
)
.await
} else {
create_canister_install_code(&create_settings_arg, &wasm_arg, CREATE_SATELLITE_CYCLES).await
create_and_install_canister_with_ic_mgmt(
&create_settings_arg,
&wasm_arg,
CREATE_SATELLITE_CYCLES,
)
.await
};

match result {
Expand Down
56 changes: 37 additions & 19 deletions src/libs/shared/src/mgmt/cmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,39 @@ fn convert_principal_to_sub_account(principal_id: &[u8]) -> Subaccount {
/// # Returns
/// - `Ok(Principal)`: On success, returns the `Principal` ID of the newly created canister.
/// - `Err(String)`: On failure, returns an error message.
pub async fn create_canister_install_code_with_cmc(
pub async fn create_and_install_canister_with_cmc(
create_settings_arg: &CreateCanisterInitSettingsArg,
wasm_arg: &WasmArg,
cycles: u128,
subnet_id: &SubnetId,
) -> Result<Principal, String> {
let canister_id = create_canister_with_cmc(create_settings_arg, cycles, subnet_id).await?;

install_code(canister_id, wasm_arg, CanisterInstallMode::Install)
.await
.map_err(|_| JUNO_ERROR_CMC_INSTALL_CODE_FAILED.to_string())?;

Ok(canister_id)
}

/// Creates a new canister on a specific subnet using the Cycles Minting Canister (CMC).
///
/// # Arguments
/// - `create_settings_arg`: Initial settings for the canister (controllers, compute allocation, etc.)
/// - `cycles`: The number of cycles to deposit into the new canister
/// - `subnet_id`: The ID of the subnet where the canister should be created
///
/// # Returns
/// - `Ok(Principal)`: On success, returns the Principal ID of the newly created canister
/// - `Err(String)`: On failure, returns an error message describing what went wrong
///
/// # Errors
/// - CMC call failures (network issues, invalid arguments, etc.)
/// - CMC canister creation failures (insufficient cycles, subnet unavailable, etc.)
pub async fn create_canister_with_cmc(
create_settings_arg: &CreateCanisterInitSettingsArg,
cycles: u128,
subnet_id: &SubnetId,
) -> Result<Principal, String> {
let cmc = Principal::from_text(CMC).unwrap();

Expand All @@ -111,22 +139,12 @@ pub async fn create_canister_install_code_with_cmc(
.await
.decode_candid::<CreateCanisterResult>();

match result {
Err(error) => Err(format!(
"{} ({})",
JUNO_ERROR_CMC_CALL_CREATE_CANISTER_FAILED, &error
)),
Ok(result) => match result {
Err(err) => Err(format!("{JUNO_ERROR_CMC_CREATE_CANISTER_FAILED} ({err})")),
Ok(canister_id) => {
let install =
install_code(canister_id, wasm_arg, CanisterInstallMode::Install).await;

match install {
Err(_) => Err(JUNO_ERROR_CMC_INSTALL_CODE_FAILED.to_string()),
Ok(_) => Ok(canister_id),
}
}
},
}
result
.map_err(|error| {
format!(
"{} ({})",
JUNO_ERROR_CMC_CALL_CREATE_CANISTER_FAILED, &error
)
})?
.map_err(|err| format!("{JUNO_ERROR_CMC_CREATE_CANISTER_FAILED} ({err})"))
}
48 changes: 33 additions & 15 deletions src/libs/shared/src/mgmt/ic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,54 @@ use ic_cdk::management_canister::{
/// # Returns
/// - `Ok(Principal)`: On success, returns the `Principal` ID of the newly created canister.
/// - `Err(String)`: On failure, returns an error message.
pub async fn create_canister_install_code(
pub async fn create_and_install_canister_with_ic_mgmt(
create_settings_arg: &CreateCanisterInitSettingsArg,
wasm_arg: &WasmArg,
cycles: u128,
) -> Result<Principal, String> {
let record = create_canister_with_extra_cycles(
let canister_id = create_canister_with_ic_mgmt(create_settings_arg, cycles).await?;

install_code(canister_id, wasm_arg, CanisterInstallMode::Install)
.await
.map_err(|_| JUNO_ERROR_CANISTER_INSTALL_CODE_FAILED.to_string())?;

Ok(canister_id)
}

/// Creates a new canister using the IC management canister.
///
/// # Arguments
/// - `create_settings_arg`: Initial settings for the canister (controllers, compute allocation, etc.)
/// - `cycles`: The number of cycles to deposit into the new canister
///
/// # Returns
/// - `Ok(Principal)`: On success, returns the Principal ID of the newly created canister
/// - `Err(String)`: On failure, returns an error message describing what went wrong
///
/// # Errors
/// - Management canister call failures (network issues, invalid arguments, etc.)
/// - Canister creation failures (insufficient cycles, etc.)
pub async fn create_canister_with_ic_mgmt(
create_settings_arg: &CreateCanisterInitSettingsArg,
cycles: u128,
) -> Result<Principal, String> {
let create_result = create_canister_with_extra_cycles(
&CreateCanisterArgs {
settings: create_canister_settings(create_settings_arg),
},
create_canister_cycles(cycles),
)
.await;

match record {
Err(err) => Err(format!(
let result = create_result.map_err(|err| {
format!(
"{} ({})",
JUNO_ERROR_CANISTER_CREATE_FAILED,
&err.to_string()
)),
Ok(record) => {
let canister_id = record.canister_id;
)
})?;

let install = install_code(canister_id, wasm_arg, CanisterInstallMode::Install).await;

match install {
Err(_) => Err(JUNO_ERROR_CANISTER_INSTALL_CODE_FAILED.to_string()),
Ok(_) => Ok(canister_id),
}
}
}
Ok(result.canister_id)
}

/// Asynchronously installs code on a specified canister.
Expand Down
Loading