Skip to content

Commit

Permalink
Allow the insurance fund to be for any bank (#946)
Browse files Browse the repository at this point in the history
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com>
(cherry picked from commit ec2d10a)
  • Loading branch information
ckamm authored and farnyser committed Aug 9, 2024
1 parent e6c9a38 commit 98ed7ef
Show file tree
Hide file tree
Showing 19 changed files with 2,367 additions and 662 deletions.
21 changes: 13 additions & 8 deletions lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use mango_v4::accounts_zerocopy::KeyedAccountSharedData;
use mango_v4::health::HealthCache;
use mango_v4::state::{
Bank, Group, MangoAccountValue, OracleAccountInfos, PerpMarket, PerpMarketIndex,
PlaceOrderType, SelfTradeBehavior, Serum3MarketIndex, Side, TokenIndex, INSURANCE_TOKEN_INDEX,
PlaceOrderType, SelfTradeBehavior, Serum3MarketIndex, Side, TokenIndex,
};

use crate::confirm_transaction::{wait_for_transaction_confirmation, RpcConfirmTransactionConfig};
Expand Down Expand Up @@ -1763,13 +1763,13 @@ impl MangoClient {
let mango_account = &self.mango_account().await?;
let perp = self.context.perp(market_index);
let settle_token_info = self.context.token(perp.settle_token_index);
let insurance_token_info = self.context.token(INSURANCE_TOKEN_INDEX);
let insurance_token_info = self.context.token_by_mint(&group.insurance_mint)?;

let (health_remaining_ams, health_cu) = self
.derive_health_check_remaining_account_metas_two_accounts(
mango_account,
liqee.1,
&[INSURANCE_TOKEN_INDEX],
&[insurance_token_info.token_index],
&[],
)
.await
Expand Down Expand Up @@ -1917,10 +1917,15 @@ impl MangoClient {
liab_token_index: TokenIndex,
max_liab_transfer: I80F48,
) -> anyhow::Result<PreparedInstructions> {
let group = account_fetcher_fetch_anchor_account::<Group>(
&*self.account_fetcher,
&self.context.group,
)
.await?;

let mango_account = &self.mango_account().await?;
let quote_token_index = 0;

let quote_info = self.context.token(quote_token_index);
let insurance_info = self.context.token_by_mint(&group.insurance_mint)?;
let liab_info = self.context.token(liab_token_index);

let bank_remaining_ams = liab_info
Expand All @@ -1933,8 +1938,8 @@ impl MangoClient {
.derive_health_check_remaining_account_metas_two_accounts(
mango_account,
liqee.1,
&[INSURANCE_TOKEN_INDEX],
&[quote_token_index, liab_token_index],
&[insurance_info.token_index],
&[insurance_info.token_index, liab_token_index],
)
.await
.unwrap();
Expand All @@ -1955,7 +1960,7 @@ impl MangoClient {
liqor: self.mango_account_address,
liqor_owner: self.authority(),
liab_mint_info: liab_info.mint_info_address,
quote_vault: quote_info.first_vault(),
quote_vault: insurance_info.first_vault(),
insurance_vault: group.insurance_vault,
token_program: Token::id(),
},
Expand Down
83 changes: 83 additions & 0 deletions mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,86 @@
}
]
},
{
"name": "groupChangeInsuranceFund",
"accounts": [
{
"name": "group",
"isMut": true,
"isSigner": false,
"relations": [
"insurance_vault",
"admin"
]
},
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "insuranceVault",
"isMut": true,
"isSigner": false
},
{
"name": "withdrawDestination",
"isMut": true,
"isSigner": false
},
{
"name": "newInsuranceMint",
"isMut": false,
"isSigner": false
},
{
"name": "newInsuranceVault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "InsuranceVault"
},
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "account",
"type": "publicKey",
"account": "Mint",
"path": "new_insurance_mint"
}
]
}
},
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "ixGateSet",
"accounts": [
Expand Down Expand Up @@ -11120,6 +11200,9 @@
},
{
"name": "HealthCheck"
},
{
"name": "GroupChangeInsuranceFund"
}
]
}
Expand Down
66 changes: 66 additions & 0 deletions programs/mango-v4/src/accounts_ix/group_change_insurance_fund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{error::MangoError, state::*};
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Mint, Token, TokenAccount};

#[derive(Accounts)]
pub struct GroupChangeInsuranceFund<'info> {
#[account(
mut,
has_one = insurance_vault,
has_one = admin,
constraint = group.load()?.is_ix_enabled(IxGate::GroupChangeInsuranceFund) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,
pub admin: Signer<'info>,

#[account(
mut,
close = payer,
)]
pub insurance_vault: Account<'info, TokenAccount>,

#[account(mut)]
pub withdraw_destination: Account<'info, TokenAccount>,

pub new_insurance_mint: Account<'info, Mint>,

#[account(
init,
seeds = [b"InsuranceVault".as_ref(), group.key().as_ref(), new_insurance_mint.key().as_ref()],
bump,
token::authority = group,
token::mint = new_insurance_mint,
payer = payer
)]
pub new_insurance_vault: Account<'info, TokenAccount>,

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

pub token_program: Program<'info, Token>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
}

impl<'info> GroupChangeInsuranceFund<'info> {
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.insurance_vault.to_account_info(),
to: self.withdraw_destination.to_account_info(),
authority: self.group.to_account_info(),
};
CpiContext::new(program, accounts)
}

pub fn close_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::CloseAccount<'info>> {
CpiContext::new(
self.token_program.to_account_info(),
token::CloseAccount {
account: self.insurance_vault.to_account_info(),
destination: self.payer.to_account_info(),
authority: self.group.to_account_info(),
},
)
}
}
2 changes: 2 additions & 0 deletions programs/mango-v4/src/accounts_ix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use alt_set::*;
pub use benchmark::*;
pub use compute_account_data::*;
pub use flash_loan::*;
pub use group_change_insurance_fund::*;
pub use group_close::*;
pub use group_create::*;
pub use group_edit::*;
Expand Down Expand Up @@ -92,6 +93,7 @@ mod alt_set;
mod benchmark;
mod compute_account_data;
mod flash_loan;
mod group_change_insurance_fund;
mod group_close;
mod group_create;
mod group_edit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct PerpLiqNegativePnlOrBankruptcyV2<'info> {
#[account(
mut,
has_one = group,
constraint = insurance_bank.load()?.token_index == INSURANCE_TOKEN_INDEX
constraint = insurance_bank.load()?.mint == insurance_vault.mint,
)]
pub insurance_bank: AccountLoader<'info, Bank>,

Expand Down
2 changes: 1 addition & 1 deletion programs/mango-v4/src/accounts_ix/token_liq_bankruptcy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::state::*;

// Remaining accounts:
// - all banks for liab_mint_info (writable)
// - merged health accounts for liqor+liqee
// - merged health accounts for liqor + liqee, including the bank for the insurance token
#[derive(Accounts)]
pub struct TokenLiqBankruptcy<'info> {
#[account(
Expand Down
24 changes: 24 additions & 0 deletions programs/mango-v4/src/instructions/group_change_insurance_fund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use anchor_lang::prelude::*;
use anchor_spl::token;

use crate::{accounts_ix::GroupChangeInsuranceFund, group_seeds};

pub fn group_change_insurance_fund(ctx: Context<GroupChangeInsuranceFund>) -> Result<()> {
{
let group = ctx.accounts.group.load()?;
let group_seeds = group_seeds!(group);
token::transfer(
ctx.accounts.transfer_ctx().with_signer(&[group_seeds]),
ctx.accounts.insurance_vault.amount,
)?;
token::close_account(ctx.accounts.close_ctx().with_signer(&[group_seeds]))?;
}

{
let mut group = ctx.accounts.group.load_mut()?;
group.insurance_vault = ctx.accounts.new_insurance_vault.key();
group.insurance_mint = ctx.accounts.new_insurance_mint.key();
}

Ok(())
}
1 change: 1 addition & 0 deletions programs/mango-v4/src/instructions/ix_gate_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn ix_gate_set(ctx: Context<IxGateSet>, ix_gate: u128) -> Result<()> {
log_if_changed(&group, ix_gate, IxGate::TokenForceWithdraw);
log_if_changed(&group, ix_gate, IxGate::SequenceCheck);
log_if_changed(&group, ix_gate, IxGate::HealthCheck);
log_if_changed(&group, ix_gate, IxGate::GroupChangeInsuranceFund);

group.ix_gate = ix_gate;

Expand Down
2 changes: 2 additions & 0 deletions programs/mango-v4/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use alt_set::*;
pub use benchmark::*;
pub use compute_account_data::*;
pub use flash_loan::*;
pub use group_change_insurance_fund::*;
pub use group_close::*;
pub use group_create::*;
pub use group_edit::*;
Expand Down Expand Up @@ -83,6 +84,7 @@ mod alt_set;
mod benchmark;
mod compute_account_data;
mod flash_loan;
mod group_change_insurance_fund;
mod group_close;
mod group_create;
mod group_edit;
Expand Down
Loading

0 comments on commit 98ed7ef

Please sign in to comment.