how to call Canister of other project?
The truth is that, as far as I know, dfx does not currently make it easy for one project to reference a canister from another project, although the underlying tools like the motoko compiler do support this feature.
The simplest workaround is to merge the projects into a single project, but that’s not very modular and prevents you from deploying the projects separately, unless you use dfx canister install
to install individual canisters in the project instead of dfx deploy
, that build and deploys all of them.
Another workaround is to hardcode the canister id from one project in the source code of the canister of the client project, using actor references. This approach is not ideal as it means you’ll need to modify the client code when the canister id changes, and you’ll need to describe the canister interface as an actor type (instead of importing it from a Candid interface).
A third approach is to make the client project’s canisters actor classes, parameterized by the actor(s) from the first project, and pass in the canister references as installation arguments. Then the code can be built separately and need not be edited when the canister identifiers, passed as installation arguments, change.
Ideally, dfx would let one project reference the Candid interface and canister id of a canister contained in another project, and simple treat that as an import of the project. I don’t believe dfx has that functionality yet, though it has been discussed internally and is probably on the roadmap. Perhaps someone from the dfx team could confirm.
Despite this, if anyone knows how make dfx build dependent projects, it would be nice to contribute a small example of how to do this the examples collection here.
It would be great for the growth of the ecosystem to make it easy to piece together “canister lego.” +1 in support of adding this to the roadmap, if it’s not already there.