-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Boilerplate to init staked banks with group-level default settings
- Loading branch information
1 parent
414f18a
commit 16c98f5
Showing
8 changed files
with
258 additions
and
24 deletions.
There are no files selected for viewing
19 changes: 2 additions & 17 deletions
19
programs/marginfi/src/instructions/marginfi_group/add_pool_permissionless.rs
This file contains 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
64 changes: 63 additions & 1 deletion
64
programs/marginfi/src/instructions/marginfi_group/edit_stake_settings.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
// TODO | ||
// Used by the group admin to edit the default features of staked collateral banks. Remember to | ||
// propagate afterwards. | ||
use crate::state::marginfi_group::{RiskTier, WrappedI80F48}; | ||
use crate::state::staked_settings::StakedSettings; | ||
use crate::{set_if_some, MarginfiGroup}; | ||
use anchor_lang::prelude::*; | ||
|
||
pub fn edit_staked_settings( | ||
ctx: Context<EditStakedSettings>, | ||
settings: StakedSettingsEditConfig, | ||
) -> Result<()> { | ||
let mut staked_settings = ctx.accounts.staked_settings.load_mut()?; | ||
|
||
set_if_some!(staked_settings.oracle, settings.oracle); | ||
set_if_some!( | ||
staked_settings.asset_weight_init, | ||
settings.asset_weight_init | ||
); | ||
set_if_some!( | ||
staked_settings.asset_weight_maint, | ||
settings.asset_weight_maint | ||
); | ||
set_if_some!(staked_settings.deposit_limit, settings.deposit_limit); | ||
set_if_some!( | ||
staked_settings.total_asset_value_init_limit, | ||
settings.total_asset_value_init_limit | ||
); | ||
set_if_some!(staked_settings.oracle_max_age, settings.oracle_max_age); | ||
set_if_some!(staked_settings.risk_tier, settings.risk_tier); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct EditStakedSettings<'info> { | ||
#[account( | ||
has_one = admin | ||
)] | ||
pub marginfi_group: AccountLoader<'info, MarginfiGroup>, | ||
|
||
#[account(mut)] | ||
pub admin: Signer<'info>, | ||
|
||
#[account( | ||
mut, | ||
has_one = marginfi_group | ||
)] | ||
pub staked_settings: AccountLoader<'info, StakedSettings>, | ||
} | ||
|
||
#[derive(AnchorDeserialize, AnchorSerialize, Default)] | ||
pub struct StakedSettingsEditConfig { | ||
pub oracle: Option<Pubkey>, | ||
|
||
pub asset_weight_init: Option<WrappedI80F48>, | ||
pub asset_weight_maint: Option<WrappedI80F48>, | ||
|
||
pub deposit_limit: Option<u64>, | ||
pub total_asset_value_init_limit: Option<u64>, | ||
|
||
pub oracle_max_age: Option<u16>, | ||
pub risk_tier: Option<RiskTier>, | ||
} |
72 changes: 71 additions & 1 deletion
72
programs/marginfi/src/instructions/marginfi_group/init_staked_settings.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,71 @@ | ||
// TODO | ||
// Used by the group admin to enable staked collateral banks and configure their default features | ||
use crate::constants::STAKED_SETTINGS_SEED; | ||
use crate::state::marginfi_group::{RiskTier, WrappedI80F48}; | ||
use crate::state::staked_settings::StakedSettings; | ||
use crate::MarginfiGroup; | ||
use anchor_lang::prelude::*; | ||
|
||
pub fn initialize_staked_settings( | ||
ctx: Context<InitStakedSettings>, | ||
settings: StakedSettingsConfig, | ||
) -> Result<()> { | ||
let mut staked_settings = ctx.accounts.staked_settings.load_init()?; | ||
|
||
*staked_settings = StakedSettings::new( | ||
ctx.accounts.staked_settings.key(), | ||
ctx.accounts.marginfi_group.key(), | ||
settings.oracle, | ||
settings.asset_weight_init, | ||
settings.asset_weight_maint, | ||
settings.deposit_limit, | ||
settings.total_asset_value_init_limit, | ||
settings.oracle_max_age, | ||
settings.risk_tier, | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct InitStakedSettings<'info> { | ||
#[account( | ||
has_one = admin | ||
)] | ||
pub marginfi_group: AccountLoader<'info, MarginfiGroup>, | ||
|
||
#[account(mut)] | ||
pub admin: Signer<'info>, | ||
|
||
/// Pays the init fee | ||
#[account(mut)] | ||
pub fee_payer: Signer<'info>, | ||
|
||
#[account( | ||
init, | ||
seeds = [ | ||
marginfi_group.key().as_ref(), | ||
STAKED_SETTINGS_SEED.as_bytes() | ||
], | ||
bump, | ||
payer = fee_payer, | ||
space = 8 + StakedSettings::LEN, | ||
)] | ||
pub staked_settings: AccountLoader<'info, StakedSettings>, | ||
|
||
pub rent: Sysvar<'info, Rent>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(AnchorDeserialize, AnchorSerialize, Default)] | ||
pub struct StakedSettingsConfig { | ||
pub oracle: Pubkey, | ||
|
||
pub asset_weight_init: WrappedI80F48, | ||
pub asset_weight_maint: WrappedI80F48, | ||
|
||
pub deposit_limit: u64, | ||
pub total_asset_value_init_limit: u64, | ||
|
||
pub oracle_max_age: u16, | ||
pub risk_tier: RiskTier, | ||
} |
This file contains 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
37 changes: 36 additions & 1 deletion
37
programs/marginfi/src/instructions/marginfi_group/propagate_staked_settings.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,36 @@ | ||
// TODO | ||
// Permissionless ix to propagate a group's staked collateral settings to any bank in that group | ||
use crate::state::marginfi_group::Bank; | ||
use crate::state::staked_settings::StakedSettings; | ||
use crate::MarginfiGroup; | ||
use anchor_lang::prelude::*; | ||
|
||
pub fn propagate_staked_settings(ctx: Context<PropagateStakedSettings>) -> Result<()> { | ||
let settings = ctx.accounts.staked_settings.load()?; | ||
let mut bank = ctx.accounts.bank.load_mut()?; | ||
|
||
bank.config.oracle_keys[0] = settings.oracle; | ||
bank.config.asset_weight_init = settings.asset_weight_init; | ||
bank.config.asset_weight_maint = settings.asset_weight_maint; | ||
bank.config.deposit_limit = settings.deposit_limit; | ||
bank.config.total_asset_value_init_limit = settings.total_asset_value_init_limit; | ||
bank.config.oracle_max_age = settings.oracle_max_age; | ||
bank.config.risk_tier = settings.risk_tier; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct PropagateStakedSettings<'info> { | ||
pub marginfi_group: AccountLoader<'info, MarginfiGroup>, | ||
|
||
#[account( | ||
has_one = marginfi_group | ||
)] | ||
pub staked_settings: AccountLoader<'info, StakedSettings>, | ||
|
||
#[account( | ||
mut, | ||
constraint = bank.load() ?.group == marginfi_group.key(), | ||
)] | ||
pub bank: AccountLoader<'info, Bank>, | ||
} |
This file contains 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
This file contains 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
This file contains 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