insurance fund withdraw ix (#561)

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2023-04-25 09:20:44 +02:00 committed by GitHub
parent 2305a160d0
commit 3741f78da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 2 deletions

View File

@ -0,0 +1,35 @@
use crate::{error::MangoError, state::*};
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount};
#[derive(Accounts)]
pub struct GroupWithdrawInsuranceFund<'info> {
#[account(
mut,
has_one = insurance_vault,
has_one = admin,
constraint = group.load()?.is_ix_enabled(IxGate::GroupWithdrawInsuranceFund) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,
pub admin: Signer<'info>,
#[account(mut)]
pub insurance_vault: Account<'info, TokenAccount>,
#[account(mut)]
pub destination: Account<'info, TokenAccount>,
pub token_program: Program<'info, Token>,
}
impl<'info> GroupWithdrawInsuranceFund<'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.destination.to_account_info(),
authority: self.group.to_account_info(),
};
CpiContext::new(program, accounts)
}
}

View File

@ -12,6 +12,7 @@ pub use flash_loan::*;
pub use group_close::*;
pub use group_create::*;
pub use group_edit::*;
pub use group_withdraw_insurance_fund::*;
pub use health_region::*;
pub use ix_gate_set::*;
pub use perp_cancel_all_orders::*;
@ -70,6 +71,7 @@ mod flash_loan;
mod group_close;
mod group_create;
mod group_edit;
mod group_withdraw_insurance_fund;
mod health_region;
mod ix_gate_set;
mod perp_cancel_all_orders;

View File

@ -0,0 +1,19 @@
use anchor_lang::prelude::*;
use anchor_spl::token;
use crate::{accounts_ix::GroupWithdrawInsuranceFund, group_seeds};
pub fn group_withdraw_insurance_fund(
ctx: Context<GroupWithdrawInsuranceFund>,
amount: u64,
) -> Result<()> {
let group = ctx.accounts.group.load()?;
let group_seeds = group_seeds!(group);
token::transfer(
ctx.accounts.transfer_ctx().with_signer(&[group_seeds]),
amount.min(ctx.accounts.insurance_vault.amount),
)?;
Ok(())
}

View File

@ -67,6 +67,7 @@ pub fn ix_gate_set(ctx: Context<IxGateSet>, ix_gate: u128) -> Result<()> {
log_if_changed(&group, ix_gate, IxGate::AccountBuybackFeesWithMngo);
log_if_changed(&group, ix_gate, IxGate::TokenForceCloseBorrowsWithToken);
log_if_changed(&group, ix_gate, IxGate::PerpForceClosePosition);
log_if_changed(&group, ix_gate, IxGate::GroupWithdrawInsuranceFund);
group.ix_gate = ix_gate;

View File

@ -12,6 +12,7 @@ pub use flash_loan::*;
pub use group_close::*;
pub use group_create::*;
pub use group_edit::*;
pub use group_withdraw_insurance_fund::*;
pub use health_region::*;
pub use ix_gate_set::*;
pub use perp_cancel_all_orders::*;
@ -70,6 +71,7 @@ mod flash_loan;
mod group_close;
mod group_create;
mod group_edit;
mod group_withdraw_insurance_fund;
mod health_region;
mod ix_gate_set;
mod perp_cancel_all_orders;

View File

@ -85,6 +85,14 @@ pub mod mango_v4 {
Ok(())
}
pub fn group_withdraw_insurance_fund(
ctx: Context<GroupWithdrawInsuranceFund>,
amount: u64,
) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::group_withdraw_insurance_fund(ctx, amount)
}
pub fn ix_gate_set(ctx: Context<IxGateSet>, ix_gate: u128) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::ix_gate_set(ctx, ix_gate)?;

View File

@ -189,6 +189,7 @@ pub enum IxGate {
AccountBuybackFeesWithMngo = 48,
TokenForceCloseBorrowsWithToken = 49,
PerpForceClosePosition = 50,
GroupWithdrawInsuranceFund = 51,
// NOTE: Adding new variants requires matching changes in ts and the ix_gate_set instruction.
}

View File

@ -236,6 +236,7 @@ export const TrueIxGateParams: IxGateParams = {
AccountBuybackFeesWithMngo: true,
TokenForceCloseBorrowsWithToken: true,
PerpForceClosePosition: true,
GroupWithdrawInsuranceFund: true,
};
// build ix gate e.g. buildIxGate(Builder(TrueIxGateParams).TokenDeposit(false).build()).toNumber(),
@ -303,8 +304,8 @@ export function buildIxGate(p: IxGateParams): BN {
toggleIx(ixGate, p, 'TokenWithdraw', 47);
toggleIx(ixGate, p, 'AccountBuybackFeesWithMngo', 48);
toggleIx(ixGate, p, 'TokenForceCloseBorrowsWithToken', 49);
toggleIx(ixGate, p, 'TokenForceCloseBorrowsWithToken', 49);
toggleIx(ixGate, p, 'PerpForceClosePosition', 49);
toggleIx(ixGate, p, 'PerpForceClosePosition', 50);
toggleIx(ixGate, p, 'GroupWithdrawInsuranceFund', 51);
return ixGate;
}