@@ -46,46 +46,10 @@ use ic_cdk::api::management_canister::main::{
4646 CanisterIdRecord , CanisterInstallMode , CanisterSettings , CreateCanisterArgument ,
4747 InstallCodeArgument , LogVisibility ,
4848} ;
49- use ic_cdk:: api:: {
50- call:: { call, call_with_payment128, CallResult } ,
51- canister_version,
52- } ;
5349use serde:: { Deserialize , Serialize } ;
5450use std:: sync:: Arc ;
5551use std:: { any:: Any , collections:: HashMap , fmt:: Debug , future:: Future } ;
5652
57- #[ derive( CandidType , Deserialize ) ]
58- struct CreateCanisterArg {
59- settings : Option < CanisterSettings > ,
60- subnet_type : Option < String > ,
61- subnet_selection : Option < SubnetSelection > ,
62- }
63-
64- #[ derive( CandidType , Deserialize ) ]
65- enum SubnetSelection {
66- Subnet { subnet : Principal } ,
67- Filter { filter : SubnetFilter } ,
68- }
69-
70- #[ derive( CandidType , Deserialize ) ]
71- struct SubnetFilter {
72- subnet_type : Option < String > ,
73- }
74-
75- #[ derive(
76- CandidType , Serialize , Deserialize , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Clone , Copy ,
77- ) ]
78- pub struct GetSubnetForCanisterResponse {
79- subnet_id : Option < Principal > ,
80- }
81-
82- #[ derive(
83- CandidType , Serialize , Deserialize , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Clone , Copy ,
84- ) ]
85- pub struct GetSubnetForCanisterArgs {
86- canister_id : Option < Principal > ,
87- }
88-
8953/// Error types for storage operations
9054#[ derive( Debug ) ]
9155pub enum NewStorageError {
@@ -253,22 +217,6 @@ where
253217 }
254218
255219 if canister_id == Principal :: anonymous ( ) {
256- let get_subnet_result: CallResult < ( GetSubnetForCanisterResponse , ) > = call (
257- Principal :: from_text ( "rwlgt-iiaaa-aaaaa-aaaaa-cai" ) . unwrap ( ) , // registry canister
258- "get_subnet_for_canister" ,
259- ( GetSubnetForCanisterArgs {
260- canister_id : Some ( ic_cdk:: id ( ) ) ,
261- } , ) ,
262- )
263- . await ;
264-
265- let subnet = match get_subnet_result {
266- Ok ( subnet) => subnet. 0 . subnet_id ,
267- Err ( e) => {
268- return Err ( NewCanisterError :: CreateCanisterError ( format ! ( "{e:?}" ) ) ) ;
269- }
270- } ;
271-
272220 // Define the initial settings for the new canister
273221 let settings = CanisterSettings {
274222 controllers : Some ( self . controllers . clone ( ) ) ,
@@ -279,55 +227,23 @@ where
279227 log_visibility : Some ( LogVisibility :: Public ) ,
280228 wasm_memory_limit : None ,
281229 } ;
282-
283- let canister_call_result : CallResult < ( CanisterIdRecord , ) > = retry_async (
230+ // Step 1: Create the canister
231+ canister_id = match retry_async (
284232 || {
285- call_with_payment128 (
286- Principal :: management_canister ( ) ,
287- "create_canister" ,
288- ( CreateCanisterArg {
233+ create_canister (
234+ CreateCanisterArgument {
289235 settings : Some ( settings. clone ( ) ) ,
290- subnet_type : None ,
291- subnet_selection : Some ( SubnetSelection :: Subnet {
292- subnet : subnet. unwrap ( ) ,
293- } ) ,
294- } , ) ,
236+ } ,
295237 self . initial_cycles as u128 ,
296238 )
297239 } ,
298240 3 ,
299241 )
300- . await ;
301-
302- canister_id = match canister_call_result {
242+ . await
243+ {
303244 Ok ( canister) => canister. 0 . canister_id ,
304245 Err ( e) => {
305- // can not create canister, try to create it with a different subnet
306- let canister_call_result: CallResult < ( CanisterIdRecord , ) > = retry_async (
307- || {
308- call_with_payment128 (
309- Principal :: management_canister ( ) ,
310- "create_canister" ,
311- ( CreateCanisterArg {
312- settings : Some ( settings. clone ( ) ) ,
313- subnet_type : None ,
314- subnet_selection : None ,
315- } , ) ,
316- self . initial_cycles as u128 ,
317- )
318- } ,
319- 3 ,
320- )
321- . await ;
322-
323- match canister_call_result {
324- Ok ( canister) => canister. 0 . canister_id ,
325- Err ( e) => {
326- return Err ( NewCanisterError :: CreateCanisterError ( format ! (
327- "{e:?}"
328- ) ) ) ;
329- }
330- }
246+ return Err ( NewCanisterError :: CreateCanisterError ( format ! ( "{e:?}" ) ) ) ;
331247 }
332248 } ;
333249
0 commit comments