Skip to content

Commit

Permalink
fix: price liquidations with price bias
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbpvsc committed Nov 1, 2023
1 parent 74737e0 commit 8ffa916
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn lending_account_liquidate(
current_timestamp,
MAX_PRICE_AGE_SEC,
)?;
asset_pf.get_price()?
asset_pf.get_price_with_lower_bias()?
};

let mut liab_bank = ctx.accounts.liab_bank.load_mut()?;
Expand All @@ -131,7 +131,7 @@ pub fn lending_account_liquidate(
MAX_PRICE_AGE_SEC,
)?;

liab_pf.get_price()?
liab_pf.get_price_with_higher_bias()?
};

let final_discount = I80F48::ONE - (LIQUIDATION_INSURANCE_FEE + LIQUIDATION_LIQUIDATOR_FEE);
Expand Down
42 changes: 42 additions & 0 deletions programs/marginfi/src/state/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub trait PriceAdapter {
/// Get a normalized price range for the given price feed.
/// The range is the price +/- the CONF_INTERVAL_MULTIPLE * confidence interval.
fn get_price_range(&self) -> MarginfiResult<(I80F48, I80F48)>;
fn get_price_with_lower_bias(&self) -> MarginfiResult<I80F48>;
fn get_price_with_higher_bias(&self) -> MarginfiResult<I80F48>;
}

#[enum_dispatch(PriceAdapter)]
Expand Down Expand Up @@ -161,6 +163,24 @@ impl PriceAdapter for PythEmaPriceFeed {

Ok((lowest_price, highest_price))
}

fn get_price_with_lower_bias(&self) -> MarginfiResult<I80F48> {
let price = self.get_price()?;
let conf_interval = self.get_confidence_interval()?;

let price = price.checked_sub(conf_interval).ok_or_else(math_error!())?;

Ok(price)
}

fn get_price_with_higher_bias(&self) -> MarginfiResult<I80F48> {
let price = self.get_price()?;
let conf_interval = self.get_confidence_interval()?;

let price = price.checked_add(conf_interval).ok_or_else(math_error!())?;

Ok(price)
}
}

pub struct SwitchboardV2PriceFeed {
Expand Down Expand Up @@ -248,6 +268,28 @@ impl PriceAdapter for SwitchboardV2PriceFeed {

Ok((lowest_price, highest_price))
}

fn get_price_with_lower_bias(&self) -> MarginfiResult<I80F48> {
let base_price = self.get_price()?;
let price_range = self.get_confidence_interval()?;

let lowest_price = base_price
.checked_sub(price_range)
.ok_or_else(math_error!())?;

Ok(lowest_price)
}

fn get_price_with_higher_bias(&self) -> MarginfiResult<I80F48> {
let base_price = self.get_price()?;
let price_range = self.get_confidence_interval()?;

let highest_price = base_price
.checked_add(price_range)
.ok_or_else(math_error!())?;

Ok(highest_price)
}
}

/// A slimmed down version of the AggregatorAccountData struct copied from the switchboard-v2/src/aggregator.rs
Expand Down

0 comments on commit 8ffa916

Please sign in to comment.