# Imported Motoko canister has wrong type, \[Nat8\] instead of Blob

**URL:** <https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312>\
**Category:** Motoko\
**Tags:** candid, Bug\
**Created:** [July 12, 2023, 7:50am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312 "2023-07-12T07:50:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZenVoich](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zenvoich/32/5969_2.png) [@ZenVoich](https://forum.dfinity.org/u/ZenVoich)\
**Post date:** [July 12, 2023, 7:50am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/1 "2023-07-12T07:50:02Z")

</div>

dfx `0.14.2`  
moc `0.9.6`

For example

`foo.mo`

```auto
actor class Foo() {
  public func add(data : Blob) {}; // expect Blob
};

```

`bar.mo`

```auto
import Foo "canister:foo";

actor class Bar() {
  public func addToFoo(data : Blob) : async () {
    Foo.add(data); // send Blob
  };
};

```

On deploy I get error:

```auto
type error [M0096], expression of type
  Blob
cannot produce expected type
  [Nat8]

```

Interestingly, with the `--mode reinstall` flag, I can deploy without errors.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [July 12, 2023, 8:51am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/2 "2023-07-12T08:51:00Z")

</div>

This is confusing but actually expected, I think.

Since you are importing the class instance as a canister (not Motoko actor class library) moc doesn’t know it’s a Motoko canister. The candid interface will say `add : [vec Nat8] -> ()` and `Foo` will have imported type `[Nat8] -> async {}` after re-translation from Candid to Moc.

The code would work if you imported the actor class directly as a library and communicated with an instance of that class, because Moc then uses the Motoko type of the actor class direclty, not the generated Candid interface.

---

<div class="post-metadata">

**Author:** ![ZenVoich](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zenvoich/32/5969_2.png) [@ZenVoich](https://forum.dfinity.org/u/ZenVoich)\
**Post date:** [July 12, 2023, 9:17am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/3 "2023-07-12T09:17:12Z")

</div>

> [@claudio](#):
>
> The code would work if you imported the actor class directly as a library and communicated with an instance of that class, because Moc then uses the Motoko type of the actor class direclty, not the generated Candid interface.

I want to use different moc build args for `Foo` (`--max-stable-pages`)

> [@claudio](#):
>
> Since you are importing the class instance as a canister (not Motoko actor class library) moc doesn’t know it’s a Motoko canister. The candid interface will say `add : [vec Nat8] -> ()` and `Foo` will have imported type `[Nat8] -> async {}` after re-translation from Candid to Moc.

Candid supports `blob` type, right? Why doesn’t moc generate `blob` instead of `vec nat8`?

In `.dfx` folder I see that it generates blob in contructor.did and vec nat8 in service.did

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [July 12, 2023, 6:07pm UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/4 "2023-07-12T18:07:41Z")

</div>

In Candid, `blob` is just an abbreviation for `vec nat8` (but not in Motoko).

You can convert the data to an array if you don’t mind the expense (`Blob.toArray(data)`).

Another hacky workaround would be to cast `Foo` to another actor type that has the `blob` method. It should work, is gross, but cheaper than the array conversion.

Something like

```auto
{
   let foo = actor (Principal.toText(Principal.fromActor(Foo))) : actor { add : Blob -> () } 
   foo.add(data);
}

```

---

<div class="post-metadata">

**Author:** ![ZenVoich](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zenvoich/32/5969_2.png) [@ZenVoich](https://forum.dfinity.org/u/ZenVoich)\
**Post date:** [July 13, 2023, 8:35am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/5 "2023-07-13T08:35:18Z")

</div>

> [@claudio](#):
>
> You can convert the data to an array if you don’t mind the expense (`Blob.toArray(data)`).

Will it consume 4x more bytes if I use [Nat8] instead of Blob for inter-canister calls(message size)?

Not my case, but if it’s not just a blob argument there, but more complex nested type, shared between canisters, there would be more inconvenience.

> [@claudio](#):
>
> In Candid, `blob` is just an abbreviation for `vec nat8` (but not in Motoko).

I don’t understand why _Motoko_ doesn’t generate `blob`, since for _Motoko_ they are two different types.

Feels like a bug, not a feature to be honest.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [July 13, 2023, 10:32am UTC](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/6 "2023-07-13T10:32:10Z")

</div>

> Will it consume 4x more bytes if I use [Nat8] instead of Blob for inter-canister calls(message size)?

In the message (Candid binary), no, in memory, yes.

> Not my case, but if it’s not just a blob argument there, but more complex nested type, shared between canisters, there would be more inconvenience.

yep.

> I don’t understand why _Motoko_ doesn’t generate `blob`, since for _Motoko_ they are two different types.

Well, it does, but Candid `blob` is just Candid `vec nat8`. There is no distinct `blob` type to generate.

> Feels like a bug, not a feature to be honest.

Well, bug is a bit strong, but needless minimalism, maybe. Life would be simpler if Candid just had a distinct blob type, I think. I mean, it does for `principal`, IIRC.
