Skip to content

Instantly share code, notes, and snippets.

@sekrystal
Created March 20, 2023 18:42
Show Gist options
  • Save sekrystal/11ccda0a6f08a23f12ee38f985cb273f to your computer and use it in GitHub Desktop.
Save sekrystal/11ccda0a6f08a23f12ee38f985cb273f to your computer and use it in GitHub Desktop.
Defi Lending Concepts 2
pragma solidity ^0.8.13;
vars.healthFactor = calculateHealthFactorFromBalances(
vars.totalCollateralInETH,
vars.totalDebtInETH,
vars.avgLiquidationThreshold
);
// ...
/**
* @dev Calculates the health factor from the corresponding balances
* @param totalCollateralInETH The total collateral in ETH
* @param totalDebtInETH The total debt in ETH
* @param liquidationThreshold The avg liquidation threshold
* @return The health factor calculated from the balances provided
**/
function calculateHealthFactorFromBalances(
uint256 totalCollateralInETH,
uint256 totalDebtInETH,
uint256 liquidationThreshold
) internal pure returns (uint256) {
if (totalDebtInETH == 0) return uint256(-1);
return (totalCollateralInETH.percentMul(liquidationThreshold)).wadDiv(totalDebtInETH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment