Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lending pool add bank lite #138

Merged
merged 4 commits into from
Nov 24, 2023
Merged
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
12 changes: 6 additions & 6 deletions clients/rust/marginfi-cli/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ pub enum GroupCommand {
#[clap(long)]
liability_weight_maint: f64,
#[clap(long)]
deposit_limit: u64,
deposit_limit_ui: u64,
#[clap(long)]
borrow_limit: u64,
borrow_limit_ui: u64,
#[clap(long)]
pyth_oracle: Pubkey,
#[clap(long)]
Expand Down Expand Up @@ -473,8 +473,8 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> {
insurance_ir_fee,
protocol_fixed_fee_apr,
protocol_ir_fee,
deposit_limit,
borrow_limit,
deposit_limit_ui,
borrow_limit_ui,
risk_tier,
oracle_type,
} => processor::group_add_bank(
Expand All @@ -487,8 +487,8 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> {
asset_weight_maint,
liability_weight_init,
liability_weight_maint,
deposit_limit,
borrow_limit,
deposit_limit_ui,
borrow_limit_ui,
optimal_utilization_rate,
plateau_interest_rate,
max_interest_rate,
Expand Down
13 changes: 10 additions & 3 deletions clients/rust/marginfi-cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ pub fn group_add_bank(
asset_weight_maint: f64,
liability_weight_init: f64,
liability_weight_maint: f64,
deposit_limit: u64,
borrow_limit: u64,
deposit_limit_ui: u64,
borrow_limit_ui: u64,
optimal_utilization_rate: f64,
plateau_interest_rate: f64,
max_interest_rate: f64,
Expand Down Expand Up @@ -342,6 +342,12 @@ pub fn group_add_bank(
let protocol_fixed_fee_apr: WrappedI80F48 = I80F48::from_num(protocol_fixed_fee_apr).into();
let protocol_ir_fee: WrappedI80F48 = I80F48::from_num(protocol_ir_fee).into();

let mint_account = rpc_client.get_account(&bank_mint)?;
let mint = spl_token::state::Mint::unpack(&mint_account.data)?;

let deposit_limit = deposit_limit_ui * 10_u64.pow(mint.decimals as u32);
let borrow_limit = borrow_limit_ui * 10_u64.pow(mint.decimals as u32);

let interest_rate_config = InterestRateConfig {
optimal_utilization_rate,
plateau_interest_rate,
Expand Down Expand Up @@ -425,7 +431,8 @@ pub fn group_add_bank(
oracle_keys: create_oracle_key_array(oracle_key),
risk_tier: risk_tier.into(),
..BankConfig::default()
},
}
.into(),
})
.instructions()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use anchor_lang::prelude::*;
use solana_program::sysvar::Sysvar;

pub fn initialize(ctx: Context<MarginfiAccountInitialize>) -> MarginfiResult {
pub fn initialize_account(ctx: Context<MarginfiAccountInitialize>) -> MarginfiResult {
let MarginfiAccountInitialize {
authority,
marginfi_group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
INSURANCE_VAULT_SEED, LIQUIDITY_VAULT_AUTHORITY_SEED, LIQUIDITY_VAULT_SEED,
},
events::{GroupEventHeader, LendingPoolBankCreateEvent},
state::marginfi_group::{Bank, BankConfig, MarginfiGroup},
state::marginfi_group::{Bank, BankConfig, BankConfigCompact, MarginfiGroup},
MarginfiResult,
};
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn lending_pool_add_bank(
}

#[derive(Accounts)]
#[instruction(bank_config: BankConfig)]
#[instruction(bank_config: BankConfigCompact)]
pub struct LendingPoolAddBank<'info> {
pub marginfi_group: AccountLoader<'info, MarginfiGroup>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::events::{GroupEventHeader, MarginfiGroupCreateEvent};
use crate::{state::marginfi_group::MarginfiGroup, MarginfiResult};
use anchor_lang::prelude::*;

pub fn initialize(ctx: Context<MarginfiGroupInitialize>) -> MarginfiResult {
pub fn initialize_group(ctx: Context<MarginfiGroupInitialize>) -> MarginfiResult {
let marginfi_group = &mut ctx.accounts.marginfi_group.load_init()?;

marginfi_group.set_initial_configuration(ctx.accounts.admin.key());
Expand Down
10 changes: 5 additions & 5 deletions programs/marginfi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod utils;
use anchor_lang::prelude::*;
use instructions::*;
use prelude::*;
use state::marginfi_group::{BankConfig, BankConfigOpt};
use state::marginfi_group::{BankConfigCompact, BankConfigOpt};

cfg_if::cfg_if! {
if #[cfg(feature = "mainnet-beta")] {
Expand All @@ -27,7 +27,7 @@ pub mod marginfi {
use super::*;

pub fn marginfi_group_initialize(ctx: Context<MarginfiGroupInitialize>) -> MarginfiResult {
marginfi_group::initialize(ctx)
marginfi_group::initialize_group(ctx)
}

pub fn marginfi_group_configure(
Expand All @@ -39,9 +39,9 @@ pub mod marginfi {

pub fn lending_pool_add_bank(
ctx: Context<LendingPoolAddBank>,
bank_config: BankConfig,
bank_config: BankConfigCompact,
) -> MarginfiResult {
marginfi_group::lending_pool_add_bank(ctx, bank_config)
marginfi_group::lending_pool_add_bank(ctx, bank_config.into())
}

pub fn lending_pool_configure_bank(
Expand Down Expand Up @@ -85,7 +85,7 @@ pub mod marginfi {

/// Initialize a marginfi account for a given group
pub fn marginfi_account_initialize(ctx: Context<MarginfiAccountInitialize>) -> MarginfiResult {
marginfi_account::initialize(ctx)
marginfi_account::initialize_account(ctx)
}

pub fn lending_account_deposit(
Expand Down
126 changes: 126 additions & 0 deletions programs/marginfi/src/state/marginfi_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,55 @@ pub fn load_pyth_price_feed(ai: &AccountInfo) -> MarginfiResult<PriceFeed> {
Ok(price_feed)
}

#[zero_copy]
#[repr(C)]
#[cfg_attr(
any(feature = "test", feature = "client"),
derive(PartialEq, Eq, TypeLayout)
)]
#[derive(Default, Debug, AnchorDeserialize, AnchorSerialize)]
pub struct InterestRateConfigCompact {
// Curve Params
pub optimal_utilization_rate: WrappedI80F48,
pub plateau_interest_rate: WrappedI80F48,
pub max_interest_rate: WrappedI80F48,

// Fees
pub insurance_fee_fixed_apr: WrappedI80F48,
pub insurance_ir_fee: WrappedI80F48,
pub protocol_fixed_fee_apr: WrappedI80F48,
pub protocol_ir_fee: WrappedI80F48,
}

impl From<InterestRateConfigCompact> for InterestRateConfig {
fn from(ir_config: InterestRateConfigCompact) -> Self {
InterestRateConfig {
optimal_utilization_rate: ir_config.optimal_utilization_rate,
plateau_interest_rate: ir_config.plateau_interest_rate,
max_interest_rate: ir_config.max_interest_rate,
insurance_fee_fixed_apr: ir_config.insurance_fee_fixed_apr,
insurance_ir_fee: ir_config.insurance_ir_fee,
protocol_fixed_fee_apr: ir_config.protocol_fixed_fee_apr,
protocol_ir_fee: ir_config.protocol_ir_fee,
_padding: [0; 8],
}
}
}

impl From<InterestRateConfig> for InterestRateConfigCompact {
fn from(ir_config: InterestRateConfig) -> Self {
InterestRateConfigCompact {
optimal_utilization_rate: ir_config.optimal_utilization_rate,
plateau_interest_rate: ir_config.plateau_interest_rate,
max_interest_rate: ir_config.max_interest_rate,
insurance_fee_fixed_apr: ir_config.insurance_fee_fixed_apr,
insurance_ir_fee: ir_config.insurance_ir_fee,
protocol_fixed_fee_apr: ir_config.protocol_fixed_fee_apr,
protocol_ir_fee: ir_config.protocol_ir_fee,
}
}
}

#[zero_copy]
#[repr(C)]
#[cfg_attr(
Expand Down Expand Up @@ -760,6 +809,83 @@ pub enum RiskTier {
Isolated,
}

#[zero_copy]
#[repr(C)]
#[cfg_attr(
any(feature = "test", feature = "client"),
derive(PartialEq, Eq, TypeLayout)
)]
#[derive(AnchorDeserialize, AnchorSerialize, Debug)]
/// TODO: Convert weights to (u64, u64) to avoid precision loss (maybe?)
pub struct BankConfigCompact {
pub asset_weight_init: WrappedI80F48,
pub asset_weight_maint: WrappedI80F48,

pub liability_weight_init: WrappedI80F48,
pub liability_weight_maint: WrappedI80F48,

pub deposit_limit: u64,

pub interest_rate_config: InterestRateConfigCompact,
pub operational_state: BankOperationalState,

pub oracle_setup: OracleSetup,
pub oracle_keys: [Pubkey; MAX_ORACLE_KEYS],

pub borrow_limit: u64,

pub risk_tier: RiskTier,

/// USD denominated limit for calculating asset value for initialization margin requirements.
/// Example, if total SOL deposits are equal to $1M and the limit it set to $500K,
/// then SOL assets will be discounted by 50%.
///
/// In other words the max value of liabilities that can be backed by the asset is $500K.
/// This is useful for limiting the damage of orcale attacks.
///
/// Value is UI USD value, for example value 100 -> $100
pub total_asset_value_init_limit: u64,
}

impl From<BankConfigCompact> for BankConfig {
fn from(config: BankConfigCompact) -> Self {
Self {
asset_weight_init: config.asset_weight_init,
asset_weight_maint: config.asset_weight_maint,
liability_weight_init: config.liability_weight_init,
liability_weight_maint: config.liability_weight_maint,
deposit_limit: config.deposit_limit,
interest_rate_config: config.interest_rate_config.into(),
operational_state: config.operational_state,
oracle_setup: config.oracle_setup,
oracle_keys: config.oracle_keys,
borrow_limit: config.borrow_limit,
risk_tier: config.risk_tier,
total_asset_value_init_limit: config.total_asset_value_init_limit,
_padding: [0; 5],
}
}
}

impl From<BankConfig> for BankConfigCompact {
fn from(config: BankConfig) -> Self {
Self {
asset_weight_init: config.asset_weight_init,
asset_weight_maint: config.asset_weight_maint,
liability_weight_init: config.liability_weight_init,
liability_weight_maint: config.liability_weight_maint,
deposit_limit: config.deposit_limit,
interest_rate_config: config.interest_rate_config.into(),
operational_state: config.operational_state,
oracle_setup: config.oracle_setup,
oracle_keys: config.oracle_keys,
borrow_limit: config.borrow_limit,
risk_tier: config.risk_tier,
total_asset_value_init_limit: config.total_asset_value_init_limit,
}
}
}

assert_struct_size!(BankConfig, 544);
#[zero_copy]
#[repr(C)]
Expand Down
5 changes: 4 additions & 1 deletion test-utils/src/marginfi_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ impl MarginfiGroupFixture {
let ix = Instruction {
program_id: marginfi::id(),
accounts,
data: marginfi::instruction::LendingPoolAddBank { bank_config }.data(),
data: marginfi::instruction::LendingPoolAddBank {
bank_config: bank_config.into(),
}
.data(),
};

let tx = Transaction::new_signed_with_payer(
Expand Down
Loading