Skip to content

feat: soroban stream lifecycle implementation#173

Merged
ogazboiz merged 4 commits into
LabsCrypt:mainfrom
0xVida:main
Feb 24, 2026
Merged

feat: soroban stream lifecycle implementation#173
ogazboiz merged 4 commits into
LabsCrypt:mainfrom
0xVida:main

Conversation

@0xVida
Copy link
Copy Markdown
Contributor

@0xVida 0xVida commented Feb 24, 2026

Description

Refactors the stream withdrawal logic to use a dedicated calculate_claimable helper 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

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧪 Test addition or update

Related Issues

Closes #123

Changes Made

Core Changes

  • Added calculate_claimable helper function: Extracts claimable amount calculation logic into a reusable, safer implementation

    • Uses saturating_sub to prevent underflow when calculating elapsed time
    • Uses checked_mul with unwrap_or(i128::MAX) to prevent overflow in multiplication
    • Properly caps claimable amount at remaining balance to prevent over-withdrawal
  • Refactored withdraw function:

    • Now uses calculate_claimable helper instead of inline calculation
    • Properly updates last_update_time with the current timestamp
    • Automatically marks stream as inactive when all funds are withdrawn
    • Maintains backward compatibility with existing behavior
  • Improved cancel_stream function:

    • Already implements proper refund calculation based on accrued amounts
    • Ensures accrued tokens belong to recipient, remaining balance refunded to sender

Code Quality Improvements

  • Fixed clippy warning: Replaced match with .unwrap_or() for cleaner code
  • Fixed unused variable warnings in tests by prefixing with _
  • Improved code organization and maintainability

Testing

Test Coverage

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed

Test Steps

  1. Run cargo test in contracts/stream_contract to verify all existing tests pass
  2. Verify test_withdraw_time_based_calculation tests the new helper function
  3. Verify test_withdraw_caps_at_remaining_balance ensures proper capping logic
  4. Verify test_cancel_stream_after_partial_withdrawal tests refund calculations
  5. Check that clippy warnings are resolved: cargo clippy

Existing Tests

All existing tests continue to pass, confirming backward compatibility:

  • test_withdraw_authorized_recipient_receives_tokens
  • test_withdraw_time_based_calculation
  • test_withdraw_caps_at_remaining_balance
  • test_cancel_stream_refunds_sender
  • test_cancel_stream_after_partial_withdrawal
  • test_top_up_preserves_accrued_amount

Breaking Changes

No breaking changes - This is a refactoring that maintains full backward compatibility. The behavior of withdraw and cancel_stream functions remains identical, with improved internal implementation.

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published
  • I have checked for breaking changes and documented them if applicable

Additional Notes

Technical Details

  • The calculate_claimable function uses safe arithmetic operations to prevent integer overflow/underflow
  • The function properly handles edge cases where elapsed time or rate calculations might overflow
  • Stream lifecycle management is now more robust with automatic inactive state marking
  • All changes maintain the existing API surface and behavior

Performance

  • No performance regression expected
  • Helper function may provide slight performance improvement due to better code organization
  • Safe arithmetic operations have minimal overhead

Security Considerations

  • Overflow protection through checked_mul and saturating_sub prevents potential exploits
  • Proper time-based calculations ensure accurate claimable amounts
  • No changes to authorization or access control logic

@ogazboiz
Copy link
Copy Markdown
Contributor

looks great, merging this in! thanks for the contribution

@ogazboiz ogazboiz merged commit 1c9de98 into LabsCrypt:main Feb 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Epic: Soroban stream lifecycle implementation

2 participants