Skip to content

Commit

Permalink
Refactor HLS collaterals.
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab committed Jun 14, 2024
1 parent 56c1a19 commit b42c1b5
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions packages/health-computer/src/health_computer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,41 +529,11 @@ impl HealthComputer {
},
liquidation_threshold,
..
}) = self.denoms_data.params.get(&c.denom)
}) = self.coin_contribute_to_collateral(c)?
else {
// If the coin is not found (whitelisted), it is not considered for collateral
continue;
};

match self.kind {
AccountKind::HighLeveredStrategy => {
// HLS should have 0 or 1 debt denom in the account
if let Some(debt) = self.positions.debts.first() {
let debt_params = self
.denoms_data
.params
.get(&debt.denom)
.ok_or(MissingParams(debt.denom.clone()))?;
let debt_hls = debt_params
.credit_manager
.hls
.as_ref()
.ok_or(MissingHLSParams(debt.denom.clone()))?;
if !debt_hls.correlations.contains(&HlsAssetType::Coin {
denom: c.denom.clone(),
}) {
continue;
}
} else if hls.is_none() {
continue;
}
}
AccountKind::Default => {}
AccountKind::FundManager {
..
} => {}
}

let coin_price =
self.denoms_data.prices.get(&c.denom).ok_or(MissingPrice(c.denom.clone()))?;
let coin_value = c.amount.checked_mul_floor(*coin_price)?;
Expand Down Expand Up @@ -595,6 +565,55 @@ impl HealthComputer {
})
}

fn coin_contribute_to_collateral(&self, coin: &Coin) -> HealthResult<Option<&AssetParams>> {
let Some(asset_params) = self.denoms_data.params.get(&coin.denom) else {
// If the coin is not found (whitelisted), it is not considered for collateral
return Ok(None);
};

match self.kind {
AccountKind::HighLeveredStrategy => {
// HLS should have 0 or 1 debt denom in the account. If there are more than 1 we can safely calculate the collateral value
// because the rule will be checked in the Credit Manager contract and won't allow more than 1 debt denom in the account.
if !self.positions.debts.is_empty() {
let mut correlations = vec![];
for debt in self.positions.debts.iter() {
let debt_params = self
.denoms_data
.params
.get(&debt.denom)
.ok_or(MissingParams(debt.denom.clone()))?;
let debt_hls = debt_params
.credit_manager
.hls
.as_ref()
.ok_or(MissingHLSParams(debt.denom.clone()))?;

// collect all the correlations of the debts
correlations.extend(&debt_hls.correlations);
}

// If the collateral is not correlated with any of the debts, skip it.
// It doesn't contribute to the collateral value.
if !correlations.contains(&&HlsAssetType::Coin {
denom: coin.denom.clone(),
}) {
return Ok(None);
}
} else if asset_params.credit_manager.hls.is_none() {
// Only collateral with hls params can be used in an HLS account and can contribute to the collateral value
return Ok(None);

Check warning on line 605 in packages/health-computer/src/health_computer.rs

View check run for this annotation

Codecov / codecov/patch

packages/health-computer/src/health_computer.rs#L605

Added line #L605 was not covered by tests
}
}
AccountKind::Default => {}
AccountKind::FundManager {
..
} => {}
}

Ok(Some(asset_params))
}

fn vaults_value(&self) -> HealthResult<CollateralValue> {
let mut total_collateral_value = Uint128::zero();
let mut max_ltv_adjusted_collateral = Uint128::zero();
Expand Down

0 comments on commit b42c1b5

Please sign in to comment.