Canister Architecture
The OpenChat Bot SDK is designed to deploy as smart contracts (canisters) on the Internet Computer blockchain. Canisters are WebAssembly modules that can store data and respond to messages.OCBot (TallyBot)
Tracks governance proposal tallies and voting statistics
CG_Bot (ProposalBot)
Sends automated notifications for new proposals
Two-Canister Design
The SDK includes two separate canister implementations:OCBot (TallyBot)
Main File:backend/TallyBot/TallyBotMain.mo
- Receives tally updates from authorized notifiers
- Formats and posts voting statistics to subscribed channels
- Manages subscriptions for multiple governance canisters
- Tracks message IDs for editing tally updates
CG_Bot (ProposalBot)
Main File:backend/ProposalBot/ProposalBotMain.mo
- Polls governance canisters for new proposals
- Formats and posts proposal notifications to channels
- Manages topic-based subscriptions
- Handles periodic timer-based updates
While both canisters are named
OCBot in their source, they are deployed as separate canisters with different configurations in dfx.json.dfx.json Configuration
The project’sdfx.json file defines the canister configuration:
Cycles Management
Understanding Cycles
Cycles are the “fuel” that powers computation and storage on the Internet Computer:- 1 XDR ≈ 1 trillion cycles (1T)
- Used for canister operations, storage, and message execution
- Canisters must maintain a cycle balance to remain active
Required Cycles
Canister Creation
~0.5 XDR (500B cycles) per canister
Bot Registration
10 XDR (10T cycles) fee to OpenChat
Recommended Initial
12+ XDR (12T cycles) total per bot
Ongoing Operations
Variable based on activity
Cycle Breakdown
initBot():
Local Deployment
Prerequisites
Setup Steps
1. Clone and Install
1. Clone and Install
2. Start Local Replica
2. Start Local Replica
3. Deploy Canisters
3. Deploy Canisters
4. Get Canister IDs
4. Get Canister IDs
Production Deployment
Step 1: Create Canister with Cycles
Create a canister with sufficient cycles:The
--with-cycles flag specifies 12 XDR (12 trillion cycles), which covers:- 0.5 XDR for canister creation
- 10 XDR for OpenChat bot registration
- 1.5 XDR buffer for operations
Step 2: Deploy to Internet Computer
Step 3: Initialize the Bot
Step 4: Add Yourself as Custodian
Canister Management
Checking Canister Status
The SDK includes built-in methods to check canister health:CLI Status Check
Topping Up Cycles
When cycles run low:Upgrade Considerations
Stable Variables
All state that must persist across upgrades should use thestable keyword:
Critical
Variables not marked as
stable will be reset to their initial values during canister upgrades!Pre-Upgrade and Post-Upgrade Hooks
The ProposalBot includes upgrade hooks:Upgrade Process
Upgrade Modes
Upgrade Modes
install: Fresh install (wipes all data)upgrade: Preserves stable variablesreinstall: Wipes data but keeps canister ID
upgrade mode in production!Monitoring and Maintenance
View Logs
Clear Logs
Check Bot Status
#NotInitialized: Bot not yet registered with OpenChat#Initializing: Registration in progress#Initialized: Bot ready to use
Security Best Practices
Controller Management
Keep canister controllers to a minimum and use hardware wallets
Custodian List
Only add trusted principals to the custodian list
Cycle Monitoring
Set up alerts when cycle balance drops below threshold
Regular Backups
Export stable variable state regularly
Controller vs Custodian
- Controller: Has full control over the canister (can upgrade, delete, etc.)
- Custodian: Application-level admin (can call admin functions but cannot modify canister)
Troubleshooting
Bot Registration Failed
Symptom:InsufficientCyclesProvided error
Solution:
Out of Cycles
Symptom: Canister stops responding Solution:Cannot Join Group
Symptom:#NotAuthorized or #GroupNotFound
Solution:
- Verify bot is initialized
- Check if group requires invite code
- Ensure bot hasn’t been blocked
Upgrade Lost State
Symptom: Data missing after upgrade Solution:- Always mark persistent data as
stable - Test upgrades on local replica first
- Consider implementing state export/import functions
Canister ID Management
Canister IDs are stored incanister_ids.json:
This file is automatically generated after deployment and should be committed to version control for production deployments.