Getting exact same canister ID's for two separate projects locally and they share ledger funds as well

I am playing around with the examples/motoko/ledger-transfer at master · dfinity/examples · GitHub
example since I need interaction with the ledger and transferring funds from wallet to wallet for the app I want to build. I am doing all of this locally (have deployed ledger canister for each app as shown in the README.md of this specific example (followed the link to setting up ledger canister locally). All of this went well and I was able to test out the above linked example locally along with transferring funds.

What just hit me when I tried to just copy this example into another directory where I want to change things and slowly start working on the app that I actually want to have in the end is that after building ledger canister in that folder and deploying it and the base motoko canister with the app code I basically got exact same canister id’s for this project as the one that I had in the example project above. How is this possible? Is it because initially, when I ran deploy in my new app that I am making, I didn’t really change any code in the main Motoko canister, just left the code exactly the same as the one in the example project so the actual canister ID is generated from the contents of the file in the end? It’s so weird, especially since I can send funds to either my new app canister from the new project OR the old canister from the example project and both would increase the balance of the canister.

Did you copy the folder .dfx as well? It contains the information and, I guess, state for the local replica.

The canister ids are saved in .dfx/local/canisters/canister_ids.json and .dfx/local/canisters/wallets.json and there is .dfx/state.

I removed the whole .dfx folder and everything in it

Your development instance assignes canisters ids in the same order after a wipe-and-restart, so if you install canisters in the same order, they will get the same ids. Nothing too surprising here, I’d say?

1 Like

So, if I understand you correctly… Since every project folder with dfx in it is another local copy, dfx will start over from it’s default set of hard coded canister ID’s for each canister that we need setup per project, so in the end we will end up with multiple same canister ID’s in our local dev depending on how many local projects we have setup? And because dfx is ran from each specific project directory as a separate entity this should be no issue and it knows which canister is the one we want to call, even though it has the same ID as a canister from another locally setup project?

Yes, roughly like that. I am not sure what (if any) confusion can arise when you run dfx start in one project and then dfx deploy in another, though :slight_smile:

1 Like

You’ll only want one replica instance (dfx start) running. You can run this from any of the projects and deploy to it from the others.

If you run dfx start in project A’s directory, then deploy from project A, B, C etc. they’ll all deploy to project A’s replica. All the local replica state will be stored in project A’s .dfx directory. The canister ids will still be stored in each project’s respective .dfx directory, but now they’re all deployed to one replica they’ll be given unique canister ids. (Edit)

For your ledger canister example, you can build it in its own project but run dfx start in your app’s project directory and deploy both your app and the ledger to that.

And as Joachim says if you later need to start clean, always deploying the canisters in the same order ensures they’re given the same canister ids they had before.

3 Likes