2026-05-21 18:04:48 +02:00
|
|
|
#![cfg_attr(not(test), no_main)]
|
2026-05-11 14:51:50 +02:00
|
|
|
|
2026-06-08 11:24:27 -03:00
|
|
|
use nssa_core::account::{AccountId, AccountWithMetadata};
|
|
|
|
|
use spel_framework::{context::ProgramContext, prelude::*};
|
2026-05-11 14:51:50 +02:00
|
|
|
|
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::*;
|
|
|
|
|
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// Returns the host program's panic-converted error if any precondition fails (see
|
|
|
|
|
/// [`stablecoin_program::open_position::open_position`] for the full list).
|
|
|
|
|
#[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,
|
|
|
|
|
token_definition: AccountWithMetadata,
|
|
|
|
|
collateral_amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::open_position::open_position(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
vault,
|
|
|
|
|
user_holding,
|
|
|
|
|
token_definition,
|
2026-05-12 13:04:59 -03:00
|
|
|
ctx.self_program_id,
|
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
|
|
|
collateral_amount,
|
|
|
|
|
);
|
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
|
|
|
|
|
|
|
|
/// Withdraw `amount` collateral tokens from an existing position back to a
|
|
|
|
|
/// user-controlled holding.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// Returns the host program's panic-converted error if any precondition
|
|
|
|
|
/// fails (see
|
|
|
|
|
/// [`stablecoin_program::withdraw_collateral::withdraw_collateral`] for the
|
|
|
|
|
/// full list).
|
|
|
|
|
#[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,
|
|
|
|
|
amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) =
|
|
|
|
|
stablecoin_program::withdraw_collateral::withdraw_collateral(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
vault,
|
|
|
|
|
destination,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
amount,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-05-22 11:09:16 +02:00
|
|
|
|
|
|
|
|
/// Repay `amount` of outstanding stablecoin debt against an existing position.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// Returns the host program's panic-converted error if any precondition
|
|
|
|
|
/// fails (see [`stablecoin_program::repay_debt::repay_debt`] for the
|
|
|
|
|
/// full list).
|
|
|
|
|
#[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,
|
|
|
|
|
amount: u128,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = stablecoin_program::repay_debt::repay_debt(
|
|
|
|
|
owner,
|
|
|
|
|
position,
|
|
|
|
|
stablecoin_definition,
|
|
|
|
|
user_stablecoin_holding,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
amount,
|
|
|
|
|
);
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(
|
|
|
|
|
post_states,
|
|
|
|
|
chained_calls,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-06-08 11:24:27 -03:00
|
|
|
|
|
|
|
|
/// Initialize redemption-rate feedback controller state for one stablecoin/feed pair.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// Returns the host program's panic-converted error if any precondition
|
|
|
|
|
/// fails (see
|
|
|
|
|
/// [`stablecoin_program::redemption_controller::initialize_redemption_controller`]
|
|
|
|
|
/// for the full list).
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface exposes controller configuration explicitly"
|
|
|
|
|
)]
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn initialize_redemption_controller(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
controller: AccountWithMetadata,
|
|
|
|
|
stablecoin_definition: AccountWithMetadata,
|
|
|
|
|
price_feed: AccountWithMetadata,
|
2026-06-15 11:08:09 -03:00
|
|
|
collateral_definition_id: AccountId,
|
2026-06-08 11:24:27 -03:00
|
|
|
initial_redemption_price: u128,
|
|
|
|
|
proportional_gain: u128,
|
|
|
|
|
integral_gain: u128,
|
|
|
|
|
max_integral_error: u128,
|
|
|
|
|
max_redemption_rate: u128,
|
|
|
|
|
max_price_feed_age: u64,
|
|
|
|
|
current_timestamp: u64,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let post_states =
|
|
|
|
|
stablecoin_program::redemption_controller::initialize_redemption_controller(
|
|
|
|
|
controller,
|
|
|
|
|
stablecoin_definition,
|
|
|
|
|
price_feed,
|
|
|
|
|
ctx.self_program_id,
|
2026-06-15 11:08:09 -03:00
|
|
|
collateral_definition_id,
|
2026-06-08 11:24:27 -03:00
|
|
|
initial_redemption_price,
|
|
|
|
|
proportional_gain,
|
|
|
|
|
integral_gain,
|
|
|
|
|
max_integral_error,
|
|
|
|
|
max_redemption_rate,
|
|
|
|
|
max_price_feed_age,
|
|
|
|
|
current_timestamp,
|
|
|
|
|
);
|
|
|
|
|
let validity_end = current_timestamp
|
|
|
|
|
.checked_add(1)
|
|
|
|
|
.expect("current_timestamp must allow an exact validity window");
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, vec![])
|
|
|
|
|
.try_with_timestamp_validity_window(current_timestamp..validity_end)
|
|
|
|
|
.expect("exact timestamp validity window must be non-empty"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update redemption price and redemption rate from the configured price feed.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// Returns the host program's panic-converted error if controller state
|
|
|
|
|
/// validation fails. Stale or unavailable price feeds pause updates by
|
|
|
|
|
/// emitting the controller state unchanged.
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn update_redemption_controller(
|
|
|
|
|
ctx: ProgramContext,
|
|
|
|
|
controller: AccountWithMetadata,
|
|
|
|
|
price_feed: AccountWithMetadata,
|
|
|
|
|
current_timestamp: u64,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let post_states = stablecoin_program::redemption_controller::update_redemption_controller(
|
|
|
|
|
controller,
|
|
|
|
|
price_feed,
|
|
|
|
|
ctx.self_program_id,
|
|
|
|
|
current_timestamp,
|
|
|
|
|
);
|
|
|
|
|
let validity_end = current_timestamp
|
|
|
|
|
.checked_add(1)
|
|
|
|
|
.expect("current_timestamp must allow an exact validity window");
|
|
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, vec![])
|
|
|
|
|
.try_with_timestamp_validity_window(current_timestamp..validity_end)
|
|
|
|
|
.expect("exact timestamp validity window must be non-empty"))
|
|
|
|
|
}
|
2026-05-11 14:51:50 +02:00
|
|
|
}
|