From 4fd8139854dde1e3e52cfe011832b2567355af03 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Tue, 24 Jan 2023 10:05:37 +0100 Subject: [PATCH] Token deposit: put expensive computation behind gates (#421) --- .../src/instructions/token_deposit.rs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/programs/mango-v4/src/instructions/token_deposit.rs b/programs/mango-v4/src/instructions/token_deposit.rs index 8796e6918..7e575ca67 100644 --- a/programs/mango-v4/src/instructions/token_deposit.rs +++ b/programs/mango-v4/src/instructions/token_deposit.rs @@ -188,13 +188,14 @@ impl<'a, 'info> DepositCommon<'a, 'info> { // let retriever = new_fixed_order_account_retriever(remaining_accounts, &account.borrow())?; let cache = new_health_cache(&account.borrow(), &retriever)?; - let health = cache.health(HealthType::Init); - msg!("health: {}", health); // Since depositing can only increase health, we can skip the usual pre-health computation. // Also, TokenDeposit is one of the rare instructions that is allowed even during being_liquidated. - // + // Being in a health region always means being_liquidated is false, so it's safe to gate the check. if !account.fixed.is_in_health_region() { + let health = cache.health(HealthType::Init); + msg!("health: {}", health); + let was_being_liquidated = account.being_liquidated(); let recovered = account.fixed.maybe_recover_from_being_liquidated(health); require!( @@ -204,14 +205,14 @@ impl<'a, 'info> DepositCommon<'a, 'info> { } // Group level deposit limit on account - let assets = cache - .health_assets_and_liabs(HealthType::Init) - .0 - .round_to_zero() - .checked_to_num::() - .unwrap(); let group = self.group.load()?; - if group.deposit_limit_quote > 0 && assets > group.deposit_limit_quote { + if group.deposit_limit_quote > 0 { + let assets = cache + .health_assets_and_liabs(HealthType::Init) + .0 + .round_to_zero() + .checked_to_num::() + .unwrap(); require_msg_typed!( assets <= group.deposit_limit_quote, MangoError::DepositLimit,