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

Allow the insurance fund to be for any bank #946

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 13 additions & 14 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 @@ -1797,13 +1797,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 @@ -1951,10 +1951,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 @@ -1967,18 +1972,12 @@ 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();

let group = account_fetcher_fetch_anchor_account::<Group>(
&*self.account_fetcher,
&self.context.group,
)
.await?;

let ix = Instruction {
program_id: mango_v4::id(),
accounts: {
Expand All @@ -1989,7 +1988,7 @@ impl MangoClient {
liqor: self.mango_account_address,
liqor_owner: self.owner(),
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 @@ -11090,6 +11170,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
Loading