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: Liquid Insurance Funds #210

Closed
wants to merge 19 commits into from
Closed
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
170 changes: 170 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion programs/marginfi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ debug = []
solana-program = { workspace = true }

anchor-lang = { workspace = true }
anchor-spl = { workspace = true }
anchor-spl = { workspace = true, features = ["metadata"] }

pyth-sdk-solana = { workspace = true }
switchboard-v2 = { workspace = true }

mpl-token-metadata = "1.13.2"
spl-token = "1.0"

bytemuck = "1.9.1"
cfg-if = "1.0.0"
enum_dispatch = "0.3.11"
Expand Down
4 changes: 4 additions & 0 deletions programs/marginfi/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub const LIQUIDITY_VAULT_SEED: &str = "liquidity_vault";
pub const INSURANCE_VAULT_SEED: &str = "insurance_vault";
pub const FEE_VAULT_SEED: &str = "fee_vault";

pub const LIQUID_INSURANCE_SEED: &str = "liquid_insurance_seed";
pub const LIQUID_INSURANCE_WITHDRAW_SEED: &str = "liquid_insurance_withdraw";
pub const LIQUID_INSURANCE_USER_SEED: &str = "liquid_insurance_user";

pub const EMISSIONS_AUTH_SEED: &str = "emissions_auth_seed";
pub const EMISSIONS_TOKEN_ACCOUNT_SEED: &str = "emissions_token_account_seed";

Expand Down
2 changes: 2 additions & 0 deletions programs/marginfi/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub enum MarginfiError {
IllegalAccountAuthorityTransfer,
#[msg("Unauthorized")] // 6045
Unauthorized,
#[msg("InvalidWithdrawal")] // 6046
InvalidWithdrawal,
}

impl From<MarginfiError> for ProgramError {
Expand Down
32 changes: 32 additions & 0 deletions programs/marginfi/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub struct AccountEventHeader {
pub marginfi_group: Pubkey,
}

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct LiquidInsuranceFundEventHeader {
pub bank: Pubkey,
}

// marginfi group events

#[event]
Expand Down Expand Up @@ -146,3 +151,30 @@ pub struct MarginfiAccountTransferAccountAuthorityEvent {
pub old_account_authority: Pubkey,
pub new_account_authority: Pubkey,
}

// liquid insurance fund events

#[event]
pub struct MarginfiCreateNewLiquidInsuranceFundEvent {
pub header: LiquidInsuranceFundEventHeader,
}

#[event]
pub struct MarginfiDepositIntoLiquidInsuranceFundEvent {
pub header: LiquidInsuranceFundEventHeader,
pub amount: u64,
pub signer_token_address: Pubkey,
}

#[event]
pub struct MarginfiWithdrawClaimLiquidInsuranceFundEvent {
pub header: LiquidInsuranceFundEventHeader,
pub amount: u64,
pub success: bool,
}

#[event]
pub struct MarginfiWithdrawRequestLiquidInsuranceFundEvent {
pub header: LiquidInsuranceFundEventHeader,
pub amount: u64,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{
constants::LIQUID_INSURANCE_USER_SEED,
prelude::*,
state::{liquid_insurance_fund::LiquidInsuranceFundAccount, marginfi_group::Bank},
};
use anchor_lang::prelude::*;
use solana_program::sysvar::Sysvar;

pub fn initialize_account(ctx: Context<LiquidInsuranceFundAccountInitialize>) -> MarginfiResult {
let LiquidInsuranceFundAccountInitialize {
user_insurance_fund_account,
signer,
..
} = ctx.accounts;

let mut user_insurance_fund_account = ctx.accounts.user_insurance_fund_account.load_init()?;

user_insurance_fund_account.initialize(signer.key());

// TODO: Emit event

Ok(())
}

#[derive(Accounts)]
pub struct LiquidInsuranceFundAccountInitialize<'info> {
#[account(
init,
payer = signer,
space = 8 + std::mem::size_of::<LiquidInsuranceFundAccount>(),
seeds = [
LIQUID_INSURANCE_USER_SEED.as_bytes(),
signer.key().as_ref(),
],
bump
)]
pub user_insurance_fund_account: AccountLoader<'info, LiquidInsuranceFundAccount>,

#[account(mut)]
pub signer: Signer<'info>,

pub system_program: Program<'info, System>,
}
Loading
Loading