feat(stablecoin): add collateralization-check helper - #336
Open
gravityblast wants to merge 1 commit into
Open
Conversation
gravityblast
marked this pull request as draft
August 27, 2026 14:32
gravityblast
marked this pull request as ready for review
August 27, 2026 14:34
There was a problem hiding this comment.
Pull request overview
Adds a reusable, spec-aligned collateralization invariant check to stablecoin_program so upcoming position-lifecycle instructions can share one canonical implementation and test suite.
Changes:
- Introduces
stablecoin_program::checks::assert_position_is_collateralized, implementing the §6.2 invariant usingU256intermediates and a zero-debt fast-path. - Adds focused unit tests covering boundary, near-boundary, and accumulator-growth scenarios.
- Wires the new module into the crate exports and adds the required direct dependency.
Reviewed changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| programs/stablecoin/src/lib.rs | Exposes the new checks module from stablecoin_program. |
| programs/stablecoin/src/checks.rs | Implements assert_position_is_collateralized and adds unit tests for key invariant edges. |
| programs/stablecoin/Cargo.toml | Adds alloy-primitives as a direct dependency for U256 use in stablecoin_program. |
| Cargo.lock | Records the new stablecoin_program -> alloy-primitives dependency in the workspace lockfile. |
| programs/stablecoin/methods/guest/Cargo.lock | Updates the guest lockfile to reflect the new dependency in guest builds. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
stablecoin_program::checks::assert_position_is_collateralized, the sharedspec §6.2 invariant used by
withdraw_collateral(post-decrement) andgenerate_debt(post-mint).Pure function over scalars — the callers project the accumulator and redemption
price forward to
now(§5.3) and pass them in, so the projection isn't computedtwice per instruction. Uses the cross-multiplied form the spec prescribes, in
U256, so no intermediate rounding enters the comparison. Zero-debt positionsshort-circuit to pass.
8 unit tests: zero debt with and without collateral, comfortable surplus, the
exact boundary and one unit below it, exactly 1.5x and one unit below it, and
accumulator growth flipping a passing position to failing.
First of eight issues in Plan 3 (#173). No
Instructionvariant and no guestchange, so the IDL is untouched.
closes #174