added socialize loss functionality

This commit is contained in:
dd 2021-04-04 15:53:21 -04:00
parent a52f7a1ca8
commit 31f8a54125
2 changed files with 20 additions and 2 deletions

View File

@ -21,7 +21,7 @@ use spl_token::state::{Account, Mint};
use crate::error::{check_assert, MangoErrorCode, MangoResult, SourceFileId, MangoError};
use crate::instruction::MangoInstruction;
use crate::state::{AccountFlag, check_open_orders, load_market_state, load_open_orders, Loadable, MangoGroup, MangoIndex, MangoSrmAccount, MarginAccount, NUM_MARKETS, NUM_TOKENS, ONE_U64F64, ZERO_U64F64, PARTIAL_LIQ_INCENTIVE};
use crate::state::{AccountFlag, check_open_orders, load_market_state, load_open_orders, Loadable, MangoGroup, MangoIndex, MangoSrmAccount, MarginAccount, NUM_MARKETS, NUM_TOKENS, ONE_U64F64, ZERO_U64F64, PARTIAL_LIQ_INCENTIVE, DUST_THRESHOLD};
use crate::utils::{gen_signer_key, gen_signer_seeds};
macro_rules! check_default {
@ -1441,9 +1441,26 @@ impl Processor {
if coll_ratio >= mango_group.init_coll_ratio {
// set margin account to no longer being liquidated
liqee_margin_account.being_liquidated = false;
} else {
// if all asset vals is dust (less than 1 cent?) socialize loss on lenders
let assets_val = liqee_margin_account.get_assets_val(&mango_group, &prices, open_orders_accs)?;
// TODO what to do in case account is fully empty but there are still borrows
if assets_val < DUST_THRESHOLD {
for i in 0..NUM_TOKENS {
let native_borrow = liqee_margin_account.get_native_borrow(&mango_group.indexes[i], i);
if native_borrow > 0 {
socialize_loss(
&mut mango_group,
&mut liqee_margin_account,
i,
U64F64::from_num(native_borrow))?;
}
}
}
}
// TODO do I need to check total deposits and total borrows?
// TODO what to do in case account is fully empty but there are still borrows
Ok(())
}

View File

@ -30,6 +30,7 @@ const MAX_R: U64F64 = U64F64!(3.17097919837645865e-08); // max 100% APY -> 1 / Y
pub const ONE_U64F64: U64F64 = U64F64!(1);
pub const ZERO_U64F64: U64F64 = U64F64!(0);
pub const PARTIAL_LIQ_INCENTIVE: U64F64 = U64F64!(1.05);
pub const DUST_THRESHOLD: U64F64 = U64F64!(0.01); // TODO make this part of MangoGroup state
macro_rules! check_default {
($cond:expr) => {