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

Cavey/lif #215

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b35899d
feat: Liquid Insurance Funds
exdx May 1, 2024
e072543
fix: Remove marginfi account pubkey
exdx May 23, 2024
66e1dd4
fix: Rename authority to admin; update shares to WrappedI80F48
exdx May 23, 2024
47308c7
fix: Update to use request timestamp at the time of the request
exdx May 23, 2024
e8ec7ec
fix: Remove lif token mint functionality entirely
exdx May 24, 2024
536ad42
fix: Remove unnecessary fields from lif
exdx May 28, 2024
450546c
fix: Use lif seed in state; remove other mint seeds
exdx May 28, 2024
48b4b71
fix: Use shares explicitly when depositing, add withdraw params to
exdx May 28, 2024
062391b
fix: Remove extra signers; fix withdrawals to use amount instead of
exdx May 29, 2024
9da4479
fix: Clean up withdraw to use amount instead of shares
exdx May 29, 2024
06ce16a
fix: Use anchor close macro on withdraw
exdx May 29, 2024
da2861f
fix: Return Result<> when creating a fund
exdx May 29, 2024
a4e0323
wip: Internal accounting model
exdx May 30, 2024
6f80884
fix: Use correct deposit seeds
exdx May 30, 2024
a8f5dab
feat: Add haircut_shares to reprice lif shares in case of bankruptcy or
exdx May 30, 2024
3477229
fix: Move InsuranceFundAccount to state
exdx May 30, 2024
0b50360
wip: Move lif module up one level; add account based accounting
exdx May 31, 2024
a2c90ec
feat: Add user deposit accounting
exdx Jun 2, 2024
5b18378
wip: Create withdraw address
exdx Jun 3, 2024
876e8ed
deposit/withdraw accounting
cavemanloverboy Jun 6, 2024
7af12e5
initial lif implementation with tests
cavemanloverboy Jun 12, 2024
e82b743
clarify lazy evaluation
cavemanloverboy Jun 12, 2024
cf7c5f2
unnecessary pubkey comparison
cavemanloverboy Jun 12, 2024
b7b793e
revert unnecessary deser
cavemanloverboy Jun 12, 2024
d33b8d1
Merge branch 'main' into cavey/lif
cavemanloverboy Jun 13, 2024
6e9d575
Merge branch 'main' into cavey/lif
jkbpvsc Jul 1, 2024
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
133 changes: 131 additions & 2 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 = "4.0"

bytemuck = "1.9.1"
cfg-if = "1.0.0"
enum_dispatch = "0.3.11"
Expand Down
3 changes: 3 additions & 0 deletions programs/marginfi/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ 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_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
6 changes: 6 additions & 0 deletions programs/marginfi/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ pub enum MarginfiError {
IllegalAccountAuthorityTransfer,
#[msg("Unauthorized")] // 6045
Unauthorized,
#[msg("InvalidWithdrawal")] // 6046
InvalidWithdrawal,
#[msg("Insurance fund account balance slots are full")] // 6047
InsuranceFundAccountBalanceSlotsFull,
#[msg("Insurance fund account withdraw slots are full")] // 6048
InsuranceFundAccountWithdrawSlotsFull,
}

impl From<MarginfiError> for ProgramError {
Expand Down
37 changes: 37 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,35 @@ 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 MarginfiCreateNewLiquidInsuranceFundAccountEvent {
pub user: Pubkey,
}

#[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 shares: f64,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{
constants::LIQUID_INSURANCE_USER_SEED,
events::MarginfiCreateNewLiquidInsuranceFundAccountEvent, prelude::*,
state::liquid_insurance_fund::LiquidInsuranceFundAccount,
};
use anchor_lang::prelude::*;
use solana_program::sysvar::Sysvar;

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

let mut user_insurance_fund_account = user_insurance_fund_account.load_init()?;

user_insurance_fund_account.initialize(signer.key());

emit!(MarginfiCreateNewLiquidInsuranceFundAccountEvent { user: signer.key() });

Ok(())
}

#[derive(Accounts)]
pub struct CreateLiquidInsuranceFundAccount<'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