Simplify health fn call, use in place serum order

This commit is contained in:
Christian Kamm 2022-03-12 14:12:37 +01:00
parent 16c0a95679
commit fce2316b03
5 changed files with 27 additions and 29 deletions

View File

@ -3,7 +3,6 @@ use anchor_spl::token;
use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use crate::error::*;
use crate::state::*;
#[derive(Accounts)]
@ -73,16 +72,7 @@ pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
// TODO: This will be used to disable is_bankrupt or being_liquidated
// when health recovers sufficiently
//
let active_len = account.indexed_positions.iter_active().count();
require!(
ctx.remaining_accounts.len() == active_len * 2, // banks + oracles
MangoError::SomeError
);
let banks = &ctx.remaining_accounts[0..active_len];
let oracles = &ctx.remaining_accounts[active_len..active_len * 2];
let health = compute_health(&mut account, &banks, &oracles)?;
let health = compute_health(&account, &ctx.remaining_accounts)?;
msg!("health: {}", health);
//

View File

@ -43,8 +43,9 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>(
);
// unpack remaining_accounts
let health_ais = &ctx.remaining_accounts[0..banks_len * 2];
// TODO: This relies on the particular shape of health_ais
let banks = &ctx.remaining_accounts[0..banks_len];
let oracles = &ctx.remaining_accounts[banks_len..banks_len * 2];
let cpi_program_id = *ctx.remaining_accounts[banks_len * 2].key;
// prepare account for cpi ix
@ -94,7 +95,7 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>(
}
// compute pre cpi health
let pre_cpi_health = compute_health(&mut account, &banks, &oracles)?;
let pre_cpi_health = compute_health(&account, health_ais)?;
require!(pre_cpi_health > 0, MangoError::HealthMustBePositive);
msg!("pre_cpi_health {:?}", pre_cpi_health);
@ -119,7 +120,7 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>(
// compute post cpi health
// todo: this is not working, the health is computed on old bank state and not taking into account
// withdraws done in adjust_for_post_cpi_token_amounts
let post_cpi_health = compute_health(&account, &banks, &oracles)?;
let post_cpi_health = compute_health(&account, health_ais)?;
require!(post_cpi_health > 0, MangoError::HealthMustBePositive);
msg!("post_cpi_health {:?}", post_cpi_health);

View File

@ -3,6 +3,7 @@ use anchor_spl::dex;
use anchor_spl::token::{Token, TokenAccount};
use dex::serum_dex;
use crate::error::*;
use crate::state::*;
#[derive(Accounts)]
@ -98,7 +99,11 @@ pub fn place_serum_order(
order.limit,
)?;
// TODO: health check
// Health check
let account = ctx.accounts.account.load()?;
let health = compute_health(&account, &ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);
Ok(())
}

View File

@ -100,16 +100,7 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
//
// Health check
//
let active_len = account.indexed_positions.iter_active().count();
require!(
ctx.remaining_accounts.len() == active_len * 2, // banks + oracles
MangoError::SomeError
);
let banks = &ctx.remaining_accounts[0..active_len];
let oracles = &ctx.remaining_accounts[active_len..active_len * 2];
let health = compute_health(&mut account, &banks, &oracles)?;
let health = compute_health(&account, &ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);

View File

@ -1,5 +1,3 @@
use std::cell::RefMut;
use anchor_lang::prelude::*;
use fixed::types::I80F48;
use pyth_client::load_price;
@ -8,8 +6,21 @@ use crate::error::MangoError;
use crate::state::{determine_oracle_type, Bank, MangoAccount, OracleType, StubOracle};
use crate::util;
pub fn compute_health(
account: &RefMut<MangoAccount>,
pub fn compute_health(account: &MangoAccount, ais: &[AccountInfo]) -> Result<I80F48> {
let active_len = account.indexed_positions.iter_active().count();
require!(
ais.len() == active_len * 2, // banks + oracles
MangoError::SomeError
);
let banks = &ais[0..active_len];
let oracles = &ais[active_len..active_len * 2];
compute_health_detail(account, banks, oracles)
}
fn compute_health_detail(
account: &MangoAccount,
banks: &[AccountInfo],
oracles: &[AccountInfo],
) -> Result<I80F48> {