2026-05-21 18:04:48 +02:00
|
|
|
#![cfg_attr(not(test), no_main)]
|
2026-05-11 14:51:50 +02:00
|
|
|
|
2026-06-24 14:41:38 -03:00
|
|
|
use nssa_core::account::{AccountId, AccountWithMetadata};
|
2026-05-12 13:04:59 -03:00
|
|
|
use spel_framework::context::ProgramContext;
|
2026-05-11 14:51:50 +02:00
|
|
|
use spel_framework::prelude::*;
|
|
|
|
|
|
2026-05-21 18:04:48 +02:00
|
|
|
#[cfg(not(test))]
|
2026-05-11 14:51:50 +02:00
|
|
|
risc0_zkvm::guest::entry!(main);
|
|
|
|
|
|
|
|
|
|
#[lez_program(instruction = "stablecoin_core::Instruction")]
|
|
|
|
|
mod stablecoin {
|
2026-05-12 13:04:59 -03:00
|
|
|
#[allow(unused_imports)]
|
2026-05-11 14:51:50 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
2026-06-24 14:41:38 -03:00
|
|
|
/// Initialize protocol globals and the stablecoin token definition.
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface initializes all stablecoin singleton accounts"
|
|
|
|
|
)]
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn initialize_program(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
admin: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
redemption_price_state: AccountWithMetadata,
|
|
|
|
|
stablecoin_definition: AccountWithMetadata,
|
|
|
|
|
stablecoin_master_holding: AccountWithMetadata,
|
|
|
|
|
collateral_definition: AccountWithMetadata,
|
|
|
|
|
market_price_oracle: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
|
|
|
|
freeze_authority_account_id: AccountId,
|
|
|
|
|
initial_stability_fee_per_millisecond: u128,
|
|
|
|
|
initial_controller_proportional_gain: i128,
|
|
|
|
|
initial_controller_integral_gain: i128,
|
|
|
|
|
initial_minimum_collateralization_ratio: u128,
|
|
|
|
|
minimum_milliseconds_between_rate_updates: u64,
|
|
|
|
|
maximum_oracle_price_age_milliseconds: u64,
|
|
|
|
|
initial_redemption_price: u128,
|
|
|
|
|
stablecoin_name: String,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::initialize_program::initialize_program(
|
|
|
|
|
admin,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
stability_fee_accumulator,
|
|
|
|
|
redemption_price_state,
|
|
|
|
|
stablecoin_definition,
|
|
|
|
|
stablecoin_master_holding,
|
|
|
|
|
collateral_definition,
|
|
|
|
|
market_price_oracle,
|
|
|
|
|
clock,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
freeze_authority_account_id,
|
|
|
|
|
initial_stability_fee_per_millisecond,
|
|
|
|
|
initial_controller_proportional_gain,
|
|
|
|
|
initial_controller_integral_gain,
|
|
|
|
|
initial_minimum_collateralization_ratio,
|
|
|
|
|
minimum_milliseconds_between_rate_updates,
|
|
|
|
|
maximum_oracle_price_age_milliseconds,
|
|
|
|
|
initial_redemption_price,
|
|
|
|
|
stablecoin_name,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Roll the global stability-fee accumulator forward.
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn accrue_stability_fee(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
caller: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) =
|
|
|
|
|
stablecoin_program::accrue_stability_fee::accrue_stability_fee(
|
|
|
|
|
caller,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
stability_fee_accumulator,
|
|
|
|
|
clock,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update stability-fee rate after accruing pending fees at the old rate.
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn set_stability_fee_per_millisecond(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
admin: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
|
|
|
|
new_rate: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) =
|
|
|
|
|
stablecoin_program::set_stability_fee_per_millisecond::set_stability_fee_per_millisecond(
|
|
|
|
|
admin,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
stability_fee_accumulator,
|
|
|
|
|
clock,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
new_rate,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
/// Open a new collateral-only position for the calling owner.
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn open_position(
|
2026-05-12 13:04:59 -03:00
|
|
|
ctx: ProgramContext,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(signer)]
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
owner: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(init)]
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
position: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(init)]
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
vault: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut, signer)]
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
user_holding: AccountWithMetadata,
|
2026-06-24 14:41:38 -03:00
|
|
|
collateral_definition: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
|
|
|
|
position_nonce: u64,
|
|
|
|
|
initial_collateral_amount: u128,
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::open_position::open_position(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
vault,
|
|
|
|
|
user_holding,
|
2026-06-24 14:41:38 -03:00
|
|
|
collateral_definition,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
clock,
|
2026-05-12 13:04:59 -03:00
|
|
|
ctx.self_program_id,
|
2026-06-24 14:41:38 -03:00
|
|
|
position_nonce,
|
|
|
|
|
initial_collateral_amount,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Mint stablecoin debt against an existing position.
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface passes explicit position, token, oracle, and protocol accounts"
|
|
|
|
|
)]
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn generate_debt(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
owner: AccountWithMetadata,
|
|
|
|
|
position: AccountWithMetadata,
|
|
|
|
|
stablecoin_definition: AccountWithMetadata,
|
|
|
|
|
user_stablecoin_holding: AccountWithMetadata,
|
|
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
redemption_price_state: AccountWithMetadata,
|
|
|
|
|
market_price_oracle: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
|
|
|
|
amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::generate_debt::generate_debt(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
stablecoin_definition,
|
|
|
|
|
user_stablecoin_holding,
|
|
|
|
|
stability_fee_accumulator,
|
|
|
|
|
redemption_price_state,
|
|
|
|
|
market_price_oracle,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
clock,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
amount,
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
);
|
2026-05-12 13:04:59 -03:00
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
feat(stablecoin): implement `open_position`
Adds the `open_position` instruction to the Stablecoin Program. The instruction
claims a per-owner `Position` PDA, initializes a collateral vault token holding
via a chained `Token::InitializeAccount` under the vault's PDA authority, and
moves `collateral_amount` from the user's holding into the vault with a chained
`Token::Transfer`. `Position` is persisted with `collateral_amount` and
`debt_amount = 0`; the debt path is deferred to `generate_debt`.
- Add `Position` struct, `OpenPosition` instruction variant, and
`compute_position_pda{,_seed}` / `compute_position_vault_pda{,_seed}` helpers
in `stablecoin_core` with domain-separated PDA seeds.
- Implement `open_position::open_position` mirroring the ATA `create` and AMM
`new_definition` patterns: authorization and uninitialized-state asserts, PDA
verification, and same-transaction chained `InitializeAccount` + `Transfer`.
- Wire the new instruction through the SPEL guest and regenerate the stablecoin
IDL artifact.
- Cover the happy path, all assertion paths, and PDA determinism /
non-collision in 11 new unit tests.
2026-05-11 17:14:27 -03:00
|
|
|
}
|
2026-05-19 15:59:10 +02:00
|
|
|
|
2026-06-24 14:41:38 -03:00
|
|
|
/// Withdraw collateral from an existing position.
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface passes explicit position and protocol accounts"
|
|
|
|
|
)]
|
2026-05-19 15:59:10 +02:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn withdraw_collateral(
|
|
|
|
|
ctx: ProgramContext,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(signer)]
|
2026-05-19 15:59:10 +02:00
|
|
|
owner: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut)]
|
2026-05-19 15:59:10 +02:00
|
|
|
position: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut)]
|
2026-05-19 15:59:10 +02:00
|
|
|
vault: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut)]
|
2026-05-19 15:59:10 +02:00
|
|
|
destination: AccountWithMetadata,
|
2026-06-24 14:41:38 -03:00
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
redemption_price_state: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
2026-05-19 15:59:10 +02:00
|
|
|
amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) =
|
|
|
|
|
stablecoin_program::withdraw_collateral::withdraw_collateral(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
vault,
|
|
|
|
|
destination,
|
2026-06-24 14:41:38 -03:00
|
|
|
stability_fee_accumulator,
|
|
|
|
|
redemption_price_state,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
clock,
|
2026-05-19 15:59:10 +02:00
|
|
|
ctx.self_program_id,
|
|
|
|
|
amount,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-05-22 11:09:16 +02:00
|
|
|
|
2026-06-24 14:41:38 -03:00
|
|
|
/// Repay stablecoin debt against an existing position.
|
2026-05-22 11:09:16 +02:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn repay_debt(
|
|
|
|
|
ctx: ProgramContext,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(signer)]
|
2026-05-22 11:09:16 +02:00
|
|
|
owner: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut)]
|
2026-05-22 11:09:16 +02:00
|
|
|
position: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut)]
|
2026-05-22 11:09:16 +02:00
|
|
|
stablecoin_definition: AccountWithMetadata,
|
2026-06-26 16:55:23 -03:00
|
|
|
#[account(mut, signer)]
|
2026-05-22 11:09:16 +02:00
|
|
|
user_stablecoin_holding: AccountWithMetadata,
|
2026-06-24 14:41:38 -03:00
|
|
|
stability_fee_accumulator: AccountWithMetadata,
|
|
|
|
|
protocol_parameters: AccountWithMetadata,
|
|
|
|
|
clock: AccountWithMetadata,
|
2026-05-22 11:09:16 +02:00
|
|
|
amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::repay_debt::repay_debt(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
stablecoin_definition,
|
|
|
|
|
user_stablecoin_holding,
|
2026-06-24 14:41:38 -03:00
|
|
|
stability_fee_accumulator,
|
|
|
|
|
protocol_parameters,
|
|
|
|
|
clock,
|
2026-05-22 11:09:16 +02:00
|
|
|
ctx.self_program_id,
|
|
|
|
|
amount,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-05-11 14:51:50 +02:00
|
|
|
}
|