Getting issue while calling create_canister

    let args = CreateCanisterArgument {
        settings: Some(CanisterSettings {
            controllers: Some(vec![Principal::from_text("bkyz2-fmaaa-aaaaa-qaaaq-cai").unwrap()]),
            compute_allocation: None,
            memory_allocation: None,
            freezing_threshold: None,
            reserved_cycles_limit: None,
            log_visibility: None,
            wasm_memory_limit: None,
        }),
    };

    ic_cdk::println!("args {:?} ",args);

    let (canister_id,): (Principal,) = ic_cdk::api::call::call_with_payment(
        Principal::management_canister(),
        "create_canister",
        (&args,),  // Note the tuple here
        100_000_000_000, // Adjust this cycle amount as needed
    ).await.map_err(|(code, msg)| (code, format!("Failed to create canister: {}", msg)))?;

Below error is coming
(variant {Err=record {variant {CanisterError}; “Failed to create canister: failed to decode canister response as (ic_principal::Principal,): Fail to decode argument 0 from table0 to principal”}})

create_canister does not return (principal) but instead

type create_canister_result = record {
    canister_id : canister_id;
};

I think you’d be happier if you used ic_cdk::api::management_canister::main::create_canister, which does most of this for you

1 Like

When I am install_code getting error
Failed to install code: CanisterError - Error from Canister ppuxv-yuaaa-aaaaa-qabaa-cai: Canister called `ic0.trap` with message: failed to decode call arguments: Custom(Fail to decode argument 0 from table0 to variant { Upgrade : opt record { token_symbol : opt text; transfer_fee : opt nat; metadata : opt vec record { text; variant { Int : int; Nat : nat; Blob : vec nat8; Text : text }; }; maximum_number_of_accounts : opt nat64; accounts_overflow_trim_quantity : opt nat64; change_fee_collector : opt variant { SetTo : record { owner : principal; subaccount : opt vec nat8 }; Unset; }; max_memo_length : opt nat16; token_name : opt text; feature_flags : opt record { icrc2 : bool }; }; Init : record { decimals : opt nat8; token_symbol : text; transfer_fee : nat; metadata : vec record { text; variant { Int : int; Nat : nat; Blob : vec nat8; Text : text }; }; minting_account : record { owner : principal; subaccount : opt vec nat8 }; initial_balances : vec record { record { owner : principal; subaccount : opt vec nat8 }; nat; }; maximum_number_of_accounts : opt nat64; accounts_overflow_trim_quantity : opt nat64; fee_collector_account : opt record { owner : principal; subaccount : opt vec nat8; }; archive_options : record { num_blocks_to_archive : nat64; max_transactions_per_response : opt nat64; trigger_threshold : nat64; max_message_size_bytes : opt nat64; cycles_for_archive_creation : opt nat64; node_max_memory_size_bytes : opt nat64; controller_id : principal; }; max_memo_length : opt nat16; token_name : text; feature_flags : opt record { icrc2 : bool }; }; }
using below to install_code

        mode: CanisterInstallMode::Install,
        canister_id: canister_2.0.canister_id,
        wasm_module:wasm_module.clone(),
        arg:arg.clone(),
    };
match ic_cdk::api::management_canister::main::install_code(install_args).await {
        Ok(_) => ic_cdk::println!("Code installed successfully"),
        Err((code, msg)) => {
            ic_cdk::println!("Failed to install code: {:?} - {}", code, msg);
            return Err((code, msg));
        }
    }```

means that your init arguments don’t have the correct type/structure. If you post the full error I may be able to spot the difference, but I am guessing that the enum may be the problem

Worked , got the issue .
Thanks for the prev help

I also got this error when passing install_code initialization parameter. How do I fix it

check the struct you are using