Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion artifacts/stablecoin-idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,34 @@
"init": false
},
{
"name": "destination",
"name": "user_collateral_holding",
"writable": true,
"signer": false,
"init": false
},
{
"name": "stability_fee_accumulator",
"writable": false,
"signer": false,
"init": false
},
{
"name": "redemption_price_state",
"writable": false,
"signer": false,
"init": false
},
{
"name": "protocol_parameters",
"writable": false,
"signer": false,
"init": false
},
{
"name": "clock",
"writable": false,
"signer": false,
"init": false
}
],
"args": [
Expand Down
38 changes: 38 additions & 0 deletions programs/integration_tests/tests/stablecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,32 @@ impl Accounts {
}
}

fn stability_fee_accumulator_init() -> Account {
Account {
program_owner: Ids::stablecoin_program(),
balance: 0,
data: Data::from(&stablecoin_core::StabilityFeeAccumulator {
accumulated_rate_at_last_accrual: stablecoin_core::math::FIXED_POINT_ONE,
last_accrued_at: OPEN_POSITION_NOW,
}),
nonce: Nonce(0),
}
}

fn redemption_price_state_init() -> Account {
Account {
program_owner: Ids::stablecoin_program(),
balance: 0,
data: Data::from(&stablecoin_core::RedemptionPriceState {
redemption_price_at_last_update: protocol_config::INITIAL_REDEMPTION_PRICE,
redemption_rate_per_millisecond: stablecoin_core::math::FIXED_POINT_ONE,
controller_integral_term: 0,
last_updated_at: OPEN_POSITION_NOW,
}),
nonce: Nonce(0),
}
}

fn oracle_init(base_asset: AccountId, quote_asset: AccountId) -> Account {
Self::oracle_with(
base_asset,
Expand Down Expand Up @@ -331,6 +357,14 @@ fn state_for_stablecoin_tests() -> V03State {
compute_protocol_parameters_pda(Ids::stablecoin_program()),
Accounts::protocol_parameters_init(),
);
state.force_insert_account(
compute_stability_fee_accumulator_pda(Ids::stablecoin_program()),
Accounts::stability_fee_accumulator_init(),
);
state.force_insert_account(
compute_redemption_price_state_pda(Ids::stablecoin_program()),
Accounts::redemption_price_state_init(),
);
seed_clock(&mut state, OPEN_POSITION_NOW);
state
}
Expand Down Expand Up @@ -478,6 +512,10 @@ fn stablecoin_open_position_then_withdraw_collateral() {
Ids::position(),
Ids::vault(),
Ids::user_holding(),
compute_stability_fee_accumulator_pda(Ids::stablecoin_program()),
compute_redemption_price_state_pda(Ids::stablecoin_program()),
compute_protocol_parameters_pda(Ids::stablecoin_program()),
CLOCK_01_PROGRAM_ACCOUNT_ID,
],
vec![current_nonce(&state, Ids::owner())],
withdraw,
Expand Down
30 changes: 19 additions & 11 deletions programs/stablecoin/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,28 @@ pub enum Instruction {
},
/// Withdraw `amount` collateral tokens from a position back to a user-controlled holding.
///
/// Required accounts (4):
/// - Owner account (authorized)
/// - Position account (initialized, owned by `self_program_id`)
/// - Position vault token holding (address must match
/// `compute_position_vault_pda(self_program_id, position_id)`)
/// - Destination user collateral holding (initialized, owned by the vault's Token Program,
/// `TokenHolding.definition_id` matches the vault holding's definition)
/// Blocked while the protocol is frozen. The §6.2 collateralization
/// invariant is checked *after* the decrement, against debt and redemption
/// price both projected forward to the clock timestamp (§5.3).
///
/// Required accounts (8), in order:
/// 1. `owner` — authorized.
/// 2. `position` — initialized, writable, owned by `self_program_id`; address must match
/// `compute_position_pda(self_program_id, owner, position_nonce)`.
/// 3. `vault` — initialized, writable; must equal `Position.vault_account_id`. Authorized in
/// the chained call via its PDA seed.
/// 4. `user_collateral_holding` — initialized destination; NOT required to be authorized. Same
/// Token Program and definition as the vault holding.
/// 5. `stability_fee_accumulator` — initialized, read-only; at its canonical PDA. Projected to
/// `now` for the position's nominal debt.
/// 6. `redemption_price_state` — initialized, read-only; at its canonical PDA. Projected to
/// `now` for the current redemption price.
/// 7. `protocol_parameters` — initialized, read-only; at its canonical PDA. Supplies the
/// minimum collateralization ratio and the freeze flag.
/// 8. `clock` — the system `CLOCK_01` account; read-only.
///
/// `token_program_id` is derived from `vault.account.program_owner`;
/// the collateral definition is read from the PDA-verified vault holding.
///
/// **Note:** until issues #97/#95 land, this instruction hard-asserts
/// `Position.normalized_debt_amount == 0` instead of accruing fees and
/// checking the collateralization ratio.
WithdrawCollateral {
/// Amount of collateral tokens to move from the vault back to `destination`.
amount: u128,
Expand Down
16 changes: 14 additions & 2 deletions programs/stablecoin/methods/guest/src/bin/stablecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ mod stablecoin {
/// [`stablecoin_program::withdraw_collateral::withdraw_collateral`] for the
/// full list).
#[instruction]
#[allow(
clippy::too_many_arguments,
reason = "the eight account inputs mirror the spec §10.6 ABI"
)]
pub fn withdraw_collateral(
ctx: ProgramContext,
#[account(signer)]
Expand All @@ -299,15 +303,23 @@ mod stablecoin {
#[account(mut)]
vault: AccountWithMetadata,
#[account(mut)]
destination: AccountWithMetadata,
user_collateral_holding: AccountWithMetadata,
stability_fee_accumulator: AccountWithMetadata,
redemption_price_state: AccountWithMetadata,
protocol_parameters: AccountWithMetadata,
clock: AccountWithMetadata,
amount: u128,
) -> SpelResult {
let (post_states, chained_calls) =
stablecoin_program::withdraw_collateral::withdraw_collateral(
owner,
position,
vault,
destination,
user_collateral_holding,
stability_fee_accumulator,
redemption_price_state,
protocol_parameters,
clock,
ctx.self_program_id,
amount,
);
Expand Down
Loading
Loading