# Motoko : encode an object into Candid to pass arguments into install\_code

**URL:** <https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330>\
**Category:** Developers\
**Tags:** candid\
**Created:** [April 21, 2022, 2:39pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330 "2022-04-21T14:39:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seb](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/seb/32/10340_2.png) [@Seb](https://forum.dfinity.org/u/Seb)\
**Post date:** [April 21, 2022, 2:39pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/1 "2022-04-21T14:39:20Z")

</div>

Hello,  
I am trying to use the IC management canister to install some wasm into an already created canister trough this method :

```auto
public type ICActor = actor {
install_code: shared(params: InstallCodeParams) -> async ();
};

 public type InstallCodeParams = {
        mode: InstallMode;
        canister_id: Principal;
        wasm_module: Blob;
        arg: Blob;
 };

```

I need to pass 4 arguments; as far as I understand they need to be passed as a Blob encoding their candid representation.  
Is there an easy way to get the candid blob directly from the object in Motoko, something like `didc encode ` but that could be used at runtime in Motoko ? (I would rather avoid to manually write the Blob…)

@skilesare is it what the bounty behind [Bounty - ICDevs.org CBOR and Candid Motoko Parser](https://icdevs.org/bounties/2022/02/22/CBOR-and-Candid-Motoko-Parser.html) is about ?  
I would like to be able to do something like Candid.encodeBlob(object) in Motoko.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [April 21, 2022, 5:02pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/2 "2022-04-21T17:02:41Z")

</div>

Only the `arg` field is Candid encoded. If the wasm module doesn’t take init args, you can pass an empty blob as well.

We are adding a new primitive in Motoko to allow serializing Motoko values into blob. It’s still work in progress.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [April 21, 2022, 5:29pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/3 "2022-04-21T17:29:09Z")

</div>

> [@chenyan](#):
>
> We are adding a new primitive in Motoko to allow serializing Motoko values into blob

!!! Will they be candid encoded? If so we can simplify the bounty quite a bit. We still need CBOR.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [April 21, 2022, 5:34pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/4 "2022-04-21T17:34:05Z")

</div>

Yes, the plan is to have primitives for `to_candid` and `from_candid` that converts between Motoko tuples and Blob.

---

<div class="post-metadata">

**Author:** ![Seb](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/seb/32/10340_2.png) [@Seb](https://forum.dfinity.org/u/Seb)\
**Post date:** [April 21, 2022, 6:49pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/5 "2022-04-21T18:49:00Z")

</div>

Yes I was talking about the arg field because the module needs arguments to be initialized; that will be really amazing to have that feature!

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 5, 2022, 6:19am UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/6 "2022-05-05T06:19:06Z")

</div>

@chenyan

> [@chenyan](#):
>
> Yes, the plan is to have primitives for `to_candid` and `from_candid` that converts between Motoko tuples and Blob.

When you say “converts between Motoko tuples and Blob”, would this not then apply to the record type?

The ability to `Blob` encode an record/object is especially important because initializing an actor canister class with a record better supports backwards compatibility when upgrading, as opposed to than a list of arguments.

Not having this feature in Motoko is borderline blocking, as it limit Motoko devs in the following ways:

- If we want to use values that already exist on the canister memory that would be rolling out the dynamic upgrades it makes sense that this would happen on the backend. Not being able to Blob encode a record forces Motoko developers who want to perform upgrades on the backend to simplify their instatiation arguments for an actor canister

Or

- It forces us to [use @dfinity/candid to encode the arguments from the frontend using Javascript](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/10) or tooling like `didc` for pushing out upgrades where the canister requires multiple instantiation arguments. This becomes awkward when an upgrade involves pulling down data to the client from one canister in order to retrieve the arguments necessary to rollout upgrades with the proper arguments to other canisters. It also means additional repeated development time where all Motoko development teams are building out custom tooling farther away from the canister rolling out the upgrades, and all of this could be solved with improved Motoko encoding capability.

@claudio @matthewhammer

I was looking at [candid/tools/didc at master · dfinity/candid · GitHub](https://github.com/dfinity/candid/tree/master/tools/didc), and would love a Motoko equivalent of

```auto
didc encode ...

```

There might need to be a type definition in there `<T>`, but how hard would it be to add something with this functionality to the Blob class in motoko-base?

The `debug_show` is amazing at converting types to Text, how difficult would it be to convert a well typed record to a Blob from the Motoko side?

Is there a prim functionality that I could leverage?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 5, 2022, 5:02pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/7 "2022-05-05T17:02:56Z")

</div>

The top-level Candid message is always a tuple: in the textual representation, Candid values are always wrapped inside a parenthesis. A record type in init arg is actually a one-tuple with record type: `( record {...} )`.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 5, 2022, 6:34pm UTC](https://forum.dfinity.org/t/motoko-encode-an-object-into-candid-to-pass-arguments-into-install-code/12330/8 "2022-05-05T18:34:39Z")

</div>

Oh that’s amazing then! Thanks Chenyan, really looking forward to this
