Skip to content

Commit 055e1f5

Browse files
feat(shared)!: rename and split create canister and install code with cmc and ic mgmt (#2475)
1 parent 6b14392 commit 055e1f5

5 files changed

Lines changed: 92 additions & 46 deletions

File tree

src/console/src/factory/mission_control.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::types::ledger::Fee;
1111
use candid::{Nat, Principal};
1212
use junobuild_shared::constants::shared::CREATE_MISSION_CONTROL_CYCLES;
1313
use junobuild_shared::ic::api::id;
14-
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
15-
use junobuild_shared::mgmt::ic::create_canister_install_code;
14+
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
15+
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
1616
use junobuild_shared::mgmt::types::cmc::SubnetId;
1717
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
1818
use junobuild_shared::types::interface::CreateMissionControlArgs;
@@ -69,15 +69,15 @@ async fn create_mission_control_wasm(
6969
};
7070

7171
let mission_control_id = if let Some(subnet_id) = subnet_id {
72-
create_canister_install_code_with_cmc(
72+
create_and_install_canister_with_cmc(
7373
&create_settings_arg,
7474
&wasm_arg,
7575
CREATE_MISSION_CONTROL_CYCLES,
7676
&subnet_id,
7777
)
7878
.await
7979
} else {
80-
create_canister_install_code(
80+
create_and_install_canister_with_ic_mgmt(
8181
&create_settings_arg,
8282
&wasm_arg,
8383
CREATE_MISSION_CONTROL_CYCLES,

src/console/src/factory/orbiter.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::types::state::{Segment, SegmentKey, StorableSegmentKind};
1212
use candid::{Nat, Principal};
1313
use junobuild_shared::constants::shared::CREATE_ORBITER_CYCLES;
1414
use junobuild_shared::ic::api::id;
15-
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
16-
use junobuild_shared::mgmt::ic::create_canister_install_code;
15+
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
16+
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
1717
use junobuild_shared::mgmt::types::cmc::SubnetId;
1818
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
1919
use junobuild_shared::types::interface::CreateOrbiterArgs;
@@ -57,15 +57,20 @@ async fn create_orbiter_wasm(
5757
};
5858

5959
let result = if let Some(subnet_id) = subnet_id {
60-
create_canister_install_code_with_cmc(
60+
create_and_install_canister_with_cmc(
6161
&create_settings_arg,
6262
&wasm_arg,
6363
CREATE_ORBITER_CYCLES,
6464
&subnet_id,
6565
)
6666
.await
6767
} else {
68-
create_canister_install_code(&create_settings_arg, &wasm_arg, CREATE_ORBITER_CYCLES).await
68+
create_and_install_canister_with_ic_mgmt(
69+
&create_settings_arg,
70+
&wasm_arg,
71+
CREATE_ORBITER_CYCLES,
72+
)
73+
.await
6974
};
7075

7176
match result {

src/console/src/factory/satellite.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::types::state::{Segment, SegmentKey, StorableSegmentKind};
1212
use candid::{Nat, Principal};
1313
use junobuild_shared::constants::shared::CREATE_SATELLITE_CYCLES;
1414
use junobuild_shared::ic::api::id;
15-
use junobuild_shared::mgmt::cmc::create_canister_install_code_with_cmc;
16-
use junobuild_shared::mgmt::ic::create_canister_install_code;
15+
use junobuild_shared::mgmt::cmc::create_and_install_canister_with_cmc;
16+
use junobuild_shared::mgmt::ic::create_and_install_canister_with_ic_mgmt;
1717
use junobuild_shared::mgmt::types::cmc::SubnetId;
1818
use junobuild_shared::mgmt::types::ic::CreateCanisterInitSettingsArg;
1919
use junobuild_shared::types::interface::{CreateSatelliteArgs, InitStorageArgs};
@@ -63,15 +63,20 @@ async fn create_satellite_wasm(
6363
};
6464

6565
let result = if let Some(subnet_id) = subnet_id {
66-
create_canister_install_code_with_cmc(
66+
create_and_install_canister_with_cmc(
6767
&create_settings_arg,
6868
&wasm_arg,
6969
CREATE_SATELLITE_CYCLES,
7070
&subnet_id,
7171
)
7272
.await
7373
} else {
74-
create_canister_install_code(&create_settings_arg, &wasm_arg, CREATE_SATELLITE_CYCLES).await
74+
create_and_install_canister_with_ic_mgmt(
75+
&create_settings_arg,
76+
&wasm_arg,
77+
CREATE_SATELLITE_CYCLES,
78+
)
79+
.await
7580
};
7681

7782
match result {

src/libs/shared/src/mgmt/cmc.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,39 @@ fn convert_principal_to_sub_account(principal_id: &[u8]) -> Subaccount {
9191
/// # Returns
9292
/// - `Ok(Principal)`: On success, returns the `Principal` ID of the newly created canister.
9393
/// - `Err(String)`: On failure, returns an error message.
94-
pub async fn create_canister_install_code_with_cmc(
94+
pub async fn create_and_install_canister_with_cmc(
9595
create_settings_arg: &CreateCanisterInitSettingsArg,
9696
wasm_arg: &WasmArg,
9797
cycles: u128,
9898
subnet_id: &SubnetId,
99+
) -> Result<Principal, String> {
100+
let canister_id = create_canister_with_cmc(create_settings_arg, cycles, subnet_id).await?;
101+
102+
install_code(canister_id, wasm_arg, CanisterInstallMode::Install)
103+
.await
104+
.map_err(|_| JUNO_ERROR_CMC_INSTALL_CODE_FAILED.to_string())?;
105+
106+
Ok(canister_id)
107+
}
108+
109+
/// Creates a new canister on a specific subnet using the Cycles Minting Canister (CMC).
110+
///
111+
/// # Arguments
112+
/// - `create_settings_arg`: Initial settings for the canister (controllers, compute allocation, etc.)
113+
/// - `cycles`: The number of cycles to deposit into the new canister
114+
/// - `subnet_id`: The ID of the subnet where the canister should be created
115+
///
116+
/// # Returns
117+
/// - `Ok(Principal)`: On success, returns the Principal ID of the newly created canister
118+
/// - `Err(String)`: On failure, returns an error message describing what went wrong
119+
///
120+
/// # Errors
121+
/// - CMC call failures (network issues, invalid arguments, etc.)
122+
/// - CMC canister creation failures (insufficient cycles, subnet unavailable, etc.)
123+
pub async fn create_canister_with_cmc(
124+
create_settings_arg: &CreateCanisterInitSettingsArg,
125+
cycles: u128,
126+
subnet_id: &SubnetId,
99127
) -> Result<Principal, String> {
100128
let cmc = Principal::from_text(CMC).unwrap();
101129

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

114-
match result {
115-
Err(error) => Err(format!(
116-
"{} ({})",
117-
JUNO_ERROR_CMC_CALL_CREATE_CANISTER_FAILED, &error
118-
)),
119-
Ok(result) => match result {
120-
Err(err) => Err(format!("{JUNO_ERROR_CMC_CREATE_CANISTER_FAILED} ({err})")),
121-
Ok(canister_id) => {
122-
let install =
123-
install_code(canister_id, wasm_arg, CanisterInstallMode::Install).await;
124-
125-
match install {
126-
Err(_) => Err(JUNO_ERROR_CMC_INSTALL_CODE_FAILED.to_string()),
127-
Ok(_) => Ok(canister_id),
128-
}
129-
}
130-
},
131-
}
142+
result
143+
.map_err(|error| {
144+
format!(
145+
"{} ({})",
146+
JUNO_ERROR_CMC_CALL_CREATE_CANISTER_FAILED, &error
147+
)
148+
})?
149+
.map_err(|err| format!("{JUNO_ERROR_CMC_CREATE_CANISTER_FAILED} ({err})"))
132150
}

src/libs/shared/src/mgmt/ic.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,54 @@ use ic_cdk::management_canister::{
2727
/// # Returns
2828
/// - `Ok(Principal)`: On success, returns the `Principal` ID of the newly created canister.
2929
/// - `Err(String)`: On failure, returns an error message.
30-
pub async fn create_canister_install_code(
30+
pub async fn create_and_install_canister_with_ic_mgmt(
3131
create_settings_arg: &CreateCanisterInitSettingsArg,
3232
wasm_arg: &WasmArg,
3333
cycles: u128,
3434
) -> Result<Principal, String> {
35-
let record = create_canister_with_extra_cycles(
35+
let canister_id = create_canister_with_ic_mgmt(create_settings_arg, cycles).await?;
36+
37+
install_code(canister_id, wasm_arg, CanisterInstallMode::Install)
38+
.await
39+
.map_err(|_| JUNO_ERROR_CANISTER_INSTALL_CODE_FAILED.to_string())?;
40+
41+
Ok(canister_id)
42+
}
43+
44+
/// Creates a new canister using the IC management canister.
45+
///
46+
/// # Arguments
47+
/// - `create_settings_arg`: Initial settings for the canister (controllers, compute allocation, etc.)
48+
/// - `cycles`: The number of cycles to deposit into the new canister
49+
///
50+
/// # Returns
51+
/// - `Ok(Principal)`: On success, returns the Principal ID of the newly created canister
52+
/// - `Err(String)`: On failure, returns an error message describing what went wrong
53+
///
54+
/// # Errors
55+
/// - Management canister call failures (network issues, invalid arguments, etc.)
56+
/// - Canister creation failures (insufficient cycles, etc.)
57+
pub async fn create_canister_with_ic_mgmt(
58+
create_settings_arg: &CreateCanisterInitSettingsArg,
59+
cycles: u128,
60+
) -> Result<Principal, String> {
61+
let create_result = create_canister_with_extra_cycles(
3662
&CreateCanisterArgs {
3763
settings: create_canister_settings(create_settings_arg),
3864
},
3965
create_canister_cycles(cycles),
4066
)
4167
.await;
4268

43-
match record {
44-
Err(err) => Err(format!(
69+
let result = create_result.map_err(|err| {
70+
format!(
4571
"{} ({})",
4672
JUNO_ERROR_CANISTER_CREATE_FAILED,
4773
&err.to_string()
48-
)),
49-
Ok(record) => {
50-
let canister_id = record.canister_id;
74+
)
75+
})?;
5176

52-
let install = install_code(canister_id, wasm_arg, CanisterInstallMode::Install).await;
53-
54-
match install {
55-
Err(_) => Err(JUNO_ERROR_CANISTER_INSTALL_CODE_FAILED.to_string()),
56-
Ok(_) => Ok(canister_id),
57-
}
58-
}
59-
}
77+
Ok(result.canister_id)
6078
}
6179

6280
/// Asynchronously installs code on a specified canister.

0 commit comments

Comments
 (0)