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:397–399, :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:345–349.
- 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:314–334; never introduce wrapping or panicking arithmetic.
- Uphold the invariants in
docs/accrual-spec.md §7: balance >= 0, accrued <= balance, start_time monotonic.
Acceptance criteria
Suggested execution
1. Fork the repo and create a branch — git 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.
Description
Both
settle_stream(src/lib.rs:336) and each iteration ofbatch_settle(src/lib.rs:359) callsettle_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 indocs/accrual-spec.md§3–§4 and duplicated inline incancel_stream/pause_stream/withdraw_stream(src/lib.rs:397–399,:430–:433,:504–:510). This issue centralises that logic in one tested helper that returnsOption<i128>so callers can distinguish “inactive → returned 0” from “settled N”.Requirements and context
Functional
fn settle_stream_amount(env: &Env, stream_id: u32) -> Option<i128>that:crate::stream::get_stream;Nonewhenis_active == false(sosettle_streamreturns0, perdocs/accrual-spec.md§5 andtest_settle_inactive_returns_zero);accrued = (elapsed as i128).saturating_mul(rate_per_second).min(balance)exactly as indocs/accrual-spec.md§3.4;balance -= accrued,start_time = now, persists viaset_stream, bumps TTL viaextend_stream_ttl;Some(accrued).settle_streamreturns0forNone, else the inner amount;batch_settlepushes0forNoneand the amount otherwise, preserving the all-or-nothing revert behaviour described insrc/lib.rs:345–349.cancel_stream,pause_stream,update_rate, andwithdraw_streamwhere the samesaturating_mul(...).min(balance)block is currently inlined, to remove duplication (coordinate with the StreamInfo-consolidation issue ifclaimable_balancesemantics change).Non-functional / repo conventions
src/lib.rs:314–334; never introduce wrapping or panicking arithmetic.docs/accrual-spec.md§7:balance >= 0,accrued <= balance,start_timemonotonic.Acceptance criteria
settle_stream_amountimplemented;src/lib.rs:336and:359resolve and the crate builds.settle_streamreturns the accrued amount and0for inactive streams.batch_settlereturns one element per input id,0for inactive, and reverts entirely on a missing id (test_batch_settle_missing_id_reverts_all).cancel_stream/pause_streamare replaced by the helper without behaviour change.cargo testpasses for the existing settle/batch tests;cargo clippy ... -D warningsis clean.Suggested execution
1. Fork the repo and create a branch —
git checkout -b feature/settle-stream-amount-helper.2. Implement changes — add
settle_stream_amounttosrc/lib.rs(orsrc/stream.rsif 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 fromdocs/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.shExample commit message
Guidelines
docs/coverage.sh).///doc-comments referencingdocs/accrual-spec.md; if accounting changes, update that spec’s “matches src/lib.rs line-by-line” note.