How do I access an actor (not actor class) from another Motoko anchor?

This question shows that I can’t import an actor.

So, if having several actors in dfx.json, how do I pass principals of canisters from one actor to another?

{
  "canisters": {
    "A": {
      "main": "src/A.mo",
      "type": "motoko"
    },
    "B": {
      "main": "src/B.mo",
      "type": "motoko"
    }
  }
}

For example, how to call B from A (if they are actors, not actor classes)?

So, I can create a canister matching an interface, then initialize another canister with ID of the first canister, obtained like dfx canister id B and use that interface to access the first canister.

Is the above the recommended way to use single actors (not actor classes)?

import B "canister:B"

1 Like

add B as a dependency to A

{
  "canisters": {
    "A": {
      "main": "src/A.mo",
      "type": "motoko",
      "dependencies": ["B"]
    },
    "B": {
      "main": "src/B.mo",
      "type": "motoko"
    }
  }
}

then import in A.mo

import B "canister:B";