Track Health Factor
Learn how to track users health factors on Morpho, on-chain via a Smart Contract & off-chain via ethers.js.
Morpho-Aave
/// IMPORTS ///
import {Types} from "@morpho-aave-v3/libraries/Types.sol";
import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";
/// FUNCTION ///
contract Snippets {
using MarketLib for Types.Market;
IMorpho public immutable morpho;
constructor(address morphoAddress) {
morpho = IMorpho(morphoAddress);
}
/// @notice Returns the health factor of a given user.
/// @param user The user of whom to get the health factor.
/// @return The health factor of the given user (in wad).
function healthFactor(address user) public view returns (uint256) {
Types.LiquidityData memory liquidityData = morpho.liquidityData(user);
return liquidityData.debt > 0 ? liquidityData.maxDebt.wadDiv(liquidityData.debt) : type(uint256).max;
}
/// IMPORTS ///
import { BigNumber, providers } from "ethers";
import { PercentMath, WadRayMath } from "@morpho-labs/ethers-utils/lib/maths";
/// FUNCTION ///
/**
* This function compute the health factor on a specific user and returns the result.
*
* @param user The user address.
* @param provider A provider instance
*
* @returns The health factor in _WAD_ units.
*/
export const getUserHealthFactor = async (user: string, provider: providers.BaseProvider) => {
const { morphoAaveV3 } = getContracts(provider);
const { debt, maxDebt } = await morphoAaveV3.liquidityData(user);
return WadRayMath.wadDiv(maxDebt, debt);
};
/// FUNCTION ///
const lens = new ethers.Contract("0x507fA343d0A90786d86C7cd885f5C49263A91FF", LensAbi, signer);
/**
* This function compute the health factor on a specific user and returns the result.
*
* @param userAddress The user address.
* @param provider A provider instance
*
* @returns The health factor in _WAD_ units.
*/
async function getUserHealthFactor(userAddress) {
const hf = await lens.getUserHealthFactor(userAddress);
return hf;
}
Last updated