/// Creates a canister on the given `Runtime` (via a wallet canister if
/// provided; else using `ProvisionalCreateCanisterWithCycles` as a whitelisted
/// principal) and installs the provided Wasm.
Creates a canister on the given `Runtime` (via a wallet canister if
provided; else using `ProvisionalCreateCanisterWithCycles` as a whitelisted
principal) and installs the provided Wasm.
Creates a canister on the given ` Runtime ` ( via a wallet canister if provided ; else using ` ProvisionalCreateCanisterWithCycles ` as a whitelisted principal ) and installs the provided Wasm .
async fn install(
    wasm: Wasm,
    api: &Runtime,
    wallet_canister_id: Option<CanisterId>,
    cycles: u64,
    principal: Option<PrincipalId>,
) -> Result<Canister<'_>, String> {
    if let Some(wallet_canister_id) = wallet_canister_id {
        #[derive(CandidType, Clone, Deserialize)]
        struct CanisterSettings {
            controller: Option<PrincipalId>,
            compute_allocation: Option<candid::Nat>,
            memory_allocation: Option<candid::Nat>,
            freezing_threshold: Option<candid::Nat>,
        }
        #[derive(CandidType, Deserialize)]
        struct CreateCanisterArgs {
            cycles: u64,
            settings: CanisterSettings,
        }
        #[derive(CandidType, Deserialize)]
        struct CreateResult {
            canister_id: PrincipalId,
        }
        // Create canister via wallet canister.
        let wallet_canister = Canister::new(api, wallet_canister_id);
        let res: Result<CreateResult, String> = wallet_canister
            .update_(
                "wallet_create_canister",
                candid,
                (CreateCanisterArgs {
                    cycles,
                    settings: CanisterSettings {
                        controller: principal,
                        compute_allocation: None,
                        memory_allocation: None,
                        freezing_threshold: None,
                    },
                },),
            )
            .await?;
        // Install the Wasm.
        let canister_id = CanisterId::new(res?.canister_id).map_err(|e| e.to_string())?;
        let mut canister = Canister::new(api, canister_id);
        wasm.install_onto_canister(&mut canister, None).await?;
        Ok(canister)
    } else {
        // Create and install canister via whitelisted principal.
        wasm.install_(api, vec![]).await
    }
}
async fn install ( wasm : Wasm , api : & Runtime , wallet_canister_id : Option < CanisterId > , cycles : u64 , principal : Option < PrincipalId > , ) -> Result < Canister < ' _ > , String > { if let Some ( wallet_canister_id ) = wallet_canister_id { # [ derive ( CandidType Clone Deserialize ) ] struct CanisterSettings { controller : Option < PrincipalId > , compute_allocation : Option < candid :: Nat > , memory_allocation : Option < candid :: Nat > , freezing_threshold : Option < candid :: Nat > , } # [ derive ( CandidType Deserialize ) ] struct CreateCanisterArgs { cycles : u64 , settings : CanisterSettings , } # [ derive ( CandidType Deserialize ) ] struct CreateResult { canister_id : PrincipalId , } // Create canister via wallet canister. let wallet_canister = Canister :: new ( api , wallet_canister_id ) ; let res : Result < CreateResult , String > = wallet_canister . update_ ( " " , candid , ( CreateCanisterArgs { cycles , settings : CanisterSettings { controller : principal , compute_allocation : None , memory_allocation : None , freezing_threshold : None , } , } , ) , ) . await ? ; // Install the Wasm. let canister_id = CanisterId :: new ( res ? . canister_id ) . map_err ( | e | e . to_string ( ) ) ? ; let mut canister = Canister :: new ( api , canister_id ) ; wasm . install_onto_canister ( & mut canister , None ) . await ? ; Ok ( canister ) } else { // Create and install canister via whitelisted principal. wasm . install_ ( api , vec ! [ ] ) . await } }
Creates a canister on the given `Runtime` (via a wallet canister if
provided; else using `ProvisionalCreateCanisterWithCycles` as a whitelisted
principal) and installs the provided Wasm.
Creates a canister on the given ` Runtime ` ( via a wallet canister if provided ; else using ` ProvisionalCreateCanisterWithCycles ` as a whitelisted principal ) and installs the provided Wasm .
wasm
Wasm
api
&Runtime
wallet_canister_id
Option<CanisterId>
cycles
u64
principal
Option<PrincipalId>