-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
disable tcs & allow purging open orders if conflicting with bank redu…
…ce_only setting
- Loading branch information
1 parent
8e8d91d
commit 031989d
Showing
6 changed files
with
96 additions
and
2 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
31 changes: 31 additions & 0 deletions
31
programs/mango-v4/src/accounts_ix/token_conditional_swap_purge.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,31 @@ | ||
use crate::error::*; | ||
use crate::state::*; | ||
use anchor_lang::prelude::*; | ||
|
||
#[derive(Accounts)] | ||
pub struct TokenConditionalSwapCancel<'info> { | ||
#[account( | ||
constraint = group.load()?.is_ix_enabled(IxGate::TokenConditionalSwapCancel) @ MangoError::IxIsDisabled, | ||
)] | ||
pub group: AccountLoader<'info, Group>, | ||
|
||
#[account( | ||
mut, | ||
has_one = group, | ||
constraint = account.load()?.is_operational() @ MangoError::AccountIsFrozen, | ||
// owner is not checked on purpose | ||
)] | ||
pub account: AccountLoader<'info, MangoAccountFixed>, | ||
|
||
/// The bank's token_index is checked at #1 | ||
#[account( | ||
mut, | ||
has_one = group, | ||
)] | ||
pub buy_bank: AccountLoader<'info, Bank>, | ||
#[account( | ||
mut, | ||
has_one = group, | ||
)] | ||
pub sell_bank: AccountLoader<'info, Bank>, | ||
} |
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
6 changes: 5 additions & 1 deletion
6
programs/mango-v4/src/instructions/token_conditional_swap_create.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
52 changes: 52 additions & 0 deletions
52
programs/mango-v4/src/instructions/token_conditional_swap_purge.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,52 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
use crate::accounts_ix::*; | ||
use crate::error::MangoError; | ||
use crate::logs::{emit_stack, TokenConditionalSwapCancelLog}; | ||
use crate::state::*; | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub fn token_conditional_swap_purge( | ||
ctx: Context<TokenConditionalSwapCancel>, | ||
token_conditional_swap_index: usize, | ||
token_conditional_swap_id: u64, | ||
) -> Result<()> { | ||
let mut buy_bank = ctx.accounts.buy_bank.load_mut()?; | ||
let mut sell_bank = ctx.accounts.sell_bank.load_mut()?; | ||
|
||
require!( | ||
buy_bank.are_deposits_reduce_only() || sell_bank.are_borrows_reduce_only(), | ||
MangoError::IxIsDisabled | ||
); | ||
|
||
let mut account = ctx.accounts.account.load_full_mut()?; | ||
let tcs = account.token_conditional_swap_mut_by_index(token_conditional_swap_index)?; | ||
require_eq!(tcs.buy_token_index, buy_bank.token_index); | ||
require_eq!(tcs.sell_token_index, sell_bank.token_index); | ||
|
||
// If the tcs is already inactive, this just is a noop | ||
if !tcs.is_configured() { | ||
return Ok(()); | ||
} | ||
|
||
require_eq!( | ||
tcs.id, | ||
token_conditional_swap_id, | ||
MangoError::TokenConditionalSwapIndexIdMismatch | ||
); | ||
*tcs = TokenConditionalSwap::default(); | ||
|
||
emit_stack(TokenConditionalSwapCancelLog { | ||
mango_group: ctx.accounts.group.key(), | ||
mango_account: ctx.accounts.account.key(), | ||
id: token_conditional_swap_id, | ||
}); | ||
|
||
let now_ts: u64 = Clock::get()?.unix_timestamp.try_into().unwrap(); | ||
|
||
// Free up any locks on token positions, possibly dust and deactivate them. | ||
account.token_decrement_dust_deactivate(&mut buy_bank, now_ts, ctx.accounts.account.key())?; | ||
account.token_decrement_dust_deactivate(&mut sell_bank, now_ts, ctx.accounts.account.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