Create canister and install code with wasm_module

Hello.
I can create the canister(s) using the new operation, referring to the actor class

Variant №1
Example: (very roughly and briefly)

actor class ObjectActor() = this{};
let oa: = await ObjectActor();
//ID new canister
let p: = Principal.fromActor(oa);

Variant №2
I can also create an empty canister. And use it when necessary.
Example:

public func create_canister_empty_with_cycles(settings: ?canister_settings, amount : ?Nat)
      : async { canister_id : canister_id } { 
            ExperimentalCycles.add(100_000_000_000); 
            let canister_new_with_cycles = 
                  await IC.provisional_create_canister_with_cycles({ settings : ?canister_settings; amount : ?Nat;});

  return canister_new_with_cycles
};

Next, using template in the specification.
Apparently, I can update an empty (and not empty) canister. I use the canister_id obtained above.

install_code : shared {
arg : [Nat8];
wasm_module : wasm_module;
mode : { #reinstall; #upgrade; #install };
canister_id : canister_id;
} → async ();

Question: how do I compile the code of the actor class(ObjectActor) in wasm_module?
Is it possible to use it dynamically in the IC today?

You can compile the actor class to suitable wasm just by passing it as the root file to the moc compiler (or making it the main file of a dfx actor).

You’re main difficulty will then be quoting the code (and its serialized argument) as a couple of blobs in Motoko to pass to IC.install_code We don’t currently expose an official way for users to to serialize shared types to blobs.

(FTR: the way the compiler currently compiles an actor that imports an actor class is to recursively invoke the compiler on the actor class, embed the binary of the class as a blob in the importer, and compile a helper function for the class constructor that serializes the argument to a blob, creates and installs a new actor with the new blob and then casts the principal to the expected interface.)

2 Likes

Hi, Claudio. Thanks. The problem was adding cycles, but I solved it. I realized that it is better to use standard methods. Apparently, if you update the wasm code with install_code (wash_module), the built-in logic of the actor’s work ( functions, properties)it will be unavailable for calls from outside it.