Skip to content

Implement the settle_stream_amount helper backing settle_stream and batch_settle #136

Description

@greatest0fallt1me

Description

Both settle_stream (src/lib.rs:336) and each iteration of batch_settle (src/lib.rs:359) call settle_stream_amount(&env, stream_id), but that helper is never defined, which is one of the unresolved symbols blocking compilation. The accrual maths is already specified normatively in docs/accrual-spec.md §3–§4 and duplicated inline in cancel_stream/pause_stream/withdraw_stream (src/lib.rs:397399, :430:433, :504:510). This issue centralises that logic in one tested helper that returns Option<i128> so callers can distinguish “inactive → returned 0” from “settled N”.

Requirements and context

Functional

  • Implement fn settle_stream_amount(env: &Env, stream_id: u32) -> Option<i128> that:
    • loads the stream via crate::stream::get_stream;
    • returns None when is_active == false (so settle_stream returns 0, per docs/accrual-spec.md §5 and test_settle_inactive_returns_zero);
    • computes accrued = (elapsed as i128).saturating_mul(rate_per_second).min(balance) exactly as in docs/accrual-spec.md §3.4;
    • applies balance -= accrued, start_time = now, persists via set_stream, bumps TTL via extend_stream_ttl;
    • returns Some(accrued).
  • settle_stream returns 0 for None, else the inner amount; batch_settle pushes 0 for None and the amount otherwise, preserving the all-or-nothing revert behaviour described in src/lib.rs:345349.
  • Reuse this helper inside cancel_stream, pause_stream, update_rate, and withdraw_stream where the same saturating_mul(...).min(balance) block is currently inlined, to remove duplication (coordinate with the StreamInfo-consolidation issue if claimable_balance semantics change).

Non-functional / repo conventions

  • Preserve saturating arithmetic semantics documented at src/lib.rs:314334; never introduce wrapping or panicking arithmetic.
  • Uphold the invariants in docs/accrual-spec.md §7: balance >= 0, accrued <= balance, start_time monotonic.

Acceptance criteria

  • settle_stream_amount implemented; src/lib.rs:336 and :359 resolve and the crate builds.
  • settle_stream returns the accrued amount and 0 for inactive streams.
  • batch_settle returns one element per input id, 0 for inactive, and reverts entirely on a missing id (test_batch_settle_missing_id_reverts_all).
  • Inlined accrual blocks in at least cancel_stream/pause_stream are replaced by the helper without behaviour change.
  • cargo test passes for the existing settle/batch tests; cargo clippy ... -D warnings is clean.

Suggested execution

1. Fork the repo and create a branchgit checkout -b feature/settle-stream-amount-helper.

2. Implement changes — add settle_stream_amount to src/lib.rs (or src/stream.rs if it only needs storage helpers); refactor the duplicated accrual blocks to call it.

3. Write/extend tests — extend inline #[cfg(test)] mod test; add a same-second (elapsed == 0) case from docs/accrual-spec.md §6 Example 3 and a multi-settlement case from Example 4.

4. Test and commit

cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --features testutils
./scripts/check-wasm-size.sh

Example commit message

feat(contract): add settle_stream_amount helper for settle and batch_settle

Guidelines

  • Target 95% line coverage (docs/coverage.sh).
  • Add /// doc-comments referencing docs/accrual-spec.md; if accounting changes, update that spec’s “matches src/lib.rs line-by-line” note.
  • Timeframe: 96 hours.

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions