Liquidator: Fix condition for starting to liquidate

This commit is contained in:
Christian Kamm 2022-08-16 13:37:42 +02:00
parent 421b2d3d19
commit 4c65204c19
3 changed files with 31 additions and 4 deletions

View File

@ -111,9 +111,10 @@ pub fn maybe_liquidate_account(
let health_cache = let health_cache =
new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok"); new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok");
let maint_health = health_cache.health(HealthType::Maint); let maint_health = health_cache.health(HealthType::Maint);
let is_bankrupt = !health_cache.has_liquidatable_assets() && health_cache.has_borrows(); let is_bankrupt = health_cache.is_bankrupt();
let is_liquidatable = health_cache.is_liquidatable();
if maint_health >= 0 && !is_bankrupt { if !is_liquidatable && !is_bankrupt {
return Ok(false); return Ok(false);
} }
@ -132,7 +133,8 @@ pub fn maybe_liquidate_account(
let health_cache = let health_cache =
new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok"); new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok");
let maint_health = health_cache.health(HealthType::Maint); let maint_health = health_cache.health(HealthType::Maint);
let is_bankrupt = !health_cache.has_liquidatable_assets() && health_cache.has_borrows(); let is_bankrupt = health_cache.is_bankrupt();
let is_liquidatable = health_cache.is_liquidatable();
// find asset and liab tokens // find asset and liab tokens
let mut tokens = account let mut tokens = account
@ -205,7 +207,7 @@ pub fn maybe_liquidate_account(
sig sig
); );
sig sig
} else if maint_health.is_negative() { } else if is_liquidatable {
let asset_token_index = tokens let asset_token_index = tokens
.iter() .iter()
.rev() .rev()

View File

@ -523,6 +523,7 @@ pub struct HealthCache {
token_infos: Vec<TokenInfo>, token_infos: Vec<TokenInfo>,
serum3_infos: Vec<Serum3Info>, serum3_infos: Vec<Serum3Info>,
perp_infos: Vec<PerpInfo>, perp_infos: Vec<PerpInfo>,
being_liquidated: bool,
} }
impl HealthCache { impl HealthCache {
@ -566,6 +567,20 @@ impl HealthCache {
spot_borrows || perp_borrows spot_borrows || perp_borrows
} }
#[cfg(feature = "client")]
pub fn is_bankrupt(&self) -> bool {
!self.has_liquidatable_assets() && self.has_borrows()
}
#[cfg(feature = "client")]
pub fn is_liquidatable(&self) -> bool {
if self.being_liquidated {
self.health(HealthType::Init).is_negative()
} else {
self.health(HealthType::Maint).is_negative()
}
}
fn health_sum(&self, health_type: HealthType, mut action: impl FnMut(I80F48)) { fn health_sum(&self, health_type: HealthType, mut action: impl FnMut(I80F48)) {
for token_info in self.token_infos.iter() { for token_info in self.token_infos.iter() {
let contrib = token_info.health_contribution(health_type); let contrib = token_info.health_contribution(health_type);
@ -922,6 +937,7 @@ pub fn new_health_cache(
token_infos, token_infos,
serum3_infos, serum3_infos,
perp_infos, perp_infos,
being_liquidated: account.fixed.being_liquidated(),
}) })
} }
@ -1478,6 +1494,7 @@ mod tests {
], ],
serum3_infos: vec![], serum3_infos: vec![],
perp_infos: vec![], perp_infos: vec![],
being_liquidated: false,
}; };
assert_eq!(health_cache.health(HealthType::Init), I80F48::ZERO); assert_eq!(health_cache.health(HealthType::Init), I80F48::ZERO);

View File

@ -3854,6 +3854,10 @@ export type MangoV4 = {
"defined": "PerpInfo" "defined": "PerpInfo"
} }
} }
},
{
"name": "beingLiquidated",
"type": "bool"
} }
] ]
} }
@ -8881,6 +8885,10 @@ export const IDL: MangoV4 = {
"defined": "PerpInfo" "defined": "PerpInfo"
} }
} }
},
{
"name": "beingLiquidated",
"type": "bool"
} }
] ]
} }