@@ -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}
0 commit comments