[REF] Stream Contract: Cleanup and Code Deduplication#217
Merged
ogazboiz merged 2 commits intoLabsCrypt:mainfrom Feb 26, 2026
Merged
Conversation
- Extract validation logic into reusable helper functions (validate_stream_ownership, validate_stream_active) - Add transfer_and_update_stream helper to consolidate token transfer logic - Improve calculate_claimable with better documentation and use .min() for clarity - Add new get_claimable_amount query function for read-only balance checks - Enhance function documentation with detailed parameter and error descriptions - Refactor top_up_stream, withdraw, and cancel_stream to use helper functions - Fix test issues with undefined variables and incorrect contract instances - All 40 tests passing successfully This refactoring reduces code duplication, improves maintainability, and makes the contract more modular while preserving all existing functionality.
Resolved conflicts in contracts/stream_contract/src/lib.rs by: - Keeping validate_token_contract function from main - Preserving all refactored helper functions (validate_stream_ownership, validate_stream_active, transfer_and_update_stream) - Maintaining improved documentation and code deduplication - All 41 tests passing successfully
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.
Description
This pull reqest addresses code quality improvements in the Soroban stream contract by eliminating code duplication and introducing reusable helper functions for better maintainability.
Changes Made
Refactoring & Code Deduplication
New Helper Functions
validate_stream_ownership() - Centralized ownership validation logic
validate_stream_active() - Centralized active status validation
transfer_and_update_stream() - Consolidated token transfer and state update logic
get_claimable_amount() - New public query function for read-only balance checks
Improved Code Quality
Enhanced calculate_claimable() with better documentation and cleaner logic using .min()
Refactored top_up_stream() to use validation helpers
Refactored withdraw() to use validation and transfer helpers
Refactored cancel_stream() to use validation helpers
Added comprehensive documentation to all helper functions
Test Fixes
Fixed undefined variable references in tests
Corrected contract instance usage in test cases
All 40 tests passing successfully ✅
Impact
Lines Changed: +185 / -54
Code Duplication: Reduced significantly
Maintainability: Improved through modular helper functions
Test Coverage: 100% (40/40 tests passing)
Testing
Benefits
Reduced Duplication:
Common validation and transfer logic extracted into reusable functions
Better Maintainability:
Changes to validation or transfer logic now only need to be made in one place
Improved Readability:
Main functions are cleaner and easier to understand
Enhanced Documentation:
All helper functions have detailed parameter and error documentation
New Functionality:
Added get_claimable_amount() for querying balances without state modification
Closes #196