feat(stablecoin): Implement stability fee accrual#198
Open
3esmit wants to merge 7 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Implements the stability-fee accrual and normalized-debt accounting model for the stablecoin program, adding the required global singleton accounts and new instructions described in docs/stablecoin/README.md.
Changes:
- Introduces protocol globals (
ProtocolParameters,StabilityFeeAccumulator,RedemptionPriceState) plus new instructions to initialize the protocol, accrue fees, and update the fee rate. - Migrates positions to
normalized_debt_amountwith projected nominal debt via the global accumulator; updatesopen_position,generate_debt,repay_debt, andwithdraw_collateralaccordingly. - Extends unit + integration tests and regenerates the stablecoin IDL to match the new instruction/account surfaces.
Reviewed changes
Copilot reviewed 16 out of 18 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| programs/stablecoin/src/withdraw_collateral.rs | Adds protocol/global reads + collateralization check using projected debt/redemption price before allowing withdrawal. |
| programs/stablecoin/src/tests.rs | Updates/extends unit tests for new globals, accrual, debt generation/repayment, and collateralization behavior. |
| programs/stablecoin/src/shared.rs | Adds shared helpers for reading canonical clock + protocol singleton PDAs and accruing fee state. |
| programs/stablecoin/src/set_stability_fee_per_millisecond.rs | Adds admin setter that auto-accrues fees before changing the per-ms rate. |
| programs/stablecoin/src/repay_debt.rs | Converts repayment to normalized-debt decrement using current accumulator and adds repay bounds. |
| programs/stablecoin/src/open_position.rs | Switches position PDA to (owner, nonce) and records new Position fields including opened_at. |
| programs/stablecoin/src/lib.rs | Registers new instruction modules and shared helpers. |
| programs/stablecoin/src/initialize_program.rs | Adds bootstrap instruction to create protocol singletons and stablecoin definition via chained token call. |
| programs/stablecoin/src/generate_debt.rs | Adds debt minting instruction with oracle staleness gating + collateralization check. |
| programs/stablecoin/src/accrue_stability_fee.rs | Adds permissionless fee accrual instruction to roll the global accumulator forward. |
| programs/stablecoin/methods/guest/src/bin/stablecoin.rs | Exposes new instructions through the guest entrypoint interface. |
| programs/stablecoin/methods/guest/Cargo.lock | Adds/updates deps for new guest-side interfaces. |
| programs/stablecoin/core/src/lib.rs | Adds new core account types, math helpers (compound/mul-div), PDA helpers, and instruction enum variants. |
| programs/stablecoin/core/Cargo.toml | Adds alloy-primitives/ruint for U256 math (with MSRV pin rationale). |
| programs/stablecoin/Cargo.toml | Adds clock_core, borsh, and oracle-core dependency wiring for host program. |
| programs/integration_tests/tests/stablecoin.rs | Updates integration flows to include initialization, accrual, generate/repay, and collateralization enforcement. |
| Cargo.lock | Lockfile updates reflecting new dependencies. |
| artifacts/stablecoin-idl.json | Regenerates IDL for new accounts/types/instructions and updated instruction interfaces. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
15df21e to
0d863b3
Compare
5a73431 to
416a03f
Compare
406f53c to
f5e831a
Compare
f5e831a to
e4aa722
Compare
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.
Closes #97 (Replaces #120)
Summary
Implements stability-fee accrual for the stablecoin program, following
docs/stablecoin/README.md.Positions now store
normalized_debt_amount. A globalStabilityFeeAccumulatorprojects current debt without rewriting each position.Changes
Position.collateralization checks.
initialize_program,accrue_stability_fee,set_stability_fee_per_millisecond, andgenerate_debt.repay_debtandwithdraw_collateralto use normalized debt and thecurrent accumulator.
(owner_account_id, position_nonce).artifacts/stablecoin-idl.json.trusted token program binding:
Scope
This PR implements the stability-fee and debt-accounting slice of
docs/stablecoin/README.md.The README also defines additional end-state instructions such as
deposit_collateral,close_position, redemption-rate updates, broader adminsetters, and freeze authority operations. Those remain separate follow-up work.
MAXIMUM_COMPOUNDING_WINDOW_MILLISECONDSis set to one day for the firstimplementation. This bounds fixed-point compounding if keepers stop calling
accrual for a long period.
Current spec has no
token_program_idconfig or known token ID input: recommend a follow up analysis if this cause any issues.