Morpho User Health Factor
Be notified if your position's health factor in Morpho is lower than a predefined threshold.
This Gatelang computes a user's health factor in Morpho. The inputs (or parameters) to this Gatelang is the market id, the user's address and the threshold - this will alert if the healthfactor is lower or equals than the supplied threshold.
use Call from hexagate;
source marketId: bytes = bytes(0x5E3E6B1E01C5708055548D82D01DB741E37D03B948A7EF9F3D4B962648BCBFA7);
source user: address = 0x1FcCC097db89A86Bfc474A1028F93958295b1Fb7;
source threshold: integer = 0;
source morpho: address = 0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb;
source ORACLE_PRICE_SCALE: integer = 10**36;
source marketParams: tuple<address, address, address, address, integer> = Call {
contract: morpho,
signature: "function idToMarketParams(bytes32 id) returns (address, address, address, address, uint256)",
params: tuple(marketId)
};
source lltv: integer = marketParams[4];
source oracle: address = marketParams[2];
/* Query the collateral price from the oracle */
source collateralPrice: integer = Call {
contract: oracle,
signature: "function price() view returns (uint256)"
};
/* Query the position of the user on Morpho */
source position: tuple<integer, integer, integer> = Call {
contract: morpho,
signature: "function position(bytes32 id, address user) view returns (uint256, uint128, uint128)",
params: tuple(marketId, user)
};
source borrowed: integer = position[1];
source collateral: integer = position[2];
/* Perform (collateral * lltv) / collateralPrice and multiply by ORACLE_PRICE_SCALE */
source maxBorrow: integer = (collateral * lltv * collateralPrice) / ORACLE_PRICE_SCALE;
source healthFactor: integer = maxBorrow / borrowed;
rule {
description: "Morpho health factor is $healthFactor (<$threshold)",
condition: borrowed == 0 ? true : healthFactor > threshold
};Last updated