Upgrade Basics
When you publish a package on Sui, you receive anUpgradeCap that allows you to publish new versions.
Publishing with Upgrade Capability
- Published package at address
0xPACKAGE_ID UpgradeCapobject sent to publisher
Upgrade Policies
Sui enforces three upgrade policies:-
Compatible: Most restrictive, safest
- Can only add new functions and structs
- Cannot modify existing public functions
- Cannot change struct definitions
-
Additive: Middle ground
- Can add abilities to structs
- Can change private function signatures
- Can add new public functions
-
Dependency-only: Least restrictive
- Can make breaking changes
- Only affects modules that depend on this package
Upgrading a Package
Step 1: Modify your code
Update your Move code with new features:Step 2: Build the upgrade
Step 3: Authorize upgrade
Publish the new version:Compatible Upgrades
Safe changes that don’t break existing code:✅ Adding new functions
✅ Adding new structs
✅ Adding private functions
Incompatible Changes
Changes that break compatibility:❌ Changing public function signatures
❌ Removing public functions
❌ Modifying struct definitions
Migration Strategies
Strategy 1: Additive Only
Add new functionality without changing existing code:Strategy 2: Wrapper Pattern
Wrap old structs in new versions:Strategy 3: Versioned Modules
Create separate modules for major versions:Using Dynamic Fields for Upgrades
Add data to existing objects without changing struct:Managing UpgradeCap
Check upgrade capability
Transfer upgrade rights
Make package immutable
Burn the UpgradeCap to prevent future upgrades:Testing Upgrades
Test upgrade compatibility
Simulate upgrade locally
- Publish v1 on localnet
- Make changes
- Test upgrade
- Verify old objects still work
- Test new functionality
Best Practices
1. Plan for upgrades from the start
2. Document breaking changes
3. Provide migration paths
4. Use semantic versioning
5. Test thoroughly before upgrading
- Test on devnet first
- Verify all existing functionality works
- Test new features
- Check gas costs
Common Upgrade Patterns
Adding optional features
Deprecating old functions
Supporting multiple versions
Troubleshooting
Upgrade fails with compatibility error
Check what changed:Lost UpgradeCap
If you lose the UpgradeCap, you cannot upgrade. Options:- Deploy as new package
- Implement migration functions in v1