Install_code() - Easiest way to convert a .wasm module to [Nat8]?

Depending on the size of your Wasm module, the Motoko compiler won’t be able to process it if it’s an array. See: https://github.com/dfinity/motoko/issues/3557

So you must convert your wasm module to hexadecimal first (there are a many options online and some Unix tools to do that offline) and convert the result to be compatible with Motoko’s Text, like:

let wasmModule: Blob = "\de\ad\be\ef";

Then it’s simple to install the module:

Cycles.add(300_000_000_000);
let id = await IC.create_canister({
     settings = ?{
        controllers = ?[Principal.fromActor(this)];
        compute_allocation = null;
        memory_allocation = null;
        freezing_threshold = null;
    }
});
  await IC.install_code({
      mode = #install;
      canister_id = id.canister_id;
      wasm_module = wasmModule;
      arg = to_candid({
          //your canister's arguments, ex: foo = 1234;
      });
  });
3 Likes