Skip to main content

Track Health Factor

Learn how to track users health factors on Morpho, onchain via a smart contract & offchain via ethers.js.

Morpho Aave Optimizer Instances

/// 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;
}