Skip to content

docs: add STREAM_MATH.md with worked examples and edge cases (#791) - #831

Open
tilljack60-cmd wants to merge 1 commit into
ritik4ever:mainfrom
tilljack60-cmd:docs/stream-math-worked-examples
Open

docs: add STREAM_MATH.md with worked examples and edge cases (#791)#831
tilljack60-cmd wants to merge 1 commit into
ritik4ever:mainfrom
tilljack60-cmd:docs/stream-math-worked-examples

Conversation

@tilljack60-cmd

@tilljack60-cmd tilljack60-cmd commented Jul 29, 2026

Copy link
Copy Markdown

Creates docs/STREAM_MATH.md as the authoritative, standalone reference for StellarStream's vesting math, expanding the brief formula summary
in README § 3 into a full document.

Changes

  • docs/STREAM_MATH.md (new) — 466 lines covering:

    • Core symbol/variable definitions table

    • Formula derivations explaining the clamp, vesting ratio, and cancel accounting

    • Two worked examples with step-by-step arithmetic:

      • 1 000 USDC over 1 year, claim at 6 months
      • Same stream, cancel at 9 months (after a prior 6-month claim), with accounting check that all token splits sum to A_total
    • Five edge cases: claim before start, claim after end, cliff stream (with its own mini-example), instant cancel at t_start, zero-duration
      guard

    • Status rules reference table (scheduled / active / completed / canceled)

    • Implementation cross-reference mapping each math concept to its source file

  • README.md — adds > Full derivations, worked examples, and edge cases callout in § 3 pointing to the new doc

  • docs/CONTRACT_BINDINGS.md — adds a callout at the top linking to STREAM_MATH.md for the math behind claimable(), claim(), and cancel()

Acceptance criteria checklist

  • Examples include explicit input values and expected outputs
  • All specified edge cases covered (before start, after end, cliff stream)
  • Linked from README section 3
  • Linked from docs/CONTRACT_BINDINGS.md (the contract ABI reference in this repo)

Notes

CONTRACT_ABI.md referenced in the issue does not exist in this repo — docs/CONTRACT_BINDINGS.md is the equivalent file and has been updated
instead. The cliff stream section is documented as planned since the feature is not yet in the MVP contract.

closes #791

Summary by CodeRabbit

  • Documentation
    • Added a comprehensive Stream Math Reference covering vesting calculations, claiming, cancellation, status rules, edge cases, and worked examples.
    • Added links from existing documentation to the new math reference for easier access to detailed accounting explanations.

…ver#791)

- Create docs/STREAM_MATH.md with full formula derivations, symbol table,
  two worked examples (1000 USDC 6-month claim, 9-month cancel), and five
  edge cases (before start, after end, cliff stream, instant cancel,
  zero-duration guard)
- Add status rules reference table and implementation cross-reference
- Link STREAM_MATH.md from README section 3
- Link STREAM_MATH.md from docs/CONTRACT_BINDINGS.md
@vercel

vercel Bot commented Jul 29, 2026

Copy link
Copy Markdown

@tilljack60-cmd is attempting to deploy a commit to the ritik4ever's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Jul 29, 2026

Copy link
Copy Markdown

@tilljack60-cmd 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

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds docs/STREAM_MATH.md as a detailed reference for stream vesting formulas, examples, edge cases, status rules, and implementation references. Links to it from the README and contract bindings documentation.

Changes

Stream math documentation

Layer / File(s) Summary
Math definitions and worked examples
docs/STREAM_MATH.md
Defines vesting variables and formulas, with numerical examples for mid-stream claims and cancellation after partial claims.
Edge cases and status rules
docs/STREAM_MATH.md
Documents pre-start, post-end, cliff, instant-cancel, zero-duration, and status behavior.
Documentation navigation and cross-reference
README.md, docs/CONTRACT_BINDINGS.md, docs/STREAM_MATH.md
Links the reference from existing documentation and maps formulas to backend and contract locations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers: emmzyemms

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the new STREAM_MATH.md docs and examples.
Linked Issues check ✅ Passed The new doc, worked examples, edge cases, and README/contract links satisfy issue #791.
Out of Scope Changes check ✅ Passed The changes are limited to documentation and cross-links, with no clear out-of-scope additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Fix failing CI checks
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/STREAM_MATH.md`:
- Around line 458-460: Correct the `R` and `A_vested` row in
`docs/STREAM_MATH.md`: update the `claimable()` return-value reference to point
to the actual internal vesting calculation in `contracts/src/lib.rs`, or
describe these values as intermediate state rather than returned values. Leave
the other cross-reference rows unchanged.
- Around line 441-446: Update the status table in STREAM_MATH.md so the
active-state note uses R ∈ [0, 1) to include t = t_start, and explicitly state
that canceled takes precedence over the scheduled, active, and completed
time-based statuses, including when t ≥ t_end.
- Around line 419-420: Update the division-by-zero failure description for the
integer-arithmetic formula R = Δt / d in STREAM_MATH.md to remove NaN and state
only the applicable behavior: a runtime trap or panic. Preserve the surrounding
explanation and formula.
- Around line 40-42: Update the A_claimable definition in docs/STREAM_MATH.md,
including the corresponding repeated definition, to document the contract’s
zero-floor behavior using max(0, A_vested - A_claimed) rather than direct
subtraction. Keep the existing meaning that the result is withdrawable
immediately.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c2b641bc-a7c5-4b68-90a8-397e14c419d3

📥 Commits

Reviewing files that changed from the base of the PR and between b3d32c1 and a5a155e.

📒 Files selected for processing (3)
  • README.md
  • docs/CONTRACT_BINDINGS.md
  • docs/STREAM_MATH.md

Comment thread docs/STREAM_MATH.md
Comment on lines +40 to +42
| $A_{claimed}$ | number | Tokens the recipient has already withdrawn |
| $A_{claimable}$ | number | $A_{vested} - A_{claimed}$ — withdrawable right now |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Document the contract’s zero-floor for claimable amounts.

contracts/src/lib.rs:382-387 returns max(0, vested - claimed), while this reference defines direct subtraction. Use the clamped formula consistently so the authoritative document matches on-chain behavior.

Proposed wording
-| $A_{claimable}$ | number | $A_{vested} - A_{claimed}$ — withdrawable right now |
+| $A_{claimable}$ | number | $\max(0, A_{vested} - A_{claimed})$ — withdrawable right now |

-$A_{claimable} = A_{vested} - A_{claimed}$
+$A_{claimable} = \max(0, A_{vested} - A_{claimed})

Also applies to: 80-84

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/STREAM_MATH.md` around lines 40 - 42, Update the A_claimable definition
in docs/STREAM_MATH.md, including the corresponding repeated definition, to
document the contract’s zero-floor behavior using max(0, A_vested - A_claimed)
rather than direct subtraction. Keep the existing meaning that the result is
withdrawable immediately.

Comment thread docs/STREAM_MATH.md
Comment on lines +419 to +420
Division by zero in $R = \Delta t / d$ would produce `NaN` or a runtime
panic.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Remove NaN from the integer-arithmetic failure description.

The contract stores amounts as i128; division by zero would trap/panic, not produce NaN. Describe the applicable integer failure mode.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/STREAM_MATH.md` around lines 419 - 420, Update the division-by-zero
failure description for the integer-arithmetic formula R = Δt / d in
STREAM_MATH.md to remove NaN and state only the applicable behavior: a runtime
trap or panic. Preserve the surrounding explanation and formula.

Comment thread docs/STREAM_MATH.md
Comment on lines +441 to +446
| Status | Condition | Notes |
|---|---|---|
| `scheduled` | $t < t_{start}$ | Stream exists but vesting has not started |
| `active` | $t_{start} \le t < t_{end}$ | Vesting in progress; $R \in (0, 1)$ |
| `completed` | $t \ge t_{end}$ | Full amount vested; $R = 1$ |
| `canceled` | Explicit `cancel()` call | Frozen at $A_{vested\_at\_cancel}$; no further vesting |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Clarify status boundaries and cancellation precedence.

active includes t = t_start, where R = 0, so the note should use R ∈ [0, 1). Also, a canceled stream can satisfy t ≥ t_end; explicitly state that canceled takes precedence over time-based statuses.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/STREAM_MATH.md` around lines 441 - 446, Update the status table in
STREAM_MATH.md so the active-state note uses R ∈ [0, 1) to include t = t_start,
and explicitly state that canceled takes precedence over the scheduled, active,
and completed time-based statuses, including when t ≥ t_end.

Comment thread docs/STREAM_MATH.md
Comment on lines +458 to +460
| `Δt` clamp | `backend/src/services/streamStore.ts` | `contracts/src/lib.rs` → `claimable()` |
| `R` and `A_vested` | `streamStore.ts` `computeProgress()` | `claimable()` return value |
| `A_claimable` | `streamStore.ts` `computeProgress()` | `claimable()` |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Correct the contract cross-reference for R and A_vested.

contracts/src/lib.rs:382-387 shows that claimable() computes intermediate vesting state but returns only the final claimable i128; it does not return R or A_vested. Point this row to the actual internal vesting calculation, or describe those values as intermediate rather than return values.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/STREAM_MATH.md` around lines 458 - 460, Correct the `R` and `A_vested`
row in `docs/STREAM_MATH.md`: update the `claimable()` return-value reference to
point to the actual internal vesting calculation in `contracts/src/lib.rs`, or
describe these values as intermediate state rather than returned values. Leave
the other cross-reference rows unchanged.

@ritik4ever

Copy link
Copy Markdown
Owner

Hi @tilljack60-cmd,

This PR could not be merged because it has merge conflicts with the target branch.

Please resolve the merge conflicts, push the updated changes, and the PR can be reviewed and merged.

Thank you!

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.

[FEATURE] Add comprehensive stream math worked examples to docs

2 participants