Dfx encode principal

I try to install a canister with a principal as argument but got following error. Probably missing something, anyone notice the issue?

[Canister …] Panicked at ‘called Option::unwrap() on a None value’, src/console/src/lib.rs:43:19

#[derive(CandidType, Deserialize)]
    pub struct ConsoleArgs {
        pub manager: Principal,
    }

#[init]
fn init() {
    let call_arg = arg_data::<(Option<ConsoleArgs>,)>().0;
    let manager = call_arg.unwrap().manager;

    print(format!("Here {}", manager.to_string()));

script

#!/usr/bin/env bash

MANAGER=$(dfx identity get-principal)

dfx deploy console --argument '(record {manager = principal"'${MANAGER}'";})' --mode reinstall

Most likely related to Bash not expanding variables between single quotes.

echo '(record {manager = principal"'${MANAGER}'";})' should reveal the string you’re providing and allow you to experiment.

For what it’s worth; if you always want the manager to be the caller, I think you can remove the argument and use ic_cdk::caller() instead.

2 Likes

Thanks! @nmattia literally just helped me debug this (not all heroes wear capes) and while we did not find the solution, he found a workaround by encoding the argument in raw

#!/usr/bin/env bash

MANAGER=$(dfx identity get-principal)

dfx deploy console --argument "$(didc encode '(record {manager = principal"'${MANAGER}'";})' --format hex)" --argument-type raw

and making the argument mandatory

let call_arg = arg_data::<(ConsoleArgs,)>().0;
let manager = call_arg.manager;

Oh that’s a good idea :+1:

3 Likes

Indeed, can confirm, your idea @paulyoung works out. I want the manager to be the one that install first the canister so using ic_cdk::caller() makes it easier. Thanks!

2 Likes