-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the insurance fund to be for any bank (#946)
Co-authored-by: microwavedcola1 <microwavedcola@gmail.com> (cherry picked from commit ec2d10a)
- Loading branch information
Showing
19 changed files
with
2,367 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
programs/mango-v4/src/accounts_ix/group_change_insurance_fund.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
programs/mango-v4/src/instructions/group_change_insurance_fund.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.