Add is_bankrupt check to most instructions

This commit is contained in:
Christian Kamm 2022-03-30 10:00:52 +02:00
parent 00ac0dfce9
commit babbff6bec
11 changed files with 22 additions and 2 deletions

View File

@ -16,4 +16,6 @@ pub enum MangoError {
InvalidMarginTradeTargetCpiProgram,
#[msg("")]
HealthMustBePositive,
#[msg("The account is bankrupt")]
IsBankrupt,
}

View File

@ -4,6 +4,7 @@ use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use fixed::types::I80F48;
use crate::error::*;
use crate::state::*;
#[derive(Accounts)]
@ -55,6 +56,8 @@ pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
// Get the account's position for that token index
let mut account = ctx.accounts.account.load_mut()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let (position, position_index) = account.token_account_map.get_mut_or_create(token_index)?;
// Update the bank and position

View File

@ -38,7 +38,10 @@ pub fn liq_token_with_token(
let account_retriever = ScanningAccountRetriever::new(ctx.remaining_accounts, group_pk)?;
let mut liqor = ctx.accounts.liqor.load_mut()?;
require!(!liqor.is_bankrupt, MangoError::IsBankrupt);
let mut liqee = ctx.accounts.liqee.load_mut()?;
require!(!liqee.is_bankrupt, MangoError::IsBankrupt);
// Initial liqee health check
let mut liqee_health_cache = health_cache_for_liqee(&liqee, &account_retriever)?;

View File

@ -27,6 +27,7 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>(
) -> Result<()> {
let group = ctx.accounts.group.load()?;
let mut account = ctx.accounts.account.load_mut()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
// remaining_accounts layout is expected as follows
// * banks_len number of banks

View File

@ -1,5 +1,6 @@
use anchor_lang::prelude::*;
use crate::error::*;
use crate::state::{
oracle_price, Book, EventQueueHeader, Group, MangoAccount, OrderType, PerpMarket, Queue, Side,
};
@ -49,6 +50,7 @@ pub fn place_perp_order(
limit: u8,
) -> Result<()> {
let mut mango_account = ctx.accounts.account.load_mut()?;
require!(!mango_account.is_bankrupt, MangoError::IsBankrupt);
let mango_account_pk = ctx.accounts.account.key();
let mut perp_market = ctx.accounts.perp_market.load_mut()?;

View File

@ -95,6 +95,8 @@ pub fn serum3_cancel_order(
//
{
let account = ctx.accounts.account.load()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let serum_market = ctx.accounts.serum_market.load()?;
// Validate open_orders

View File

@ -1,5 +1,6 @@
use anchor_lang::prelude::*;
use crate::error::*;
use crate::state::*;
#[derive(Accounts)]
@ -51,6 +52,7 @@ pub fn serum3_create_open_orders(ctx: Context<Serum3CreateOpenOrders>) -> Result
let serum_market = ctx.accounts.serum_market.load()?;
let mut account = ctx.accounts.account.load_mut()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let serum_account = account
.serum3_account_map
.create(serum_market.market_index)?;

View File

@ -65,6 +65,7 @@ pub fn serum3_liq_force_cancel_orders(
//
{
let account = ctx.accounts.account.load()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let serum_market = ctx.accounts.serum_market.load()?;
// Validate open_orders

View File

@ -157,6 +157,7 @@ pub fn serum3_place_order(
//
{
let account = ctx.accounts.account.load()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let serum_market = ctx.accounts.serum_market.load()?;
// Validate open_orders
@ -241,7 +242,7 @@ pub fn serum3_place_order(
let health =
compute_health_from_fixed_accounts(&account, HealthType::Init, &ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);
require!(health >= 0, MangoError::HealthMustBePositive);
Ok(())
}

View File

@ -64,6 +64,7 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
//
{
let account = ctx.accounts.account.load()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let serum_market = ctx.accounts.serum_market.load()?;
// Validate open_orders

View File

@ -58,6 +58,8 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
// Get the account's position for that token index
let mut account = ctx.accounts.account.load_mut()?;
require!(!account.is_bankrupt, MangoError::IsBankrupt);
let (position, position_index) = account.token_account_map.get_mut_or_create(token_index)?;
// The bank will also be passed in remainingAccounts. Use an explicit scope
@ -103,7 +105,7 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
let health =
compute_health_from_fixed_accounts(&account, HealthType::Init, &ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);
require!(health >= 0, MangoError::HealthMustBePositive);
//
// Deactivate the position only after the health check because the user passed in