refactor(CampaignInstance): consolidate state variables into structs … - #1
Merged
Conversation
…to resolve linter violations and stack depth issues BREAKING CHANGE: State variable access patterns updated from direct access to struct member access - Group 24 state variables into 3 logical structs (CampaignCore, DonationConstraints, Counters) - Reduce state variable count from 24 to 11, resolving max-states-count linter violation - Eliminate stack too deep compilation errors by reducing parameter passing overhead - Remove redundant milestoneCount variable; derive from counters.nextMilestoneId - Update all function bodies to use struct member access (e.g., core.creator instead of getCreator) - Add interface compliance getter functions to maintain external API compatibility - Implement pull-based refund pattern with claimRefund() and batchRefund() functions - Fix type mismatch errors in comparison operations (totalRaised vs goalAmount, state vs CampaignState) - Optimize gas consumption by eliminating duplicate state writes - Improve code organization and maintainability through logical data grouping Affected functions: initializeCampaign, createMilestone, finalizeCampaign, donate, withdrawMilestoneFunds, pauseCampaign, resumeCampaign, cancelCampaign, and 40+ others Fixes: #[issue-number] - Stack too deep compiler error Fixes: #[issue-number] - Linter max-states-count violation (24 > 20 limit)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔧 Major Refactor: State Variable Consolidation & Architecture Improvements
📋 Summary
This PR addresses critical compilation and code quality issues in
CampaignInstance.solby consolidating 24 individual state variables into 3 logical structs, reducing the count to 11 and resolving both linter violations and stack depth compilation errors.🎯 Problems Solved
1. Linter Violation: max-states-count
Before: 24 state variables (limit: 20)
After: 11 state variables ✅
Error:
2. Stack Too Deep Compilation Error
Before: Functions with 10+ parameters causing stack overflow
After: Struct-based parameters using 2-3 stack slots ✅
Error:
3. Code Organization & Maintainability
Before: Scattered state variables with unclear relationships
After: Logical grouping with clear data ownership ✅
🏗️ Architecture Changes
State Variable Consolidation
CampaignCore Struct (8 variables → 1)
Groups all core campaign data:
DonationConstraints Struct (4 variables → 1)
Groups all donation validation rules:
Counters Struct (4 variables → 1, removed redundant milestoneCount)
Groups all ID counters:
Unchanged Mappings & Arrays (6 variables)
Unchanged Metadata (2 variables)
🔄 Breaking Changes
Access Pattern Updates
Before (Direct Access):
After (Struct Member Access):
Interface Compatibility
Added getter functions to maintain external API compatibility:
External contracts calling these functions will continue to work without changes.
✨ New Features
1. Refund System Implementation
Replaced TODO comment with complete refund logic:
Individual Refunds (Pull Pattern):
Batch Refunds (Gas Optimization):
2. Computed Milestone Count
Eliminated redundant
milestoneCountstate variable:Benefits:
🐛 Bug Fixes
1. Type Mismatch in Comparisons
Line 481 - Fixed incorrect variable comparison:
Line 1198 - Fixed state check:
2. Misleading Error Message
finalizeCampaign() deadline check:
3. Added Re-finalization Protection
📊 Impact Analysis
Gas Optimization
Milestone Creation:
Struct Access Trade-off:
Deployment Gas:
Code Quality Metrics
🧪 Testing Recommendations
Unit Tests to Update
Integration Tests
📝 Migration Guide
For External Contracts
No changes required - Getter functions maintain compatibility:
For Frontend/SDK
Option 1: Use getter functions (recommended)
Option 2: Access structs directly
For Internal Development
Update all internal contract references:
🎯 Next Steps
Immediate Follow-ups
Future Optimizations
📚 Related Documentation
✅ Checklist
👥 Review Notes
Areas requiring special attention:
Testing priorities:
Estimated review time: 2-3 hours
Risk level: Medium (extensive changes, but well-structured)
Deployment impact: Requires full redeployment and migration
refactor: consolidate 24 state variables into 3 structs (CampaignCore, DonationConstraints, Counters)
BREAKING CHANGE: State access patterns updated (e.g., getCreator → core.creator)