feat(stablecoin): add stability fee accrual#120
Open
3esmit wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds stability-fee accrual plumbing to the Stablecoin program by introducing a program-global fee accumulator state (singleton PDA) and per-position accumulator snapshots, alongside fixed-point continuous compounding helpers to ensure collateralization checks reflect accrued debt.
Changes:
- Added
StabilityFeeStatesingleton PDA initialization flow and required it duringopen_positionso new positions snapshot the global accumulator. - Implemented fixed-point continuous-compounding math + helpers to accrue global fees, accrue positions, and apply debt changes after fee accrual (with saturation/overflow handling).
- Introduced shared
oracle_coreprimitives and amock_oracleprogram + IDLs to standardize oracle price-account shapes for LEZ consumers/tests.
Reviewed changes
Copilot reviewed 29 out of 32 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| stablecoin/src/tests.rs | Extends program tests to cover stability-fee state init and ensures open_position snapshots the global accumulator. |
| stablecoin/src/open_position.rs | Updates open_position to require the initialized global fee-state PDA and snapshot its accumulator into new positions. |
| stablecoin/src/lib.rs | Exposes the new initialize_stability_fee_state module from the stablecoin program crate. |
| stablecoin/src/initialize_stability_fee_state.rs | Adds an instruction implementation that initializes the global stability-fee state PDA (rate + initial timestamp). |
| stablecoin/methods/guest/src/bin/stablecoin.rs | Adds guest entrypoints for the new instruction and extends open_position to accept the fee-state account. |
| stablecoin/methods/guest/Cargo.toml | Adds oracle_core as a path dependency for IDL generation / type availability. |
| stablecoin/methods/guest/Cargo.lock | Updates the guest lockfile to reflect the new dependency graph. |
| stablecoin/core/src/tests.rs | Adds unit tests for fee-growth math, global/position accrual, debt-change helpers, and collateralization checks. |
| stablecoin/core/src/stability_fee.rs | Introduces core stability-fee math/state: accumulator, continuous compounding, accrual helpers, and collateralization checks. |
| stablecoin/core/src/lib.rs | Wires stability-fee APIs/types into stablecoin_core, adds PDA derivation/verification helpers, and re-exports oracle helpers/types. |
| stablecoin/core/Cargo.toml | Adds oracle_core dependency for shared oracle account/feed types. |
| oracle/core/src/lib.rs | New shared crate entrypoint exposing oracle price-account and feed validation APIs. |
| oracle/core/src/account.rs | Defines the canonical OraclePriceAccount data layout with Borsh encode/decode helpers. |
| oracle/core/src/feed.rs | Implements consumer-side feed validation (validate_price_account) and a swappable PriceFeed interface. |
| oracle/core/src/tests.rs | Adds tests for oracle account encoding length and feed validation behaviors (mismatch, stale, zero price, future timestamp). |
| oracle/core/Cargo.toml | Introduces the oracle_core crate manifest and dependencies. |
| mock_oracle/src/lib.rs | New mock oracle program crate exposing set_price and tests. |
| mock_oracle/src/set_price.rs | Adds a mock oracle instruction implementation to write canonical oracle prices into an authorized account. |
| mock_oracle/src/tests.rs | Adds tests covering initialization/update behavior and validation panics for the mock oracle program. |
| mock_oracle/core/src/lib.rs | Adds mock_oracle_core instruction/data types and conversion into OraclePriceAccount. |
| mock_oracle/core/Cargo.toml | Introduces the mock_oracle_core crate manifest. |
| mock_oracle/Cargo.toml | Introduces the mock_oracle_program crate manifest. |
| mock_oracle/methods/src/lib.rs | Adds host-side embedding for the mock oracle guest ELF via generated constants. |
| mock_oracle/methods/build.rs | Adds build script to embed RISC Zero methods. |
| mock_oracle/methods/Cargo.toml | Adds the mock oracle methods crate manifest and RISC0 build configuration. |
| mock_oracle/methods/guest/src/bin/mock_oracle.rs | Adds the mock oracle guest entrypoint and instruction wrapper for set_price. |
| mock_oracle/methods/guest/Cargo.toml | Adds the mock oracle guest manifest (incl. dependencies for guest wrapper + oracle types). |
| mock_oracle/methods/guest/Cargo.lock | Adds the mock oracle guest lockfile. |
| Cargo.toml | Registers new workspace members (oracle/mock-oracle) and updates workspace dependency versions. |
| Cargo.lock | Updates workspace lockfile for new crates and dependency graph changes. |
| artifacts/stablecoin-idl.json | Regenerates Stablecoin IDL to include the new initialize instruction and updated account shapes. |
| artifacts/mock_oracle-idl.json | Adds generated IDL for the new mock oracle program. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Collaborator
Author
539e70c to
9253664
Compare
9253664 to
387dd86
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
Summary
Adds stability-fee accrual support to the Stablecoin Program by storing a
per-position fee accumulator snapshot and a program-global stability fee
accumulator. Debt helpers accrue global fees first, settle the position against
the current accumulator, then apply debt changes so collateralization checks use
fee-adjusted debt.
What Changed
StabilityFeeStatewithstability_fee_accumulator,stability_fee_rate, andlast_fee_update_timestamp.Position.fee_accumulatorwhile keeping nominaldebt_amount.initialize_stability_fee_stateso positions snapshot an initializedglobal accumulator at creation.
Scope
This only implements the stability-fee accrual shape requested by issue #97.
It does not add debt-generation or repayment instructions, does not read oracle
accounts directly, and does not add a fee-rate update instruction.
Validation