Skip to content

feat(payment-streaming): per-second vesting, cancel settlement, and token escrow - #1987

Open
dhikbass wants to merge 1 commit into
StellarDevHub:mainfrom
dhikbass:feat/payment-streaming-1867
Open

dhikbass wants to merge 1 commit into
StellarDevHub:mainfrom
dhikbass:feat/payment-streaming-1867

Conversation

@dhikbass

Copy link
Copy Markdown

Summary

The payment streaming contract did not meet SC-070. Its lib.rs used a period-based pull model (whole period_length ledgers, floor division) with no token transfers and no balance. Its test.rs was not declared as a module and called an API that lib.rs did not provide, so it was never compiled.

This PR rewrites the contract's accrual, cancellation, and escrow logic so the three acceptance criteria hold:

  • Accrual is per second: V = rate_per_second * (now - start_time), clamped to [0, deposit].
  • Funds move through the Soroban token interface. create_stream escrows the deposit, and withdraw and cancel_stream transfer tokens out.
  • cancel_stream pays the accrued, unwithdrawn amount to the recipient and refunds the unvested remainder to the sender, in one call.
  • The contract's token balance equals the sum of deposit - withdrawn over Active streams after every operation. A test helper checks this in a multi-stream scenario.
  • Arithmetic is checked. Vesting uses exact integer math, with no rounding. The rounding note is in vested_at.
  • src/test.rs is wired in with #[cfg(test)] mod test; and replaces the inline tests.

No dependencies, lockfiles, or other contracts were changed. soroban-sdk stays at 28.0.0.

What breaks if this is wrong

  • API change: create_stream now takes a token address and a rate, and returns a u32 stream id. pull_payment is removed, and cancel_stream takes a stream id and returns the refund. Any caller of the old signatures will fail to compile or call. A repo search found no Rust callers outside the contract. The references are in docs and the contracts workspace manifest.
  • Funding: the sender must hold at least rate_per_second * (stop_time - start_time) of the token. The transfer happens inside create_stream, so a short balance reverts the whole call.
  • Cancellation: a cancelled stream holds no balance. If the recipient was owed tokens, they are paid at cancel time, not left for a later withdrawal.
  • Storage: streams live in instance storage, so the entry grows with the number of streams. A production deployment should move streams to persistent storage with TTL management. That is out of scope here.
  • Auth: the token transfer from the sender inside create_stream depends on the sender's authorization covering the token sub-invocation. This is standard for Soroban but has not been exercised against a real network.

Criteria status

# Criterion Before this PR Added here Tested
1 Recipient withdraws accrued tokens linearly per second, V = rate * (now - start) Neither. Period-based floor division (old lib.rs pull_payment/claimable). Yes: vested_at, withdraw, claimable in lib.rs. Yes: test_vesting_is_linear_per_second, test_withdraw_pays_accrued_amount, test_full_withdrawal_exhausts_stream.
2 Cancel returns 100% of unvested tokens to sender and vests accrued tokens to recipient Partial. Old cancel_stream computed a refund but never paid the accrued amount, so it was stranded. Old test checked only the refund. Yes: cancel_stream transfers accrued to recipient and unvested to sender. Yes: test_cancel_mid_stream_vests_accrued_and_refunds_unvested, test_cancel_after_partial_withdrawal_pays_remaining_accrued, test_cancel_before_start_refunds_everything, test_cancel_after_full_vesting_refunds_nothing.
3 Contract balance equals the sum of unwithdrawn active stream balances at all times Neither. No token balance existed and no check was made. Yes: assert_invariant in test.rs sums deposit - withdrawn over Active streams and compares it with the token balance. It is test-only and loops over stream_count(), so it is not an on-chain sum. Yes: test_invariant_holds_across_multi_stream_scenario, which calls the check after each step.

Failure paths, all in test.rs: withdraw before start (test_withdraw_before_start_fails), non-recipient withdraw (test_non_recipient_cannot_withdraw), non-sender cancel (test_non_sender_cannot_cancel), withdraw after cancel (test_withdraw_after_cancel_fails), double cancel with no second payout (test_double_cancel_fails_and_pays_nothing_twice), deposit overflow (test_overflow_in_deposit_is_rejected), and non-positive rate (test_non_positive_rate_is_rejected).

Verification

Verified by reading only. Nothing was compiled or run. Each signature was checked against its call sites, and the imports were checked against the soroban-sdk 28.0.0 API as used in this crate.

APIs the reviewer should confirm at compile time: Env::register_stellar_asset_contract_v2 and StellarAssetContract::address() (testutils), soroban_sdk::token::{TokenClient, StellarAssetClient}, the generated try_* client methods, and the TokenClient::transfer and mint argument forms (references).

Commands that should run, not yet run:

cargo test -p payment_streaming
cargo clippy -p payment_streaming -- -D warnings

Closes #1867

…nd token escrow

- Replace the period-based pull model with per-second streams. Accrual is
  V = rate_per_second * (now - start_time), clamped to [0, deposit], using
  ledger timestamp seconds.
  Reason: SC-070 requires linear per-second accrual.
  Consequence: pull_payment, period_length, and amount_per_period are removed.
- Move funds through the Soroban token interface. create_stream escrows the
  deposit from the sender; withdraw and cancel_stream transfer tokens out.
  Reason: the balance invariant (contract balance equals the sum of unwithdrawn
  active stream balances) needs a real token balance to check.
  Consequence: create_stream takes a token address and the sender must hold the
  deposit.
- Settle cancellation in one call: the accrued, unwithdrawn amount goes to the
  recipient and the unvested remainder goes back to the sender.
  Reason: the previous code computed a refund but neither paid the accrued
  amount nor left it pullable, so those tokens were stranded.
  Consequence: a cancelled stream holds zero balance and cannot be withdrawn from.
- Support multiple streams keyed by u32 id, with get_stream, stream_count,
  vested, and claimable views.
  Reason: the invariant is defined over the sum of several streams.
  Consequence: streams are kept in instance storage, so storage grows with the
  number of streams.
- Use checked arithmetic for the deposit, vesting, and transfer amounts. An
  overflow aborts the call. Vesting uses exact integer multiplication with no
  rounding; the rounding direction is documented in vested_at.
- Replace the inline tests with src/test.rs, wired in with `mod test`. The tests
  cover vesting, cancellation, the balance invariant across a multi-stream
  scenario, and the failure paths.
  Reason: the previous src/test.rs was never compiled and targeted an API that
  lib.rs did not provide.
  Consequence: src/test.rs is the single test module; tests use the stellar
  asset contract from soroban-sdk testutils, with no new dependencies.
@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@dhikbass is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Nov 25, 2026

Copy link
Copy Markdown

@dhikbass Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

This branch has not been deployed

No deployments
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.

[SC-070] Payment Streaming Protocol with Per-Second Vesting and Cancellation Escrow

1 participant