feat: soroban stream lifecycle implementation#173
Merged
Conversation
Contributor
|
looks great, merging this in! thanks for the contribution |
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
Refactors the stream withdrawal logic to use a dedicated
calculate_claimablehelper function, improving code safety and maintainability. This change implements proper time-based claimable amount calculations with overflow protection and ensures streams are correctly marked as inactive when fully withdrawn.Type of Change
Related Issues
Closes #123
Changes Made
Core Changes
Added
calculate_claimablehelper function: Extracts claimable amount calculation logic into a reusable, safer implementationsaturating_subto prevent underflow when calculating elapsed timechecked_mulwithunwrap_or(i128::MAX)to prevent overflow in multiplicationRefactored
withdrawfunction:calculate_claimablehelper instead of inline calculationlast_update_timewith the current timestampImproved
cancel_streamfunction:Code Quality Improvements
matchwith.unwrap_or()for cleaner code_Testing
Test Coverage
Test Steps
cargo testincontracts/stream_contractto verify all existing tests passtest_withdraw_time_based_calculationtests the new helper functiontest_withdraw_caps_at_remaining_balanceensures proper capping logictest_cancel_stream_after_partial_withdrawaltests refund calculationscargo clippyExisting Tests
All existing tests continue to pass, confirming backward compatibility:
test_withdraw_authorized_recipient_receives_tokenstest_withdraw_time_based_calculationtest_withdraw_caps_at_remaining_balancetest_cancel_stream_refunds_sendertest_cancel_stream_after_partial_withdrawaltest_top_up_preserves_accrued_amountBreaking Changes
No breaking changes - This is a refactoring that maintains full backward compatibility. The behavior of
withdrawandcancel_streamfunctions remains identical, with improved internal implementation.Checklist
Additional Notes
Technical Details
calculate_claimablefunction uses safe arithmetic operations to prevent integer overflow/underflowPerformance
Security Considerations
checked_mulandsaturating_subprevents potential exploits