Skip to content

feat(stablecoin): rebuild repay_debt with floor-rounded debt deltas - #353

Open
gravityblast wants to merge 1 commit into
feat/stablecoin-generate-debtfrom
feat/stablecoin-repay-debt-rebuild
Open

feat(stablecoin): rebuild repay_debt with floor-rounded debt deltas#353
gravityblast wants to merge 1 commit into
feat/stablecoin-generate-debtfrom
feat/stablecoin-repay-debt-rebuild

Conversation

@gravityblast

Copy link
Copy Markdown
Collaborator

Rebuilds repay_debt to spec §10.8. The Plan 1 scaffold subtracted amount
directly from normalized_debt_amount and trusted whatever
stablecoin_definition the caller passed.

Accounts go 4 → 7, adding stability_fee_accumulator, protocol_parameters and
clock. 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_definition is pinned against
ProtocolParameters.stablecoin_definition_id, closing the gap the scaffold's
// TODO called out.

Allowed while frozen: protocol_parameters is read for the accumulator rate and
the definition binding, never for is_frozen. Repaying only improves the
protocol'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

Copilot AI 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.

🟡 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_debt host logic to compute debt_delta = ⌊amount × FIXED_POINT_ONE / current_accumulator⌋ (projected to clock) and validate stablecoin_definition via ProtocolParameters.
  • 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");
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.

2 participants