Skip to content

feat(stablecoin): add stability fee accrual#120

Open
3esmit wants to merge 1 commit into
mainfrom
issue-97-stability-fee-accrual
Open

feat(stablecoin): add stability fee accrual#120
3esmit wants to merge 1 commit into
mainfrom
issue-97-stability-fee-accrual

Conversation

@3esmit
Copy link
Copy Markdown
Collaborator

@3esmit 3esmit commented May 21, 2026

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

  • Added StabilityFeeState with stability_fee_accumulator,
    stability_fee_rate, and last_fee_update_timestamp.
  • Added Position.fee_accumulator while keeping nominal debt_amount.
  • Added fixed-point continuous-compounding helpers and position accrual helpers.
  • Updated collateralization helpers to use accrued debt and redemption price.
  • Added initialize_stability_fee_state so positions snapshot an initialized
    global accumulator at creation.
  • Regenerated the Stablecoin Program IDL.

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

cargo +nightly fmt --all -- --check
taplo fmt --check .
RISC0_SKIP_BUILD=1 cargo +1.94.0 clippy --workspace --all-targets -- -D warnings
RISC0_DEV_MODE=1 cargo +1.94.0 test --workspace --exclude integratioan_tests
RISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests
cargo +1.94.0 build -p idl-gen --release
# full IDL drift loop from the repository guide

@3esmit 3esmit self-assigned this May 21, 2026
Copilot AI review requested due to automatic review settings May 21, 2026 06:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 StabilityFeeState singleton PDA initialization flow and required it during open_position so 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_core primitives and a mock_oracle program + 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.

@3esmit
Copy link
Copy Markdown
Collaborator Author

3esmit commented May 21, 2026

This PR is branched in top of #109
The commit about this PR is 539e70c

@0x-r4bbit 0x-r4bbit force-pushed the issue-97-stability-fee-accrual branch from 539e70c to 9253664 Compare May 26, 2026 08:50
@0x-r4bbit 0x-r4bbit force-pushed the issue-97-stability-fee-accrual branch from 9253664 to 387dd86 Compare May 26, 2026 12:10
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.

Implement stability fee accrual

2 participants