Rust Canister initializing with value

I want to use the same code to deploy multiple canisters. dfx.json looks like

{
  "canisters": {
    "canister_1": {
      "candid": "same path",
      "package": "same path",
      "type": "rust"
    },
    "canister_2": {
      "candid": "same path",
      "package": "same path",
      "type": "rust"
    },
    "canister_3": {
      "candid": "same path",
      "package": "same path",
      "type": "rust"
    }
  },
  "defaults": {
    "build": {
      "args": "",
      "packtool": ""
    }
  },
  "dfx": "0.14.1",
  "version": 1
}

I have a stable structure

    pub static CANISTER_INFO: RefCell<StableCell<CanisterInfo, Memory>> =
        MEMORY_MANAGER.with(|memory_manager|
            RefCell::new(
                StableCell::init(memory_manager.borrow().get(CANISTER_INFO_MEMORY_ID), CanisterInfo 
         {
                name: "".to_string(),
            }).expect("Failed to init CANISTER_INFO")
            )
        );

Is there a way to somehow initialize the CanisterInfo.name field when deploying all the canisters? The name can be the same with the canister name specified in dfx.json. Worst case, I could call a API to set the name after deployment for each canister. But since I will have many canisters, doing that during development is a pain

At some point I’d like to see a field argument_file for canisters so you can point all of them at the same file and you’re good to go. Until then, I think the easiest solution is to write a script that calls dfx deploy with the same --argument <...> for every canister

1 Like

Thanks. I was not aware that there is a --argument option. Two questions

  1. Say I have a candid file (the actual code is more complex so I simply the request type)
type SetCanisterInfoRequest = record {
  name : text;
};
service : {
  set_canister_info : (SetCanisterInfoRequest) -> ();
}

I tried dfx canister call test_canister set_canister_info '(record { name="hey" })' and I got

Error: Failed to create argument blob.
Caused by: Failed to create argument blob.
  Invalid data: Unable to serialize Candid values: Invalid data: Unable to serialize Candid values: record field info not found

Did I miss anything? I followed the example here Internet Computer Loading

  1. When using dfx deploy test_canister --argument= as you suggested, what should I put in the argument using the example above? The official doc says

Specifies an argument using Candid syntax to pass to the canister during deployment. Note that this option requires you to define an actor class in the Motoko program.

but I still don’t understand. Are you aware of any example of this argument usage?

1 Like

Looks completely fine to me. Really stupid question: did you save the candid file and point at it properly? It sounds like it’s expecting a different record that includes a field called info

If your canister takes SetCanisterInfoRequest as an init argument then you’d simply call dfx deploy test_canister --argument='(record { name="hey" })'

1 Like

Thanks. I just passed the wrong record. Now it is working