From ded7ec3b7fc77c7a7026fd221786a1f97735d6b1 Mon Sep 17 00:00:00 2001 From: jjjj Date: Mon, 31 Jul 2023 22:50:37 +0200 Subject: [PATCH] fix: asset price oracle bounds --- programs/marginfi/src/state/price.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/programs/marginfi/src/state/price.rs b/programs/marginfi/src/state/price.rs index 3b290788..584f9ff0 100644 --- a/programs/marginfi/src/state/price.rs +++ b/programs/marginfi/src/state/price.rs @@ -135,11 +135,17 @@ impl PriceAdapter for PythEmaPriceFeed { } fn get_confidence_interval(&self) -> MarginfiResult { - Ok( + let conf_interval = pyth_price_components_to_i80f48(I80F48::from_num(self.price.conf), self.price.expo)? .checked_mul(CONF_INTERVAL_MULTIPLE) - .ok_or_else(math_error!())?, - ) + .ok_or_else(math_error!())?; + + assert!( + conf_interval >= I80F48::ZERO, + "Negative confidence interval" + ); + + Ok(conf_interval) } fn get_price_range(&self) -> MarginfiResult<(I80F48, I80F48)> { @@ -217,9 +223,16 @@ impl PriceAdapter for SwitchboardV2PriceFeed { let std_div = swithcboard_decimal_to_i80f48(std_div) .ok_or(MarginfiError::InvalidSwitchboardDecimalConversion)?; - Ok(std_div + let conf_interval = std_div .checked_mul(CONF_INTERVAL_MULTIPLE) - .ok_or_else(math_error!())?) + .ok_or_else(math_error!())?; + + assert!( + conf_interval >= I80F48::ZERO, + "Negative confidence interval" + ); + + Ok(conf_interval) } fn get_price_range(&self) -> MarginfiResult<(I80F48, I80F48)> {