diff --git a/liquidator/src/liquidate.rs b/liquidator/src/liquidate.rs index bed7be94f..eb3674d7b 100644 --- a/liquidator/src/liquidate.rs +++ b/liquidator/src/liquidate.rs @@ -111,9 +111,10 @@ pub fn maybe_liquidate_account( let health_cache = new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok"); 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); } @@ -132,7 +133,8 @@ pub fn maybe_liquidate_account( let health_cache = new_health_cache_(&mango_client.context, account_fetcher, &account).expect("always ok"); 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 let mut tokens = account @@ -205,7 +207,7 @@ pub fn maybe_liquidate_account( sig ); sig - } else if maint_health.is_negative() { + } else if is_liquidatable { let asset_token_index = tokens .iter() .rev() diff --git a/programs/mango-v4/src/state/health.rs b/programs/mango-v4/src/state/health.rs index 1767b1810..ced0fea69 100644 --- a/programs/mango-v4/src/state/health.rs +++ b/programs/mango-v4/src/state/health.rs @@ -523,6 +523,7 @@ pub struct HealthCache { token_infos: Vec, serum3_infos: Vec, perp_infos: Vec, + being_liquidated: bool, } impl HealthCache { @@ -566,6 +567,20 @@ impl HealthCache { 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)) { for token_info in self.token_infos.iter() { let contrib = token_info.health_contribution(health_type); @@ -922,6 +937,7 @@ pub fn new_health_cache( token_infos, serum3_infos, perp_infos, + being_liquidated: account.fixed.being_liquidated(), }) } @@ -1478,6 +1494,7 @@ mod tests { ], serum3_infos: vec![], perp_infos: vec![], + being_liquidated: false, }; assert_eq!(health_cache.health(HealthType::Init), I80F48::ZERO); diff --git a/ts/client/src/mango_v4.ts b/ts/client/src/mango_v4.ts index 38ef967d0..eedfe9eaa 100644 --- a/ts/client/src/mango_v4.ts +++ b/ts/client/src/mango_v4.ts @@ -3854,6 +3854,10 @@ export type MangoV4 = { "defined": "PerpInfo" } } + }, + { + "name": "beingLiquidated", + "type": "bool" } ] } @@ -8881,6 +8885,10 @@ export const IDL: MangoV4 = { "defined": "PerpInfo" } } + }, + { + "name": "beingLiquidated", + "type": "bool" } ] }