This repository demonstrates how to dynamically deploy canisters in Motoko using a Manager canister. The Manager is responsible for creating new Greeter canisters, setting their controllers, and managing them.
The Manager canister allows users to create new Greeter canisters dynamically. When a user calls createGreeter
, the Manager:
- Creates a new Greeter canister.
- Assigns the caller as a controller.
- Updates the canister settings using the IC Management Canister.
This approach enables on-demand canister deployment for multi-user applications.
sequenceDiagram
User->>Manager: Request to create a Greeter canister
Manager->>Greeter: Instantiate new Greeter canister
Manager->>IC: Update settings (assign controllers)
IC-->>Manager: Confirm settings update
Manager-->>User: Return Greeter canister ID
User->>Greeter: Send greeting request
Greeter-->>User: Respond with greeting message
The Manager is deployed first, allowing users to request Greeter canisters.
- A user calls
createGreeter()
, passing their principal. - The Manager:
- Instantiates a Greeter canister.
- Adds it to its list of greeters.
- Retrieves the Greeter canister ID.
- Updates the settings to assign the caller as a controller.
- Returns the canister ID to the caller.
- Users can interact with their Greeter canister by calling
greet(name)
, which returns a personalized greeting.
-
Deploy the Manager canister:
dfx deploy dynamic_canister_deployment_backend
-
Create a new Greeter canister:
dfx canister call dynamic_canister_deployment_backend createGreeter
-
Interact with the Greeter:
dfx canister call <greeter_canister_id> greet '("Alice")'
-
Restricting Anonymous Calls:
Theinspect
function ensures that anonymous calls are rejected. -
Access Control:
Each Greeter canister only assigns control to its creator.