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

Last updated

Logo

This documentation is provided by MorphoLabs, main contributor to Morpho DAO. For any question, reach out.