|
| 1 | +# Motoko Canister Creation Examples |
| 2 | + |
| 3 | +This project demonstrates various approaches to creating and managing canisters on the Internet Computer using Motoko. It showcases the differences between high-level actor class management and low-level management canister operations. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The example includes implementations of: |
| 8 | + |
| 9 | +- **Actor Class Management**: High-level canister creation using the `system` keyword |
| 10 | +- **Manual Canister Management**: Low-level creation using the management canister directly |
| 11 | +- **Canister Lifecycle Operations**: Upgrade and reinstall operations |
| 12 | + |
| 13 | +## Key Differences |
| 14 | + |
| 15 | +### Actor Class Management (High-level) |
| 16 | +- Simpler API with automatic WASM installation |
| 17 | +- Limited canister settings: `controllers`, `compute_allocation`, `memory_allocation`, `freezing_threshold` |
| 18 | +- Good for most common use cases |
| 19 | +- [Documentation](https://internetcomputer.org/docs/motoko/language-manual#actor-class-management) |
| 20 | + |
| 21 | +### Management Canister (Low-level) |
| 22 | +- Full control over canister creation and settings |
| 23 | +- Access to all settings: `reserved_cycles_limit`, `wasm_memory_limit`, `log_visibility`, `wasm_memory_threshold` |
| 24 | +- Requires separate steps for creation and code installation |
| 25 | +- [Documentation](https://internetcomputer.org/docs/references/ic-interface-spec#ic-create_canister) |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +- [DFX](https://internetcomputer.org/docs/current/developer-docs/setup/install) 0.29.0 or later |
| 30 | +- [Mops](https://mops.one/) package manager |
| 31 | + |
| 32 | +## Project Structure |
| 33 | + |
| 34 | +``` |
| 35 | +├── src/ |
| 36 | +│ └── backend/ |
| 37 | +│ ├── Main.mo # Main actor with canister creation examples |
| 38 | +│ ├── Child.mo # Simple actor class for demonstrations |
| 39 | +│ └── AnotherChild.mo # Alternative actor class for upgrades |
| 40 | +├── dfx.json # DFX configuration |
| 41 | +├── mops.toml # Mops package configuration |
| 42 | +└── README.md # This file |
| 43 | +``` |
| 44 | + |
| 45 | +## Getting Started |
| 46 | + |
| 47 | +### 1. Start the local Internet Computer |
| 48 | + |
| 49 | +```bash |
| 50 | +dfx start --background |
| 51 | +``` |
| 52 | + |
| 53 | +### 2. Deploy the canister |
| 54 | + |
| 55 | +```bash |
| 56 | +dfx deploy |
| 57 | +``` |
| 58 | + |
| 59 | +This will deploy the main canister that contains all the canister creation examples. |
| 60 | + |
| 61 | +## Available Functions |
| 62 | + |
| 63 | +### 1. Actor Class Creation (High-level) |
| 64 | + |
| 65 | +#### `newActorClass(cycles: Nat)` |
| 66 | +Creates a new canister using actor class with automatic installation. |
| 67 | + |
| 68 | +```bash |
| 69 | +# Create a canister with 2 trillion cycles |
| 70 | +dfx canister call backend newActorClass '(2_000_000_000_000)' |
| 71 | +``` |
| 72 | + |
| 73 | +#### `installActorClass(cycles: Nat)` |
| 74 | +Creates a canister and installs an actor class using a two-step process. |
| 75 | + |
| 76 | +```bash |
| 77 | +# Create and install actor class with 2 trillion cycles |
| 78 | +dfx canister call backend installActorClass '(2_000_000_000_000)' |
| 79 | +``` |
| 80 | + |
| 81 | +### 2. Canister Lifecycle Management |
| 82 | + |
| 83 | +#### Understanding Upgrade vs Reinstall |
| 84 | + |
| 85 | +This example demonstrates the critical difference between **upgrading** and **reinstalling** canisters: |
| 86 | + |
| 87 | +**🔄 Upgrade (`#upgrade`)** |
| 88 | +- **State is PRESERVED**: All mutable variables keep their current values |
| 89 | +- **New functionality is added**: The `substractFromValue` endpoint becomes available |
| 90 | +- **Existing functionality remains**: All original endpoints continue working |
| 91 | +- **Use case**: Adding features, bug fixes, optimizations while keeping data |
| 92 | + |
| 93 | +**🔥 Reinstall (`#reinstall`)** |
| 94 | +- **State is RESET**: All mutable variables return to their initial values |
| 95 | +- **New functionality is added**: The `substractFromValue` endpoint becomes available |
| 96 | +- **Existing functionality resets**: Original endpoints work but with fresh state |
| 97 | +- **Use case**: Breaking changes, schema migrations, fresh start scenarios |
| 98 | + |
| 99 | +#### `upgradeActorClass(canisterId: Principal)` |
| 100 | +Upgrades an existing canister to use a different actor class (preserves state). |
| 101 | + |
| 102 | +```bash |
| 103 | +# Upgrade a canister (replace with actual canister ID) |
| 104 | +dfx canister call backend upgradeActorClass '(principal "rdmx6-jaaaa-aaaaa-aaadq-cai")' |
| 105 | +``` |
| 106 | + |
| 107 | +#### `reinstallActorClass(canisterId: Principal)` |
| 108 | +Reinstalls an existing canister with a different actor class (destroys state). |
| 109 | + |
| 110 | +```bash |
| 111 | +# Reinstall a canister (replace with actual canister ID) |
| 112 | +dfx canister call backend reinstallActorClass '(principal "rdmx6-jaaaa-aaaaa-aaadq-cai")' |
| 113 | +``` |
| 114 | + |
| 115 | +### 3. Manual Canister Management (Low-level) |
| 116 | + |
| 117 | +#### `createAndInstallCanisterManually(cycles: Nat)` |
| 118 | +Creates a canister manually using the management canister with full control over settings. |
| 119 | + |
| 120 | +```bash |
| 121 | +# Create canister manually with advanced settings |
| 122 | +dfx canister call backend createAndInstallCanisterManually '(2_000_000_000_000)' |
| 123 | +``` |
| 124 | + |
| 125 | +## Example Workflow |
| 126 | + |
| 127 | +Here's a complete example demonstrating all canister creation approaches and the differences between upgrade vs reinstall: |
| 128 | + |
| 129 | +### Part 1: Different Canister Creation Approaches |
| 130 | + |
| 131 | +```bash |
| 132 | +# 1. Start local replica |
| 133 | +dfx start --background --clean |
| 134 | + |
| 135 | +# 2. Deploy the main canister |
| 136 | +dfx deploy --with-cycles 30000000000000 |
| 137 | + |
| 138 | +# 3. Create a canister using actor class (high-level) |
| 139 | +CANISTER1=$(dfx canister call backend newActorClass '(2_000_000_000_000)' | grep -o 'principal "[^"]*"' | cut -d'"' -f2) |
| 140 | +echo "Created canister via actor class: $CANISTER1" |
| 141 | + |
| 142 | +# 4. Create a canister using manual management (low-level) |
| 143 | +CANISTER2=$(dfx canister call backend createAndInstallCanisterManually '(2_000_000_000_000)' | grep -o 'principal "[^"]*"' | cut -d'"' -f2) |
| 144 | +echo "Created canister manually: $CANISTER2" |
| 145 | + |
| 146 | +# 5. Create canister with two-step process |
| 147 | +CANISTER3=$(dfx canister call backend installActorClass '(2_000_000_000_000)' | grep -o 'principal "[^"]*"' | cut -d'"' -f2) |
| 148 | +echo "Created canister with install process: $CANISTER3" |
| 149 | +``` |
| 150 | + |
| 151 | +### Part 2: Upgrade vs Reinstall Demonstration |
| 152 | + |
| 153 | +```bash |
| 154 | +# 6. Create additional canisters for testing upgrade vs reinstall |
| 155 | +CANISTER_UPGRADE=$(dfx canister call backend newActorClass '(2_000_000_000_000)' | grep -o 'principal "[^"]*"' | cut -d'"' -f2) |
| 156 | +CANISTER_REINSTALL=$(dfx canister call backend newActorClass '(2_000_000_000_000)' | grep -o 'principal "[^"]*"' | cut -d'"' -f2) |
| 157 | +echo "Created upgrade test canister: $CANISTER_UPGRADE" |
| 158 | +echo "Created reinstall test canister: $CANISTER_REINSTALL" |
| 159 | + |
| 160 | +# 7. Test initial state of both canisters |
| 161 | +echo "=== Initial State ===" |
| 162 | +echo "Upgrade canister initial value:" |
| 163 | +dfx canister call $CANISTER_UPGRADE getValue |
| 164 | +echo "Reinstall canister initial value:" |
| 165 | +dfx canister call $CANISTER_REINSTALL getValue |
| 166 | + |
| 167 | +# 8. Modify state by incrementing internal counters |
| 168 | +echo "=== Modifying State ===" |
| 169 | +echo "Adding 10 to upgrade canister..." |
| 170 | +dfx canister call $CANISTER_UPGRADE addToValue '(10)' |
| 171 | +echo "Adding 20 to reinstall canister..." |
| 172 | +dfx canister call $CANISTER_REINSTALL addToValue '(20)' |
| 173 | + |
| 174 | +# 9. Upgrade the first canister (preserves state) |
| 175 | +echo "=== Performing Upgrade (State Preserved) ===" |
| 176 | +dfx canister call backend upgradeActorClass "(principal \"$CANISTER_UPGRADE\")" |
| 177 | +echo "Upgraded canister: $CANISTER_UPGRADE" |
| 178 | + |
| 179 | +# 10. Test upgraded canister - should have preserved state AND new functionality |
| 180 | +echo "Upgraded canister value (should be preserved):" |
| 181 | +dfx canister call $CANISTER_UPGRADE getValue |
| 182 | +echo "Testing new substractFromValue endpoint:" |
| 183 | +dfx canister call $CANISTER_UPGRADE substractFromValue '(5)' |
| 184 | + |
| 185 | +# 11. Reinstall the second canister (resets state) |
| 186 | +echo "=== Performing Reinstall (State Reset) ===" |
| 187 | +dfx canister call backend reinstallActorClass "(principal \"$CANISTER_REINSTALL\")" |
| 188 | +echo "Reinstalled canister: $CANISTER_REINSTALL" |
| 189 | + |
| 190 | +# 12. Test reinstalled canister - should have reset state BUT new functionality |
| 191 | +echo "Reinstalled canister value (should be reset to initial):" |
| 192 | +dfx canister call $CANISTER_REINSTALL getValue |
| 193 | +echo "Adding 20 to reinstall canister..." |
| 194 | +dfx canister call $CANISTER_REINSTALL addToValue '(20)' |
| 195 | +echo "Testing new substractFromValue endpoint:" |
| 196 | +dfx canister call $CANISTER_REINSTALL substractFromValue '(5)' |
| 197 | + |
| 198 | +echo "=== Summary ===" |
| 199 | +echo "✅ Different creation approaches: actor class, manual, two-step" |
| 200 | +echo "🔄 Upgrade: State preserved, new functionality added" |
| 201 | +echo "🔥 Reinstall: State reset, new functionality added" |
| 202 | +``` |
| 203 | + |
| 204 | +## Understanding the Code |
| 205 | + |
| 206 | +### Actor Classes |
| 207 | +- `Child.mo`: A simple persistent actor class with mutable state for demonstrating initial installations and state behavior |
| 208 | +- `AnotherChild.mo`: An extended actor class with additional `substractFromValue` functionality used for upgrade/reinstall demonstrations |
| 209 | + |
| 210 | +### State Behavior Demonstration |
| 211 | +The example shows how: |
| 212 | +- **Child**: Has basic functionality (`getValue`, `addToValue`) and mutable state |
| 213 | +- **AnotherChild**: Extends Child with new functionality (`substractFromValue`) |
| 214 | +- **Upgrade**: Migrates from Child to AnotherChild while preserving any modified state |
| 215 | +- **Reinstall**: Migrates from Child to AnotherChild but resets state to initial values |
| 216 | + |
| 217 | +### Main Functions |
| 218 | +- **High-level functions** use Motoko's `system` keyword with actor classes |
| 219 | +- **Low-level functions** interact directly with the management canister |
| 220 | +- **Lifecycle functions** demonstrate upgrade and reinstall capabilities |
| 221 | + |
| 222 | +## Cycles Management |
| 223 | + |
| 224 | +All functions require cycles to create canisters. The examples use 2 trillion cycles (2_000_000_000_000), which is sufficient for most development purposes. In production, you'll want to calculate appropriate cycle amounts based on your canister's needs. |
| 225 | + |
| 226 | +## Troubleshooting |
| 227 | + |
| 228 | +### Common Issues |
| 229 | + |
| 230 | +1. **Insufficient cycles**: Increase the cycle amount in function calls |
| 231 | +2. **Invalid canister ID**: Ensure you're using the correct Principal format |
| 232 | +3. **Deploy failures**: Check that dfx is running and properly configured |
| 233 | + |
| 234 | +### Getting Help |
| 235 | + |
| 236 | +- [Internet Computer Documentation](https://internetcomputer.org/docs) |
| 237 | +- [Motoko Documentation](https://internetcomputer.org/docs/motoko) |
| 238 | +- [DFX Command Reference](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/) |
| 239 | +- [Developer Forum](https://forum.dfinity.org/) |
| 240 | + |
| 241 | +## Related Examples |
| 242 | + |
| 243 | +For more Motoko examples, visit the [official examples repository](https://github.com/dfinity/examples/tree/master/motoko). |
| 244 | + |
| 245 | +## License |
| 246 | + |
| 247 | +This project is licensed under the Apache 2.0 license. See LICENSE for more details. |
0 commit comments