feat(stablecoin): rebuild repay_debt with floor-rounded debt deltas - #353
Open
gravityblast wants to merge 1 commit into
Open
feat(stablecoin): rebuild repay_debt with floor-rounded debt deltas#353gravityblast wants to merge 1 commit into
gravityblast wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
There are correctness/documentation gaps in the updated repay_debt documentation/tests and an error message mismatch that should be aligned with the spec/acceptance criteria for clarity and consistency.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR rebuilds the stablecoin program’s repay_debt instruction to be fee-aware per spec §10.8 by projecting the stability fee accumulator to the provided clock timestamp, computing a floor-rounded normalized-debt delta, and pinning the stablecoin definition against ProtocolParameters.
Changes:
- Update
repay_debthost logic to computedebt_delta = ⌊amount × FIXED_POINT_ONE / current_accumulator⌋(projected toclock) and validatestablecoin_definitionviaProtocolParameters. - Expand the instruction ABI from 4 → 7 accounts across host + guest wiring (adds accumulator, protocol parameters, clock).
- Add/adjust unit + integration tests and regenerate the stablecoin IDL to match the new account list.
File summaries
| File | Description |
|---|---|
| programs/stablecoin/src/repay_debt.rs | Implements fee-aware, floor-rounded debt delta with protocol-parameters binding and clock-based accumulator projection. |
| programs/stablecoin/methods/guest/src/bin/stablecoin.rs | Updates guest entrypoint signature to pass the three new accounts through to the host function. |
| programs/stablecoin/core/src/lib.rs | Updates Instruction::RepayDebt documentation to reflect the new account list and semantics. |
| programs/stablecoin/src/tests.rs | Adds new unit tests for §10.8 behavior and updates existing repay tests for the new ABI. |
| programs/integration_tests/tests/stablecoin.rs | Seeds the new global accounts and updates the integration repay test to pass the new accounts. |
| artifacts/stablecoin-idl.json | Regenerates IDL to include the three new accounts for repay_debt. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1223
to
+1225
| assert_eq!(post_states.len(), 7); | ||
| assert_eq!(*post_states[6].account(), clock_account(NOW).account); | ||
| } |
Comment on lines
14
to
17
| /// Burns `amount` stablecoins from `user_stablecoin_holding` via a chained | ||
| /// `Token::Burn` and decreases `Position.normalized_debt_amount` by the same | ||
| /// amount. The position post-state uses plain [`AccountPostState::new`] — the | ||
| /// PDA was already claimed at `open_position` time. |
Comment on lines
146
to
149
| let new_debt = position_data | ||
| .normalized_debt_amount | ||
| .checked_sub(amount) | ||
| .checked_sub(debt_delta) | ||
| .expect("Repay amount exceeds outstanding debt"); |
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.
Rebuilds
repay_debtto spec §10.8. The Plan 1 scaffold subtractedamountdirectly from
normalized_debt_amountand trusted whateverstablecoin_definitionthe caller passed.Accounts go 4 → 7, adding
stability_fee_accumulator,protocol_parametersandclock. The decrement is now⌊amount × FIXED_POINT_ONE / current_accumulator⌋,rounded down per §6.3 against the accumulator projected to the clock
timestamp — debt shrinks by at most what was burned, and the rounding remainder
stays with the protocol.
stablecoin_definitionis pinned againstProtocolParameters.stablecoin_definition_id, closing the gap the scaffold's// TODOcalled out.Allowed while frozen:
protocol_parametersis read for the accumulator rate andthe definition binding, never for
is_frozen. Repaying only improves theprotocol's position (§7).
7 new tests: the three new accounts echoed, the floor rounding at accumulator 3.0
(100 → 33), repay while frozen, an unbound stablecoin definition, overrepay
against the floored delta, and an uninitialized variant of each new global.
Sixth of eight issues in Plan 3 (#173). Stacked on #352.
closes #179